Example of Struct inside Class.

This is a discussion on Example of Struct inside Class. within the C++ Programming forums, part of the General Programming Boards category; Hi, I have been trying to learn C++ and I don't know how to use a struct inside a class, ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Example of Struct inside Class.

    Hi,
    I have been trying to learn C++ and I don't know how to use a struct inside a class, when both the struct and the class have Constructors defined. I'd be grateful if someone can point out where I'm going wrong in the following example.

    Thanks a lot.
    Code:
    struct Items{
                int qty;
                Items(int input){qty = input;}//constructor.
    };
    
    class Basket{
          private:
                Items InBasket;
          public:
                Basket(int inval);
                void GetStuff();
    };
    
    Basket::Basket(int inval){
          Items(inval);
    }
    
    void Basket::GetStuff(){
          cout<<"\nIn basket = "<<InBasket.qty;
    }
    
    int main(void){
          Basket B1(5);
          B1.GetStuff();
          return(EXIT_SUCCESS);
    }

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    EDIT** This is what you want

    Code:
    #include <iostream>
    using namespace std;
    
    struct Items{
        int qty;
        Items(int qty):qty(qty){}
    };
    
    class Basket{
        Items *InBasket;
    public:
        Basket(int inval):InBasket(new Items(inval)){}
        void GetStuff(){
            cout << endl << "Inbasket = " << InBasket->qty << endl;
        }
        ~Basket(){delete InBasket;}
    
    };
    
    int main(void){
    
        Basket B1(5);
        B1.GetStuff();
    
        return(0);
    }
    Last edited by Syscal; 09-15-2010 at 09:31 PM.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Quote Originally Posted by Syscal View Post
    EDIT** This is what you want

    Code:
    #include <iostream>
    using namespace std;
    
    struct Items{
        int qty;
        Items(int qty):qty(qty){}
    };
    
    class Basket{
        Items *InBasket;
    public:
        Basket(int inval):InBasket(new Items(inval)){}
        void GetStuff(){
            cout << endl << "Inbasket = " << InBasket->qty << endl;
        }
        ~Basket(){delete InBasket;}
    
    };
    
    int main(void){
    
        Basket B1(5);
        B1.GetStuff();
    
        return(0);
    }
    Thanks ! this works like a charm. However I have a question, what does the ":" notation mean? I mean what does the second line in the struct declaration say? Is it just initializing the value of qty with the supplied value? Please let me know, thanks again.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    More or less, that is the idea: in inheritance hierarchies and with const members, initialization lists are required. Your actual problem is solved through assignment also, if you wish to avoid constructs you haven't learned.

    Code:
    inBasket = inval;
    // Actually calls Basket(int)
    There is one particular case where assignment looks like this:
    Code:
    inBasket = Items(inval);
    // The constructor is 'explicit' and must be called this way
    Explicit constructors should be a class of constructors that always have one argument. By declaring a class constructor explicit you are saying that you can't call the constructor this way:
    Code:
    Items i = 5;
    As a more important aside, note that Syscal has changed the member of Basket to be a pointer. While in this case there is little difference it would perhaps have been more instructive to stick to one example.
    Last edited by whiteflags; 09-15-2010 at 11:23 PM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,635
    The new is absolutely unnecessary, actually.
    Taking syscal's example, this should work:
    Code:
    #include <iostream>
    using namespace std;
    
    struct Items
    {
        int qty;
        Items(int qty): qty(qty) {}
    };
    
    class Basket
    {
        Items InBasket;
    public:
        Basket(int inval): InBasket(inval) {}
        void GetStuff()
        {
            cout << endl << "Inbasket = " << InBasket.qty << endl;
        }
    };
    
    int main()
    {
        Basket B1(5);
        B1.GetStuff();
    
        return 0;
    }
    Last edited by Elysia; 09-16-2010 at 07:14 AM. Reason: Fixed code bug
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Then you have to change:

    InBasket->qty

    to

    InBasket.qty


    Also, the ":" notation is an initialization list. http://www.cprogramming.com/tutorial...lists-c++.html

    And yes, it just initializes a field. So take this for example:

    some_ctor(int value):some_private_field(value){}

    "some_private_field" gets initialized to "value".
    Last edited by Syscal; 09-16-2010 at 07:13 AM.

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Thanks a lot to all of you for the help provided.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm donating my vector lib.
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 06-24-2010, 06:10 PM
  2. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 01:11 PM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  4. Struct inside a Struct inside a Struct?!?
    By tegwin in forum C++ Programming
    Replies: 8
    Last Post: 05-02-2003, 11:50 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21