We help IT Professionals succeed at work.

Struct within a class

brian_k
brian_k asked
on
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.
Comment
Watch Question

Example of how to use struct and class is:
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!!!
CERTIFIED EXPERT

Commented:
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
yes ! u can use structure within a class. and u can either embedd the structure decleration outside the class or within the class itself.

e.g:

struct node {
       int data;
       node *link;     //will carry address for next node
};

class linkedlist {
       node *p;
    public:
        linkedlist(void);    //constructor
        void insertatbeg(int data);  //member function
        --------------------------
        --------------------------
};


                        OR


class linkedlist {

       struct node {
       int data;
       node *link;     //will carry address for next node
       };

       node *p;

    public:
        linkedlist(void);    //constructor
        void insertatbeg(int data);  //member function
        --------------------------
        --------------------------
};
       


so, u can define ur structure accordingly.
Normally, we use structure when we have to use the hetrogeneous data only. and classes when we also need to define functions for that data. But, this is not a hard and fast rule. u can always use both in structures and classes as well. The only difference is that members of a structure are public by default n members of a class are private by default.

Commented:
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.

Commented:
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 :-)

Author

Commented:
OK thanks for the help

Author

Commented:
I'm impressed by the quick and numerous responses. I salute you all

Explore More ContentExplore courses, solutions, and other research materials related to this topic.