Jump to content

structure variable

- - - - -

  • Please log in to reply
14 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi :)

A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are two types of data. Am I correct?) about something as a new type (I think using "type" is not a good choice here because in itself a structure has no 'type', what do you say?) with an identifier. In other words, a structure, let's say St1, combines two or more kinds of variables under the same umbrella, and then whenever some other variable, x, is declared using St1 x; one needs to assign as much values to x as there are in the St1. A structure is an 'umbrella' variable declaration. Please correct me.

These points should be remembered:
1: The variable, such as x above, declared using a structure cannot be read or written like a normal variable such as int y. e.g. You cannot write: cin >> x, or, cout << x.
2: One structure variable could be assigned to another variable of the same structure. e.g. If x and z both are St1 variables, then it can be written: x = y.
3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
4: A structure can be nested within another structure.

Please review the above material and please make corrections if necessary. Thanks a lot.

And if you run or check out the output of the below given code you will notice that the second tab character "\t" produces more space than the first. Notice the distance between "1" and "2", and between "2" and "3".

Sample code:

#include <iostream>

#include <cstdlib>


using namespace std;


struct date

{

    int day; int month; int year;

};


int main ()


{

    date D1, D2;


    {

        cout << "Enter the data for Day No. 1" << endl;

        cout << "Enter Day: "; cin >> D1.day;

        cout << "Enter Month: "; cin >> D1.month;

        cout << "Enter Year: "; cin >> D1.year;

    }


        cout << " " << endl;


    {

        cout << "Enter the data for Day No. 2" << endl;

        cout << "Enter Day: "; cin >> D2.day;

        cout << "Enter Month: "; cin >> D2.month;

        cout << "Enter Year: "; cin >> D2.year;

    }


    cout << " " << endl;


    cout << "Day No. 1 Detail: " << D1.day << "\t" << D1.month << "\t" << D1.year << endl;


    cout << "Day No. 2 Detail: " << D2.day << "\t" << D2.month << "\t" << D2.year << endl;


    system("pause");


}


Output:

Enter the data for Day No. 1

Enter Day: 1

Enter Month: 2

Enter Year: 3


Enter the data for Day No. 2

Enter Day: 4

Enter Month: 5

Enter Year: 6


Day No. 1 Detail: 1     2       3

Day No. 2 Detail: 4     5       6

Press any key to continue . . .


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Seb said:

I have a series of questions for you. Please demonstrate the ability to answer basic questions by answering them.
  • Which book are you reading? I've asked you this one before, numerous times.
  • Are you incapable of answering such basic questions?
  • Are you incapable of reading accurately?

Once you have demonstrated the ability to answer basic questions, I'll tell you where you can find decent answers to your questions.

Sir, did you read the following post? Please have a loook I did answer to the best of ability!:)

http://forum.codecal...html#post300767
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
@jackson: I'd say that's a pretty accurate description

@Seb: what is your obsession with books? They're nice (I have a lot), but many people have limited resources and have to make do with learning off the web.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

jackson6612 said:

1: The variable, such as x above, declared using a structure cannot be read or written like a normal variable such as int y. e.g. You cannot write: cin >> x, or, cout << x.
It can, in C++ you can overload operators to do just that.

jackson6612 said:

2: One structure variable could be assigned to another variable of the same structure. e.g. If x and z both are St1 variables, then it can be written: x = y.
True, although sometimes you will need to overload assignment ( = ) operator to correctly copy data. Default assignment operator is created by compiler if you don't create one.

jackson6612 said:

3: The comparison of structure variables cannot be carried out using relational operators like regular variables. e.g. One cannot do: x > z.
You can with overloaded operator. :)

jackson6612 said:

4: A structure can be nested within another structure.
True, but I'm not sure how deep you can go but I'm pretty sure you can go deeper than Inception (ie 5 times).

In C++ structures and classes have the same meaning, only difference is default visibility: in structure by default everything is public and in class everything is private.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, everyone. I'm very much grateful. I'm sorry if I ask something which you think has already been answered in some indirect way which I was able to understand.

1: First thing I would like to ask is about "overload". I only need basic understanding. I think it means to make some operator to do something which it doesn't normally do. I mean to make it do something particular in a certain context. Do you find it correct?

Quote

A structure defines a collection of related data (Data could be in different forms, e.g. variables and constants are two types of data. Am I correct?) about something as a new type (I think using "type" is not a good choice here because in itself a structure has no 'type', what do you say?) with an identifier.

