Professional Documents
Culture Documents
To Write A C++ Program To Add Two Numbers Using Constructor and Destructor
To Write A C++ Program To Add Two Numbers Using Constructor and Destructor
Aim:
To write a C++ program to add two numbers using constructor and destructor.
Algorithm:
numbers.
#include<iostream.h>
#include<conio.h>
class add
{
int c;
public:
add(int a, int b); //constructor function
void display();
~add() //destructor function
{
cout<<"Objects are destroyed”<<endl;
}
};
add::add(int a, int b)
{
c=a+b;
}
void add:: display()
{
cout<<”The addition of 2 number is”<<c;
}
void main()
{
int c,d;
clrscr();
cout<<”Enter the value of c and d”<<”\n”;
cin>>c>>d;
add a1(c,d);
a1.display();
}
Result:
Thus the C++ program to add two numbers using constructor and destructor was verified and
executed successfully
Aim:
To write a C++ program to set and display a value using unary operator overloading.
Algorithm:
#include<iostream.h>
#include<conio.h>
class space
{
int x;
public:
void setvalue()
{
x=10;
}
void display()
{
cout<<”\n”<<"x:"<<x;
}
void operator -()
{
x=-x;
}
};
void main()
{
clrscr();
space s;
s.setvalue();
cout<<"S values";
s.display();
cout<<endl;
-s;
cout<<"S values";
s.display();
cout<<endl;
getch();
}
Result:
Thus the C++ program to set and display a value using unary operator overloading was
verified and executed successfully.
1.i BINARY OPERATOR OVERLOADING
Aim:
To write a C++ program to add two complex number using binary operator overloading.
Algorithm:
Write it by urself
Program:
Aim:
To write a C++ program to find the maximum of two numbers using friend function.
Algorithm:
friend void max(abc m,xyz n); in both the classes.max is friend of both the
classes
#include<iostream.h>
#include<conio.h>
class xyz;
class abc
{
int a;
public:
void setvalue()
{
a=10;
}
friend void max(abc m,xyz n);
};
class xyz
{
int b;
public:
void setvalue()
{
b=20;
}
friend void max(abc m,xyz n);
};
void main()
{
abc a;
a.setvalue();
xyz b;
b.setvalue();
max(a,b);
getch();
}
Result:
Thus the C++ program to find the maximum of two numbers using friend function was
Aim:
To write a C++ program Overload addition operator Using friend function for complex
numbers.
Algorithm:
#include<iostream.h>
#include<conio.h>
class myclass
{
int a;
int b;
public:
myclass(){}
myclass(int x,int y)
{
a=x;
b=y;
}
void show()
{
cout<<”Complex value addition is”
cout<<a<<”+j”<<b<<endl;
}
friend myclass operator+(myclass,myclass);
};
void main()
{
clrscr();
int c,d,e,f;
cout<<”Enter first complex number values c and d”;
cin>.c>.d;
cout<<”enter second complex number values e and f”;
cin>.e,f
myclass a(c,d);
myclass b(e,f);
myclass g;
g=a+b;
cout<<”a=”;
a.show();
cout<<”b=”;
b.show();
cout<<”result=”;
g.show();
}
Result:
Thus the C++ program to add two complex numbers using operator overloading-friend
functions was verified and executed successfully.
Aim:
To write a C++ program to display the current status of a class using virtual function.
Algorithm:
void show()
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
void main()
{
base b;
derived d;
base *bptr;
clrscr();
cout<<"\n bptr points to base:";
bptr=&b;
bptr->display();
bptr->show();
cout<<"\n bptr points to derived:";
bptr=&d;
bptr->display();
bptr->show();
getch();}
Result:
Thus the C++ program to display the current status of a class using virtual function was
Aim:
To write a C++ program to find addition and subtraction of two numbers using virtual
class.
Algorithm:
Step 6: create manipulate as derived class from sub classes add and sub.Display the
#include <iostream.h>
#include <conio.h>
class arithmatic
{
protected:
int a,b;
public:
void get_ab()
{
cout<<"Enter A:";
cin>>a;
cout<<"Enter B:";
cin>>b;
}
};
void disp_sum()
{
cout<<"\n Addition of two numbers:";
cout<<"\n A:"<<a;
cout<<"\n B:"<<b;
cout<<"\n Sum:"<<sum;
}
};
void disp_diff()
{
cout<<"\n Difference of two numbers:";
cout<<"\n A:"<<a;
cout<<"\n B:"<<b;
cout<<"\n Difference:"<<diff;
}
};
void cal()
{
sum_ab();
diff_ab();
}
void disp()
{
disp_sum();
disp_diff();
}
};
void main()
{
clrscr();
manipulate m1;
m1.get();
m1.cal();
m1.disp();
}
Result:
Thus the C++ program to find addition and subtraction of two numbers using virtual class
was verified and executed successfully.