brian_k
asked on
Struct within a class
I'm a novice programmer learning how to master classes and structs. My assignment is to use a struct within a class. The struct "Item" has these members:
int Code
string Nomenclature
float Price
If anyone can help with questions:
1. Would it be best to abandon the struct and just code it as a class object?
2. Has anyone done a struct in a class? Can't find an example of this.
thanks in advance for any tips here.
int Code
string Nomenclature
float Price
If anyone can help with questions:
1. Would it be best to abandon the struct and just code it as a class object?
2. Has anyone done a struct in a class? Can't find an example of this.
thanks in advance for any tips here.
do you mean nest the declaration of the struct within the class or simple use the struct in the class, the latter for example
struct my_struct
{
int i;
};
class my_class
{
private:
struct my_struct *array_of_structs;
public:
struct my_struct *get_all_structs();
};
to tell you the truth i dont know if C++ supports nesting of classes etc, java does, neitod will know this for sure
struct my_struct
{
int i;
};
class my_class
{
private:
struct my_struct *array_of_structs;
public:
struct my_struct *get_all_structs();
};
to tell you the truth i dont know if C++ supports nesting of classes etc, java does, neitod will know this for sure
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
C++ allows nested classes (and structs), also called member classes. If you want to you can declare them within the encompassing class, and define them later:
class parentClass {
struct Item;
Item *aPointerToThatStruct;
};
struct parentClass::Item {
int Code;
string Nomenclature;
float Price;
};
>>1. Would it be best to abandon the struct and just code it as a class object?
Well, there is a lot of discussion on the use of structs. As you will have noticed, they're almost redundant - given that they're just "public by default" classes. Many people use them when they want an object that simply stores information, in the way that Item does. It is reasonable to model Item as a struct.
class parentClass {
struct Item;
Item *aPointerToThatStruct;
};
struct parentClass::Item {
int Code;
string Nomenclature;
float Price;
};
>>1. Would it be best to abandon the struct and just code it as a class object?
Well, there is a lot of discussion on the use of structs. As you will have noticed, they're almost redundant - given that they're just "public by default" classes. Many people use them when they want an object that simply stores information, in the way that Item does. It is reasonable to model Item as a struct.
rpsingh99:
You hadn't made your answer when I started my comment. I wouldn't have bothered starting if I'd seen it, given that they're almost identical :-)
You hadn't made your answer when I started my comment. I wouldn't have bothered starting if I'd seen it, given that they're almost identical :-)
ASKER
OK thanks for the help
ASKER
I'm impressed by the quick and numerous responses. I salute you all
struct Item
{
int Code;
string Nomenclature;
float Price;
};
class test
{
private:
struct Item str;
int j;
public:
....
};
If you have set and get methods on the members of your class, then I suggest you go for a class, else you can use a struct.
Hope this helps!!!