Quantcast
Channel: Comments on: 11.4 — Constructors and initialization of derived classes
Viewing all articles
Browse latest Browse all 159

By: shraa

$
0
0

I just tried below and it worked:

class Base
{
    public:
            int m_id;
            Base(int i=0):m_id(i){}
            int getID() const { return m_id;}
};

class Derived:public Base
{
    public:
            double d;
            Derived():d(0){}
            Derived(double db=0,int i=0):d(db),Base(i){}
            double getDouble()const {return d;}
};
int main()
{
    Base b;
    Derived dr(0);
    Derived dr1(3.2,5);
    cout<<dr.getDouble()<<" "<<endl;
    cout<<dr1.getDouble()<<" "<<endl;
    cout<<dr1.getID()<<" "<<endl;
    return 0;
    
}

i dont understand difference between
Derived():d(0){}  and
Derived(double db=0):d(db){}


Viewing all articles
Browse latest Browse all 159

Trending Articles