Monday, 4 February 2013

inheritance in c++ part2


//accessing base less constructer
#include<iostream.h>
#include<conio.h>

class parent              //super class
{
protected:
int n;
public:
   parent()
{
   n=0;
   }

   parent(int p)      //this constructor is of no cost in this program
   {
n=p;
   }

   void show()
   {
   cout<<"n= "<<n<<endl;
   }};

class child : public parent        //sub class
{
protected:
char ch;
public:
child()
   {
   ch='x';
   }

   child (char c,int m) //parameters
   {
   ch=c;
   //n=m;   //if u will activate this line then value of n will be change
//for c2;--------------------1
   }

   void display()
   {
   cout<<"ch= "<<ch;
   }};

   void main()
   {
   child c1;
cout<<"c1 is: \n";
   c1.show();             //parent()::this constructor will execute
   c1.display();         //child():: this constructor will execute

   child c2('w',100);   //arguments
cout<<"\n\nc2 is: \n";
   c2.show();              //again call parent constructor
    //( child(char c,int m):: this constuctor
                           //will execute but is of no cost for the value of 'n'
                           //untill the step 1 is activated

   c2.display();      //as double parametrized constuctor has already been
                      //executed thus the value of "ch" will simply b executed

   getch();
   }

0 comments:

Post a Comment