Initialize parameters in base class : initialization syntax « Class « C++ Tutorial






#include <iostream.h>

class Pointer
{
private:
       long x,y;
public:
       Pointer(long a=0,long b=0) { 
           x=a;
           y=b;
    }
       
       long GetX(){ 
           return x;
    }
       long GetY(){ 
           return y;
    }
       void Disp(){ 
           cout << "x=" <<x<<"  y="<<y;
    }
};

class Pointer3D : public Pointer
{
       long z;
public:
       Pointer3D(long a=0,long b=0,long c=0):Pointer(a,b){
           z=c;
       }
       long GetZ(){ return z;}
       
       void Disp()
       {
               cout <<"x= "<<GetX()<<"  y="<<GetY();
               cout << "  z="<< z <<endl;
    }
};

main()
{
   Pointer3D p1;
   Pointer3D p2(3,-4);
   p2.Disp();
   p2=p1;
   p2.Disp();
   return 0;
}
x= 3  y=-4  z=0
x= 0  y=0  z=0








9.8.initialization syntax
9.8.1.Initialize variables from base class using initialization syntax
9.8.2.Initialize numA and numB inside the MyClass constructor using normal syntax
9.8.3.Initialize parameters in base class