2: Please comment on the red statements.

3: And if you run or check out the output of the below given code you will notice that the second tab character "\t" produces more space than the first. Notice the distance between "1" and "2", and between "2" and "3".

4: I have enclosed the related cout's and cin's within the braces. Does enclosing them within the braces make it a block? If not, then how would you simply define a block?

5: In the last "Example Code" the author has enclosed structure part within series of backslashes "/". Is this normal practice? Doesn't it affect compiling procedure because for making a comment only two "//" are used.

Sample Code:

#include <iostream>

#include <cstdlib>


using namespace std;


struct date

{

    int day; int month; int year;

};


int main ()


{

    date D1, D2;


   [B][COLOR="red"] {[/COLOR][/B]

        cout << "Enter the data for Day No. 1" << endl;

        cout << "Enter Day: "; cin >> D1.day;

        cout << "Enter Month: "; cin >> D1.month;

        cout << "Enter Year: "; cin >> D1.year;

    [COLOR="red"][B]}[/B][/COLOR]


        cout << " " << endl;


    {

        cout << "Enter the data for Day No. 2" << endl;

        cout << "Enter Day: "; cin >> D2.day;

        cout << "Enter Month: "; cin >> D2.month;

        cout << "Enter Year: "; cin >> D2.year;

    }


    cout << " " << endl;


    cout << "Day No. 1 Detail: " << D1.day << "\t" << D1.month << "\t" << D1.year << endl;


    cout << "Day No. 2 Detail: " << D2.day << "\t" << D2.month << "\t" << D2.year << endl;


    system("pause");


}


Output:

Enter the data for Day No. 1

Enter Day: 1

Enter Month: 2

Enter Year: 3


Enter the data for Day No. 2

Enter Day: 4

Enter Month: 5

Enter Year: 6


Day No. 1 Detail: 1     2       3

Day No. 2 Detail: 4     5       6

Press any key to continue . . .


Example Code:

// parts.cpp

// uses parts inventory to demonstrate structures

#include <iostream>

using namespace std;

[COLOR="red"]////////////////////////////////////////////////////////////////[/COLOR]

struct part                   //declare a structure

   {

   int modelnumber;           //ID number of widget

   int partnumber;            //ID number of widget part

   float cost;                //cost of part

   };

[COLOR="red"]////////////////////////////////////////////////////////////////[/COLOR]

int main()

   {

   part part1;                //define a structure variable


   part1.modelnumber = 6244;  //give values to structure members

   part1.partnumber = 373;

   part1.cost = 217.55F;

                              //display structure members

   cout << "Model "  << part1.modelnumber;

   cout << ", part "   << part1.partnumber;

   cout << ", costs $" << part1.cost << endl;

   return 0;

   }


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
1. Something like that, yea. You can bend it according to your own rules, perhaps a wiki might share a better explanation.

2. If you mean constants like when you add const keyword, then this is the same as a normal variable, except you can not change it's value, hence the constant.

3. I'm not sure why second tab produces more space, could be OS related.

4. Yeah, that's a code block but in your case it doesn't really do/mean anything. If you were to declare a variable in that block, then that variable would only be visible inside that block.

5. That is still a comment because it starts with 2 forward slashes and it doesn't matter what follows (it's different for multi line comment however).
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Seb, if you think that my questions are stupid and answers can be found in books and dictionaries... then why are you taking all the pains to reply to them? May I request you something?! Stop hounding me and go read your books, man!
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#8
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
You'll learn them later, but there are things called classes which are a lot like structures. The only real difference is that by default structure data members are public while by default they're private for classes. Private just means that data members of the structure cannot be directly accessed, they are only accessible from public functions. Public means the data member can be directly accessed (i.e. structVariable1.x = 5 would be directly setting the x data member of the structVariable1 to 5). Classes also often have functions within the data type while structures don't. I believe you can have functions in structures, but they're usually not included just to adhere to a sort of common practice, structures were in C and that is how they were handled so people will usually handle them like that in C++ and use classes if they want functions in their created data type. Structures and classes fall under something called an ADT or abstract data type.

So anyway, basically structures and classes are a user created data type. Data types are things like int, double, char, and bool. The data members of an int data type are all of the numbers from 0-whatever, the data members of a bool data type are 0 and 1, etc. Now you created a data type in your code called date and the data members are integers for day, month, year. Now you could also include operations on that data in your structure or class. For instance if you wanted to create a function called IncrementDay() you could then you could call it by creating a variable of your new date data type then using the dot operator to access the member function IncrementDay(). But you should probably get in the practice of using a class instead when including functions. The syntax is pretty much the same except replace struct with class, and then make the data members public if you want or create public functions to access the private data members.


