You are on page 1of 12

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()
{
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

1.h UNARY OPERATOR OVERLOADING

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


Ex no:1h// Unary Operator Overloading

#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:

Refer class note

1.j FRIEND FUNCTION

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


ex no:1j // Friend Function

#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 max(abc m,xyz n)


{
if(m.a >= n.b)
cout<<m.a;
else
cout<<n.b;
}

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

verified and executed successfully

1.k OVERLOAD ADDITION OPERATOR USING FRIEND FUNCTION

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.

Ex no:1k// overload addition operator Using friend functions For complex


numbers

#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);
};

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.

1.l VIRTUAL FUNCTION

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.


Ex no: l L // Virtual Function

#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();
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

verified and executed successfully.

1.m VIRTUAL CLASS

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.


ex no:1m// Virtual Class

#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;
}
};

class add:virtual public arithmatic


{
protected:
int sum;
public:
void sum_ab()
{
sum=a+b;
}

void disp_sum()
{
cout<<"\n Addition of two numbers:";
cout<<"\n A:"<<a;
cout<<"\n B:"<<b;
cout<<"\n Sum:"<<sum;
}
};

class sub:virtual public arithmatic


{
protected:
int diff;
public:
void diff_ab()
{
diff=a-b;
}

void disp_diff()
{
cout<<"\n Difference of two numbers:";
cout<<"\n A:"<<a;
cout<<"\n B:"<<b;
cout<<"\n Difference:"<<diff;
}
};

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 verified and executed successfully.

You might also like