1.

g CONSTRUCTOR AND DESTRUCTOR

Aim:
To write a C++ program to add two numbers using constructor and destructor.

Algorithm:
Step 1: Start the program.
Step 2: Create a class as add
Step 3: Create a constructor and perform addition of two
numbers.
Step 4: Use destruction fuction.
Step 5: End of the program
.
Ex no:1g // Constructor and Destructor
#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()

Step 2: Create a class as space.display(). Step 4: Create operator overloading function to change the sign of x Step 4: Display the x value.{ cout<<”The addition of 2 number is”<<c. Algorithm: Step 1: Start the program. clrscr(). add a1(c. } void main() { int c.d). cout<<”Enter the value of c and d”<<”\n”. Step 3: set a value for x.d. cin>>c>>d. } Result: Thus the C++ program to add two numbers using constructor and destructor was verified and executed successfully 1. Step 5: End of the program . a1.h UNARY OPERATOR OVERLOADING Aim: To write a C++ program to set and display a value using unary operator overloading.

-s. .h> class space { int x. } void display() { cout<<”\n”<<"x:"<<x. } }. cout<<"S values".setvalue().display(). cout<<endl. s. void main() { clrscr(). getch().display(). s. space s. } void operator -() { x=-x. public: void setvalue() { x=10. s. cout<<"S values".Ex no:1h// Unary Operator Overloading #include<iostream. } Result: Thus the C++ program to set and display a value using unary operator overloading was verified and executed successfully. cout<<endl.h> #include<conio.

Algorithm: Write it by urself Program: Refer class note 1.max is friend of both the classes Step 4: Find the maximum of two numbers and display it.1.i BINARY OPERATOR OVERLOADING Aim: To write a C++ program to add two complex number using binary operator overloading. Step 5: End of the program . Step 3: Create a friend function. Algorithm: Step 1: Start the program Step 2: Create classes abc and xyz.j FRIEND FUNCTION Aim: To write a C++ program to find the maximum of two numbers using friend function.xyz n). friend void max(abc m. in both the classes.

} friend void max(abc m. } void main() { abc a. }.setvalue().b). getch(). a. b. void max(abc m. } friend void max(abc m. xyz b. else cout<<n. class abc { int a. } . public: void setvalue() { a=10.xyz n).setvalue().ex no:1j // Friend Function #include<iostream.xyz n) { if(m. max(a. class xyz { int b. }.b) cout<<m. public: void setvalue() { b=20.h> #include<conio.b.a >= n.a.h> class xyz.xyz n).

h> class myclass { int a.Result: Thus the C++ program to find the maximum of two numbers using friend function was verified and executed successfully 1. Step 5: End of the program. friend myclass operator+(myclass.k OVERLOAD ADDITION OPERATOR USING FRIEND FUNCTION Aim: To write a C++ program Overload addition operator Using friend function for complex numbers.h> #include<conio.myclass). Step 2: Create a class as myclass. Step 3: Create a Friend Function. Step 4: Add and display the complex number as output. Algorithm: Step 1: Start the program. int b. Ex no:1k// overload addition operator Using friend functions For complex numbers #include<iostream. public: .

d. myclass b(e. } void main() { clrscr().b. cin>. cout<<”a=”.f. temp. cin>.myclass).f myclass a(c.myclass ob2) { myclass temp. int c. myclass g.d).a. temp. g=a+b.b = ob1. }. a.e.show().a = ob1. return temp. } Result: .myclass(){} myclass(int x.f).a + ob2. } friend myclass operator+(myclass.e.b + ob2.show().show().d. myclass operator+(myclass ob1. cout<<”result=”. b=y.int y) { a=x. b. g.c>. cout<<”b=”. cout<<”Enter first complex number values c and d”. cout<<”enter second complex number values e and f”. } void show() { cout<<”Complex value addition is” cout<<a<<”+j”<<b<<endl.

Algorithm: Step 1: Start the program. Step 2: Create a class as base. Step 5: Create a function void show() Step 6: Display the current status. . Step 3: Create a virtual function virtual void show() Step 4: Create a class as derived. 1.Thus the C++ program to add two complex numbers using operator overloading-friend functions was verified and executed successfully.l VIRTUAL FUNCTION Aim: To write a C++ program to display the current status of a class using virtual function. Step 7: End of the program.

} }. } void show() { cout<<"\n show derived". bptr->display(). void main() { base b. derived d. } }.Ex no: l L // Virtual Function #include<iostream. cout<<"\n bptr points to base:". bptr=&b. } virtual void show() { cout<<"\n show base". base *bptr. . class derived:public base { public: void display() { cout<<"\n display derived". bptr->show().h> class base { public: void display() { cout<<"\n display base". clrscr().h> #include<conio.

cout<<"\n bptr points to derived:". Algorithm: Step 1: Start the program.} Result: Thus the C++ program to display the current status of a class using virtual function was verified and executed successfully.m VIRTUAL CLASS Aim: To write a C++ program to find addition and subtraction of two numbers using virtual class. Step 7: End of the program. bptr->display(). Step 6: create manipulate as derived class from sub classes add and sub.Display the addition and subtraction of two numbers. 1. . Step 3: Create add and sub as virtual classes Step 4: Add the two numbers in derived class add. getch(). Step 5: Subtract the two numbers in derived class sub. bptr->show(). bptr=&d. Step 2: Create a class as arithmetic.

} }. cout<<"Enter B:". public: void sum_ab() { sum=a+b. cin>>a. A:"<<a. public: void diff_ab() { diff=a-b.b.h> #include <conio. } void disp_sum() { cout<<"\n cout<<"\n cout<<"\n cout<<"\n } Addition of two numbers:". cin>>b. B:"<<b. }.ex no:1m// Virtual Class #include <iostream. class sub:virtual public arithmatic { protected: int diff. class add:virtual public arithmatic { protected: int sum.h> class arithmatic { protected: int a. Sum:"<<sum. . public: void get_ab() { cout<<"Enter A:".

m1. } }. diff_ab(). class manipulate:public add.get(). disp_diff(). } Result: Thus the C++ program to find addition and subtraction of two numbers using virtual class was verified and executed successfully. void main() { clrscr().cal(). }. manipulate m1.disp(). } void cal() { sum_ab(). B:"<<b. A:"<<a. Difference:"<<diff.} void disp_diff() { cout<<"\n cout<<"\n cout<<"\n cout<<"\n } Difference of two numbers:". } void disp() { disp_sum(). m1. m1. public sub { public: void get() { get_ab(). .