Thread: chain of constructors

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    35

    Question chain of constructors

    hi!
    There is a base class in my program from which I have built a subclass.
    As you can see, I have defined 2 constructors for my base class "Account".
    Code:
    class Account
    {
    public:
    	int account_number;
    	int Deposit;
    
    	Account(int number, int money): account_number(number) , Deposit(money) {}
    	Account(int number)
    	{
    		account_number=number;
    		Deposit=0;
    	}
    }
    Then There is my subclass defined as:
    class ShortTerm: public Account

    I've got 2 questions here.
    first, isn't it true that when the attributes"account_number" and "Deposit" have been declared in my base class, the subclass is prone to know them? so i don't have to declare them in the subclass once more? and if I do, don't they represent some other new attributes with the scope ShortTerm( for instance-> ShortTerm:: Deposit ) which coincidently have the same name as the attributes of the base class?
    if all what I've written above is true, then why can't I set Deposit and account_number in the base class constructor?
    I've tried this:
    Code:
    ShortTerm(int number, int money): account_number(number) , Deposit(money){
    interest=12;
    }
    //interest is a new attribute declared in the subclass ShortTerm
    it draws a line under Deposit and account_number with the following error:
    "Deposit" is not a non static data member or base class of class ShortTerm.

    now, my second question:
    how can I call a chain of constructors?
    having confronted the error mentioned above, I came up with the idea of calling the constructor of the base class in the constructor of the subclass. so that by this method, I may succeed to set the values of Deposit and account_number.
    By the way, is it necessary to do so?

  2. #2
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    first, isn't it true that when the attributes"account_number" and "Deposit" have been declared in my base class, the subclass is prone to know them? so i don't have to declare them in the subclass once more?
    Wrong. Only sub-classes objects can know the information.,....
    how can I call a chain of constructors?
    When you call a subclass constructor, it automates itself to the parent class constructor.
    NOTE: I'll recommend you to read the OOP tutorials.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    In the subclass constructor, you call the base class constructor in the initializer list:

    Code:
    ShortTerm(int number, int money): Account(number, money), interest(12) { }
    The constructor should only directly set the fields defined in that class. Fields defined in a base class should be handled by a base class constructor.

    Also, in your example of Account, the second constructor which passes a single argument should use an initializer list, just as the first one does.

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    35
    ok thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constructors
    By MarlonDean in forum C++ Programming
    Replies: 3
    Last Post: 06-18-2008, 01:21 AM
  2. Protecting Constructors
    By kidburla in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2006, 11:18 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM