Thread: Problem regarding constructor inheritance

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    76

    Problem regarding constructor inheritance

    In this underlying code I have used parameterized constructor both in base class and in derived class but this is not working, I understand that I haven't passed any argument to the the constructor which has been inherited from A by B. How to get out of this mess?


    Code:
    #include<iostream>
    using namespace std;
    class A
        {
            int x;
            public:
            A(int x)
                {
                    this->x=x;
                    cout<<"I am in BASE CONSTRUCTOR "<<x<<endl;
                }
        };
    class B:public A
        {
            int y;
            public:
            B(int y)
                {
                    this->y=y;
                    cout<<"I am in BASE CONSTRUCTOR "<<y<<endl;
                }
        };
    int main()
        {
            A obj(123);
            B ob(345);
            return 0;
        }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Have you tried to use the constructor initialization list?

    Code:
    #include<iostream>
    using namespace std;
    class A
        {
            int x;
            public:
            A(int x) : x(x)
                {
                    cout<<"I am in BASE CONSTRUCTOR "<<x<<endl;
                }
        };
    
    class B:public A
        {
            int y;
            public:
            B(int y) : A(y), y(y)
                {
    
                    cout<<"I am in BASE CONSTRUCTOR "<<y<<endl;
                }
        };
    
    int main()
        {
            A obj(123);
            B ob(345);
            return 0;
        }
    Jim

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    76
    got it, but can we initialize PARAMATERIZED BASE constructor with NON-PARAMATERIZED DERIVED CLASS CONSTRUCTOR?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gaurav#
    got it, but can we initialize PARAMATERIZED BASE constructor with NON-PARAMATERIZED DERIVED CLASS CONSTRUCTOR?
    Yes, though what you are doing is invoking the base class constructor to initialise the base class subobject. The point is that by default initialisation will be attempted using the base class default constructor, but here the base class does not have a default constructor because none was defined and yet another constructor was defined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The base class constructor is just like any normal function - you can pass it any arguments as you normally would a normal function. The source of those arguments is simply what makes most sense in that instance.
    For example, in your code, I can just do B(int y) : A(0) in B's constructor, if it makes sense to do so.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-16-2009, 06:46 AM
  2. inheritance - constructor
    By misterowakka in forum C++ Programming
    Replies: 26
    Last Post: 01-18-2008, 12:22 PM
  3. How do I override constructor in inheritance?
    By Loduwijk in forum C++ Programming
    Replies: 13
    Last Post: 03-24-2006, 09:36 AM
  4. Constructor inheritance
    By Hunter2 in forum C++ Programming
    Replies: 9
    Last Post: 07-14-2004, 10:13 AM
  5. Constructor inheritance
    By Jasel in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 10:43 AM