help with class varialbe definition..

This is a discussion on help with class varialbe definition.. within the C++ Programming forums, part of the General Programming Boards category; i am going to show you what i want in code cause its kinda hard to explain... Code: class whatever ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    help with class varialbe definition..

    i am going to show you what i want in code cause its kinda hard to explain...

    Code:
    class whatever {
        int int1;
    };
    
    class whatever2:whatever {
        //is it possible to define the default value of int1 here?
    };
    would i use a constructor for that or what?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    A default value, yes. You can do this many different ways:

    Code:
    class whatever
    {
        int int1;
    };
    
    class whatever2 : public whatever
    {
    public:
        whatver2(int value = 0) : int1(value)
        {
        }
    };
    With that you can create an object of type whatever2 and they will have a default value of 0 if you don't specify an argument when you instantiate the object or whatever value you do pass during instantiation:
    Code:
    whatever2 foo1;     // foo1.int1 = 0
    whatever2 foo2(0);  // foo2.int1 = 0
    whatever2 foo3(10); // foo3.int1 = 10
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    hk_mp5kpdw's example won't work because int1 is private in the base class. If it was public (usually bad design) then it would work. Another solution would be to have the base class constructor take a value for int1, and then call the base class constructor.
    Code:
    class whatever
    {
        int int1;
    public:
        whatever(int value = 0) : int1(value)
    };
    
    class whatever2 : public whatever
    {
    public:
        whatever2() : whatever(5)
        {
        }
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class definition error
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 05-16-2009, 11:26 AM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Class Definition?
    By Jamina in forum C++ Programming
    Replies: 4
    Last Post: 08-07-2003, 11:12 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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