Professional Documents
Culture Documents
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()
{
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:
Step 1: Start the program.
Step 2: Create a class as space.
Step 3: set a value for x.
Step 4: Create operator overloading function to change the sign of x
Step 4: Display the x value.
Step 5: End of the program
Result:
Thus the C++ program to set and display a value using unary operator overloading was
verified and executed successfully.
Aim:
To write a C++ program to add two complex number using binary operator overloading.
Algorithm:
Write it by urself
Program:
Refer class note
Aim:
To write a C++ program to find the maximum of two numbers using friend function.
Algorithm:
Step 1: Start the program
Step 2: Create classes abc and xyz.
Step 3: Create a friend function.
friend void max(abc m,xyz n); in both the classes.max is friend of both the
classes
Step 4: Find the maximum of two numbers and display it.
Step 5: End of the program
Result:
Thus the C++ program to find the maximum of two numbers using friend function was
verified and executed successfully
Aim:
To write a C++ program Overload addition operator Using friend function for complex
numbers.
Algorithm:
Step 1: Start the program.
Step 2: Create a class as myclass.
Step 3: Create a Friend Function.
friend myclass operator+(myclass,myclass);
Step 4: Add and display the complex number as output.
Step 5: End of the program.
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);
};
myclass operator+(myclass ob1,myclass ob2)
{
myclass temp;
temp.a = ob1.a + ob2.a;
temp.b = ob1.b + ob2.b;
return temp;
}
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:
Step 1: Start the program.
Step 2: Create a class as base.
Step 3: Create a virtual function
virtual void show()
Step 4: Create a class as derived.
Step 5: Create a function
void show()
Step 6: Display the current status.
Step 7: End of the program.
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"\n display base";
}
virtual void show()
{
cout<<"\n show base";
}
};
class derived:public base
{
public:
void display()
{
cout<<"\n display derived";
}
void show()
{
cout<<"\n show derived";
}
};
void main()
{
base b;
derived d;
base *bptr;
clrscr();
cout<<"\n bptr points to base:";
bptr=&b;
bptr->display();
bptr->show();
Result:
Thus the C++ program to display the current status of a class using virtual function was
verified and executed successfully.
Aim:
To write a C++ program to find addition and subtraction of two numbers using virtual
class.
Algorithm:
Step 1: Start the program.
Step 2: Create a class as arithmetic.
Step 3: Create add and sub as virtual classes
Step 4: Add the two numbers in derived class add.
Step 5: Subtract the two numbers in derived class sub.
Step 6: create manipulate as derived class from sub classes add and sub.Display the
addition and subtraction of two numbers.
Step 7: End of the program.
};
class sub:virtual public arithmatic
{
protected:
int diff;
public:
void diff_ab()
{
diff=a-b;
}
void disp_diff()
{
cout<<"\n
cout<<"\n
cout<<"\n
cout<<"\n
}
};
class manipulate:public add, public sub
{
public:
void get()
{
get_ab();
}
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