Headlines
Loading...
C++ PROGRAM  Constructors in derived class

C++ PROGRAM Constructors in derived class

 //Constructors in derived class

#include<stdio.h>

#include<iostream.h>

#include<conio.h>

class counter

{

 protected:

  unsigned int count;

 public:

  counter()    //constructor,no args

  {

  count=0;

  }

  counter(int c) //constructor one arg

  {

   count=c;

  }

  unsigned int get_count() const //return count

  {

   return count;

  }

  counter operator ++() //incr count(prefix)

  {

   return counter(++count);

  }

};

class countdn:public counter

{

 public:

  countdn():counter()     //constructor,no args

  { }

  countdn(int c):counter(c) //constructor one arg

  { }

  countdn operator --()     //decr count(prefix)

  {

   return countdn(--count);

  }

};

void main()

{

 countdn c1;   //class countdn

 countdn c2(100);

 clrscr();

 cout<<"\nc1="<<c1.get_count();  //display

 cout<<"\nc2="<<c2.get_count();  //display

 ++c1;  //increment c1

 ++c1;

 ++c1;

 cout<<"\nc1="<<c1.get_count(); //display c1

 --c2; //decrement

 --c2;

 cout<<"\nc2="<<c2.get_count();  //display c2

 countdn c3=--c2; //creates c3 from c2

 cout<<"\nc3="<<c3.get_count();  //display c3

 cout<<endl;

 getch();

}

*** PLEASE checkout the Best deals from for top sites like Amazon, Flipkart etc ***