Struct:


struct date
{
    int day; int month; int year; //these are public by default
};

int main ()

{
    date D1, D2;

    D1.day++;

return 0;
}
Class:




class date
{
    int day; int month; int year;   //these are private by default
public:
    void IncrementDay();            //this can access those private data members
};

int main ()

{
    date D1, D2;

    D1.IncrementDay();

return 0;
}


#9
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts

Flying Dutchman said:

1. Something like that, yea. You can bend it according to your own rules, perhaps a wiki might share a better explanation.

2. If you mean constants like when you add const keyword, then this is the same as a normal variable, except you can not change it's value, hence the constant.

3. I'm not sure why second tab produces more space, could be OS related.

4. Yeah, that's a code block but in your case it doesn't really do/mean anything. If you were to declare a variable in that block, then that variable would only be visible inside that block.

5. That is still a comment because it starts with 2 forward slashes and it doesn't matter what follows (it's different for multi line comment however).

Thanks a lot, Dutchman, Zer033.

I meant the constant integers such as "1", "4", "5", etc.

Okay. That means enclosing anything within the braces converts it into a block. That visibility point is important. Suppose if a person has a habit of declaring variables throughout the code (most like to declare variables at the top), then he/she could run into problems when the variable(s) has been declared somewhere within the braces because they would only be visible inside the braces where they were declared and not accessible from the outside.

I'm much grateful for all the help.

Best regards
Jackson

Edited by jackson6612, 02 May 2011 - 05:29 AM.

I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#10
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Overloading can take care of not being able to use > or < relational operators. For instance say you have a structure called Square. Now what makes 1 square greater or less than another square? Probably the length/width. So you could overload the > and < operators to handle that. All you're doing is telling your class how to handle those relational operators. If the data members of length and width are both less than the length and width of the other square then return true, otherwise return false.

struct Square
{
int length, width;

bool operator<(const Square& s) const
bool operator>(const Square& s) const
};
then implement the functions:


bool operator<(const Square& s) const
{
return (width < s.width && length < s.length)
}

bool operator>(const Square& s) const
{
return (width > s.width && length > s.length)
}

Now you can use the relational operators with your struct/class.


main()
{
Square S1, S2;

if (S1 < S2)
cout << "S1 is less than S2";

if (S1 > S2)
cout << "S1 is greater than S2";

return 0;
}



#11
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thank you, Zero033, for teaching me all this.

1: As you have tried to give me some idea what a class is. As you say there isn't much difference between both of them, structure and class. But I have read there is a third related entity called object. Let's talk how they are interrelated. I think an object is a super-set and it contains sets "structure" and "class" within itself. In other words, classes and structures are used to make up objects. I'm after a simple explanation.


2: I would like to request someone to comment on the content in this post.
http://forum.codecal...html#post300894
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#12
Zer033

Zer033

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
An object is what you create from your class or structure. So each object is basically a copy of all the data members and member functions of the class or structure. So in that sense each object works on its own data of the class or structure (unless static variables are declared then those are shared among all objects of that class/structure).

So say you have this class created:

class SomeClass
{
int x, y;

public:
SomeClass();
int getX();
void setX(int);
int getY();
void setY(int);
};
and you implement it like this:

SomeClass::SomeClass()  //this is the constructor upon creation of a SomeClass object x and y will be initialized to 0
{
x=0;
y=0;
}

int SomeClass::getX()
{
return x;
}
int SomeClass::getY()
{
return y;
}
void SomeClass::setX(int px)
{
x = px;
}
void SomeClass::setY(int py)
{
y = py;
}
then in main you can create as many objects as you want of this class and each object works on its own data:


#include <iostream>

using namespace std;

int main()
{
SomeClass object1;
SomeClass object2;
SomeClass object3;

object1.setX(1);
object1.setY(1);

object2.setX(2);
object2.setY(2);

object3.setX(3);
object3.setY(3);

cout << "object 1 members are: " << object1.getX() << " " << object1.getY() << endl;

cout << "object 2 members are: " << object2.getX() << " " << object2.getY() << endl;

cout << "object 3 members are: " << object3.getX() << " " << object3.getY() << endl;

return 0;
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users