Friday, 12 August 2011

Example program for default, parametrized and copy constructor

//Example program for default, parametrized and copy constructor
#include<iostream.h>
#include<conio.h>
class rational
{
 private:
  int nr,dr;
public:
 rational()
 {
   nr=22;
  dr=7;
 }
rational(int nr1,int dr1)
 {
   nr=nr1;
   dr=dr1;
 }
rational(int nr1)
 {
   nr=nr1;
   dr=nr1+1;
 }
rational(rational &a)
 {
   nr=a.dr;
   dr=a.nr;
 }
~rational()
 cout<<"obj. is lost\n";
}
void display()
{
 cout<<nr<<"/"<<dr<<endl;
}
};
void main()
{
 rational a,b(5,8),c(5);
 rational d(b); //or rational d=b;
rational f;
f=b;
rational &e=c;
a.display();
b.display();
c.display();
d.display();
e.display();
f.display();
getch();
}

No comments:

Post a Comment