Welcome To Uday Satya Blog

51-60 PROGRAMS



/********************************************************
         51.  Program to implement default constructor
**********************************************************/
#include
#include
class test
{
             int m,n;
public:
             test()
             {
             m=0;
             n=0;
             }
             void display()
             {
             cout<<"m="<
             cout<<"n="<
    }
};
void main()
{
 test t1;
 clrscr();
 t1.display(); //invoking a default constructor
 getch();
}

Output:

m=0
n=0







/***************************************************************
     52. Program to implement parameterized constructor
*****************************************************************/
#include
#include
class test
{
              int m,n;
public:
              test(int a,int b)
              {
              m=a;
              n=b;
              }
              void display()
              {
              cout<<"m="<
              cout<<"n="<
              }
};
void main()
{
              test t1(10,20);// invoking constructor implicitly
              test t2=test(20,40);//invoking explicitly
              clrscr();
              t1.display();
              t2.display();
              getch();
}

Output:
  
m=10
n=20
m=20
n=40



/**********************************************************
      53. Program for implementation of copy constructor
************************************************************/
#include
#include

class test
            {
            int x,y;
            public :
                        test(int a,int b)
                        {
                        x = a;
                        y = b;
                        }
                        test(test &t)
                        {
                        x = t.x;
                        y = t.y;
                        }
                        void putdata()
                        {
                        cout<<"x = "<                        }
            };
void main()
            {
            clrscr();
            test t1(34,99);
            test t3(t1);
            cout<<"Invoking Constants Implicitly"<
            t3.putdata();
            test t2 = t3;
            cout<<"Invoking Constants Explicitly"<
            t2.putdata();
            getch();
            }


OUTPUT

Invoking Constants Implicitly

x = 34   y = 99

Invoking Constants Explicitly

x = 34   y = 99






























/***********************************************************
      54. Program to implement overloaded constructor
************************************************************/

#include
#include
class sample
{
private:
                         int a;
public:
                         sample()//default constructor
                         {
                         a=5;
                         }
                         sample(int b)//parameterized constructor
                         {
                         a=b;
                         }
                         sample(sample &s)//copy constructor
                         {
                         a=s.a;
                         }
                         void display()
                         {
                         cout<<"value of a="<
}
};
void main()
{
                         sample s;
                         sample t(10);//invoking parameterized constructor implicitly
                         sample g(t);//copy constructor explicitly
                         sample h=sample(15); //invoking parameterized constructor explicitly
                         sample k=t;//invoking copy constructor
                         s.display();
                         t.display();
                         g.display();
                         h.display();
                         k.display();
                         getch();
}



Output:

value of a=5
value of a=10
value of a=10
value of a=15
value of a=10
























/*******************************************************************
55.  Program to print Fibonacci series using constructor
*******************************************************************/
#include
#include
class fib
            {
            int a,b,f;
            static int x;
            public :
                        fib()
                        {
                        a = 0;
                        b = 1;
                        f = a + b;
                        }
                        void next()
                        {
                        a = b;
                        b = f;
                        f = a + b;
                        }
                        void showdata()
                        {
                        cout<
                        }
            };
void main()
            {
            clrscr();
            fib f;
            cout<<"Enter the limit : "<
            int n;
            cin>>n;
            for(int i = 0;i
                        {
                        f.showdata();
                        f.next();
                        }
            getch();
            }



OUTPUT   :

Enter the limit :
8

0
1
1
2
3
5
8
13




                                                                                
                                                                               
                                                                               
                                                                                
                                                                               
                                                                               
                                                                                
                                                                               
                                                                               
                                                                               
             
                                                                 
                                                                               
                                                                               
                                                                                
                                                                               
                                                               
/******************************************************************
   56.To find gcd of two numbers using constructor and destructors
*******************************************************************/
#include
#include
class gcd
{
             int m,n,p;
public:
             gcd(int a,int b)
             {
             m=a;
             n=b;
             }
             void display();
             void gcd1();
    ~gcd();

};

void gcd::display()
{
cout<<"m="<
cout<<"n="<
}

void gcd::gcd1()
{
p=m%n;
while(p!=0)
{
m=n;
n=p;
p=m%n;
}
cout<<"the gcd is:"<
}
gcd::~gcd()
{
cout<<"object has been destroyed"<
}
void main()
{

 gcd t(12,9);

 clrscr();

 t.display();
 t.gcd1();
 getch();
}




Output:

m=12
n=9
the gcd is:3

















/***************************************************************
     57. To add 2 complex nos(use of objects as function args)
****************************************************************/
#include
#include
class complex
{
             int a,b;
public:
             void get()
             {
             cout<<"enter real and imaginary part"<
             cin>>a>>b;
             }
             friend complex sum(complex c1,complex c2)
             {
             complex c3;
             c3.a=c1.a+c2.a;
             c3.b=c1.b+c2.b;
             return(c3);
             }
             void display()
             {
             cout<
             }};
             void main()
             {
             complex s1,s2,s3;
             s1.get();
             s2.get();
             s3=sum(s1,s2);
             s1.display();
             s2.display();
    cout<<"after addition:"<
             s3.display();
             getch();
             }




Output:

enter real and imaginary part
2
3
enter real and imaginary part
5
6
2+i3
5+i6

after addition:
7+i9
























/******************************************************************
58. To find mean of two numbers using friend function
******************************************************************/
#include
#include
class sample
{
                        float a,b;
public:
                        void accept();
                        friend float mean(sample m);
};
void sample::accept()
{
cout<<"enter a and b values"<
cin>>a>>b;
}
float mean(sample m)
{
return((m.a+m.b)/2);
}
void main()
{
sample m;
clrscr();
m.accept();
float f=mean(m);
cout<<"mean="<
getch();
}

Output:

enter a and b values
8
10
mean=9


/************************************************************
       59. To add 2 complex numbers using friend function
**************************************************************/
#include
#include
class complex
{
            int a,b;
public:
            void input(int x,int y)
            {
              a=x;
              b=y;
            }
            friend complex sum(complex c1,complex c2);
            void display()
            {
              cout<
            }
};
complex sum(complex c1,complex c2)
{
  complex c3;
  c3.a=c1.a+c2.a;
  c3.b=c1.b+c2.b;
  return(c3);
}
void main()
{
  clrscr();
  complex c1,c2,c3;
  c1.input(2,3);
  c2.input(4,5);
  c3=sum(c1,c2);
  cout<<"c1=";
  c1.display();
  cout<<"c2=";
  c2.display();
  cout<<"c3=";
  c3.display();
  getch();
}


OUTPUT :

c1 = 2+i3

c2 = 4+i5

c3 = 6+i8















/*****************************************************
        60Program to add time using friend function
******************************************************/

#include
#include
class time
{
            int hrs,min;
public:
            void gettime(int h,int m)
            {
              hrs=h;
              min=m;
            }
            void add(time t1,time t2)
            {
              min=t1.min+t2.min;
              hrs=min/60;
              min=min%60;
              hrs=hrs+t1.hrs+t2.hrs;
            }
            void puttime()
            {
              cout<  "<
            }
};
void main()
{
  time t1,t2,t3;
  clrscr();
  t1.gettime(2,45);
  t2.gettime(3,30);
  t3.add(t1,t2);
  cout<<"t1= ";
  t1.puttime();
  cout<<"t2= ";
  t2.puttime();
  cout<<"t3= ";
  t3.puttime();
  getch();
}



OUTPUT  :

t1=2 hrs     45min

t2=3 hrs     30min

t3=6hrs      15min

0 comments:

Post a Comment