Iend Functions:: #Include #Include Int Class Class Int Public Void Int
Iend Functions:: #Include #Include Int Class Class Int Public Void Int
Iend Functions:: #Include #Include Int Class Class Int Public Void Int
FRIEND FUNCTIONS:
#include<iostream.h>
#include<conio.h>
int i,j,c[5];
class vector;
class matrix
{
int m[5][5];
public:
void getdata(int n)
{
cout<<"\nEnter the "<<n*n<<" elements of the matrix:\n";
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>m[i][j];
}
void display(int n)
{
cout<<"\nThe elements in the matrix are..\n";
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
cout<<m[i][j]<<"\t";
cout<<"\n";
}
}
friend void mul(matrix,vector);
}M;
class vector
{
int v[5];
public:
void getdata(int n)
{
cout<<"\nEnter the "<<n<<" elements of the vector:\n";
for(i=0;i<n;i++)
cin>>v[i];
}
void display(int n)
{
cout<<"\nThe elements in the vector are..\n";
for(i=0;i<n;i++)
cout<<v[i]<<"\n";
}
friend void mul(matrix,vector);
}V;
void mul(matrix M,vector V)
{
cout<<"\nThe product of the matrix and the vector is :\n";
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i]=M.m[i][j]*V.v[j]+c[i];
for(i=0;i<3;i++)
cout<<c[i]<<"\t";
}
void main()
int x;
clrscr();
cout<<"\nEnter the order of matrix:";
cin>>x;
M.getdata(x);
V.getdata(x);
M.display(x);
V.display(x);
mul(M,V);
getch();
Output:
Enter the order of matrix:2
Enter the 4 elements of the matrix:
23
45
Enter the 2 elements of the vector:
3
1
The elements in the matrix are..
2
3
4
5
The elements in the vector are..
3
1
The product of the matrix and the vector is :
9
17
2.TYPE CONVERSION:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class complex
{
double r,i;
public:
void print()
{
cout<<"\n The real and imaginary parts complex are:";
cout<<r<<"\t"<<i;
}
complex(){r=0;i=0;}
complex(double a,double b){r=a;i=b;}
friend complex operator +(complex,complex);
friend complex operator -(complex,complex);
complex operator *(complex);
complex operator /(complex);
operator double();
};
complex operator +(complex c1,complex c2)
{
complex temp;
temp.r=c1.r+c2.r;
temp.i=c1.i+c2.i;
return temp;
}
complex operator -(complex c1,complex c2)
{
complex temp;
temp.r=c1.r-c2.r;
temp.i=c1.i-c2.i;
return temp;
}
complex complex::operator *(complex c)
{
complex temp;
temp.r=(r*c.r)-(i*c.i);
temp.i=(r*c.i)+(i*c.r);
return temp;
}
complex complex::operator /(complex c)
{
complex temp;
float g;
g=(c.r*c.r)+(c.i*c.i);
temp.r=((r*c.r)+(i*c.i))/g;
temp.i=((r*c.i)-(i*c.r))/g;
return temp;
}
complex::operator double()
{
double ans=0;
ans=(r*r)+(i*i);
return sqrt(ans);
}
void main()
{
double a,b,c,d,mag,ans=0;
cout<<"Enter the real and imaginary part:";
cin>>a>>b;
complex c1(a,b);
cout<<"Enter the real and imaginary of second complex";
cin>>c>>d;
complex c2;
c2=complex(c,d);
complex c3,c4,c5,c6;
c3=c1+c2;
c4=c1-c2;
c5=c1/c2;
c6=c1*c2;
c3.print();
c4.print();
c5.print();
c6.print();
mag=c1;
cout<<"\nThe magnitude of c1 is"<<mag;
mag=c2;
cout<<"\nThe magnitude of c2 is"<<mag;
getch();
}
OUTPUT
Enter the real and imaginary part: 2 3
Enter the real and imaginary of second complex 2 6
The real and imaginary parts complex for addition are: 4 9
The real and imaginary parts complex for subtraction are: -2 -2
The real and imaginary parts complex for multiplication are: -6 10
The real and imaginary parts complex for division are:-1 3
The magnitude of c1 is 0.560976
The magnitude of c2 is 0.04878
}
void test::operator delete(void *p,size_t size)
{
::delete p,size;
}
void main()
{clrscr();
while(1)
new test;
}
Output:
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cout<<A.put_element(i,j)<<"\t";
cout<<"\n";
}
cout<<"\nThe elements of the matrix B...\n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cout<<B.put_element(i,j)<<"\t";
cout<<"\n"; }
cout<<"\nThe elements of the matrix C...\n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cout<<C.put_element(i,j)<<"\t";
cout<<"\n";
}
cout<<"\nThe elements of the matrix D...\n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
cout<<D.put_element(i,j)<<"\t";
cout<<"\n";
}
getch();
OUTPUT
Output:
Sorting the employees 2_a_cse, 5_b_cse, 4_c_eie, 1_d_set, 3_e_fet
1
2
3
4
5
d
a
e
c
b
set
cse
fet
eie
cse
6.2
0.4
6.2
1.1
2.6
3.5
1.1
0.4
cout<<a[i]<<"\t";
}
else
cout<<"\nThere are no elements in the list !\n";
}
};
void main()
{
int ch,op,x;
clrscr();
linked_list <int> n1;
linked_list <char> n2;
linked_list <float> n3;
cout<<"\nEnter the operation to be performed...\n1.Using
integer\n2.Using Character\n3.Using Float\n";
cout<<"\nEnter your choice:";
cin>>op;
if(op==1)
{
do
{
cout<<"\nEnter the
choice\n1.Insert\n2.Delete\n3.Find\n4.Display\nEnter your choice\n";
cin>>ch;
switch(ch)
{
case 1:n1.insertion();break;
case 2:n1.deletion();break;
case 3:x=n1.find();
if(x==1)
cout<<"\nElement is present";
else
cout<<"\nElement is not found";
break;
case 4:n1.display();break;
}
}while(ch<5);
}
else if(op==2)
{
do
{
cout<<"\nEnter the
choice\n1.Insert\n2.Delete\n3.Find\n4.Display\nEnter your choice\n";
cin>>ch;
switch(ch)
{
case 1:n2.insertion();break;
case 2:n2.deletion();break;
case 3:x=n2.find();
if(x==1)
cout<<"\nElement is present";
else
cout<<"\nElement is not found";
break;
case 4:n2.display();break;
}
}while(ch<5);
else if(op==3)
{
do
{
cout<<"\nEnter the
choice\n1.Insert\n2.Delete\n3.Find\n4.Display\nEnter your choice\n";
cin>>ch;
switch(ch)
{
case 1:n3.insertion();break;
case 2:n3.deletion();break;
case 3:x=n3.find();
if(x==1)
cout<<"\nElement is present";
else
cout<<"\nElement is not found";
break;
case 4:n3.display();break;
}
}while(ch<5);
}
getch();
}
Output:
Enter the operation to be performed...
1.Using integer
2.Using Character
3.Using Float
Enter your choice:1
Enter the choice
1.Insert
2.Delete
3.Find
4.Display
Enter your choice
1
Enter the value:23
Element inserted
Enter the choice
1.Insert
2.Delete
3.Find
4.Display
Enter your choice
1
Enter the value:24
Element inserted
24
Element deleted
Enter the choice
1.Insert
2.Delete
3.Find
4.Display
Enter your choice
4
Values are...24
Enter the choice
1.Insert
2.Delete
3.Find
4.Display
Enter your choice
3
Enter the value25
Element is present
Enter the choice
1.Insert
2.Delete
3.Find
4.Display
Enter your choice
4
Values are...24
25
25
8.EXCEPTION HANDLING:
#include<iostream.h>
#include<conio.h>
int i,n,val;
class myexception
{
char *msg;
public:
myexception(char *ptrmsg)
{ msg=ptrmsg; }
void error_msg()
{
cout<<msg<<"\n";
}
};
class stack
{
int stkptr;
int starr[10];
public:
stack()
{
stkptr=-1;
}
void push(int n);
void pop();
void display()
{
for(i=stkptr;i>=0;i--)
{ cout<<starr[i]<<"\t"; }
}
};
void stack:: push(int n)
{
try
{
if(stkptr==n-1)
{ myexception m("\nStack Overflow");
throw m;
}
else
{ cout<<"\nEnter the value:";
cin>>val;
starr[++stkptr]=val;
n++;
cout<<"\nElement inserted!";
}
}
catch(myexception m)
{ m.error_msg(); }
}
void stack:: pop()
{
try
{
if(stkptr<0)
{myexception m1("\nStack Underflow");throw m1;}
else
{ stkptr--;n--;
cout<<"\nElement Deleted";
}
}
catch(myexception m1)
{ m1.error_msg(); }
}
void main()
{
int op;
stack s;
cout<<"\nEnter the size of the stack:";
cin>>n;
do{
cout<<"\nEnter the operation to be performed...\n1.push\n2.pop\n3.Display\n";
cout<<"\nEnter your choice:";
cin>>op;
switch(op)
{
case 1:s.push(n);break;
case 2:s.pop();break;
case 3:cout<<"\nThe elements in the stack are..\n";
s.display();
}
}while(op<=3);
getch();
}
Output:
Enter the size of the stack:3
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:1
Enter the value:23
Element inserted!
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:1
Enter the value:24
Element inserted!
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:1
Enter the value:25
Element inserted!
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:3
The elements in the stack are..
25
24
23
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:1
Stack Overflow
itemsinterested=0;
itemsinterested=new item[custref.totalitems];
for(int i=0;i<custref.totalitems;++i)
{
itemsinterested[i]=custref.itemsinterested[i];
}
totalitems=custref.totalitems;
}
customer(int tempcustno,char *tempcustname,char *tempcustaddress,item
*tempitemsinterested,int temptotalitems)
{
custno=tempcustno;
custname=tempcustname;
custaddress=tempcustaddress;
itemsinterested=new item[temptotalitems];
for(int i=0;i<temptotalitems;++i)
{
itemsinterested[i]=tempitemsinterested[i];
}
totalitems=temptotalitems;
cout<<"Parameterized constructor is called for"<<custname<<"\n";
}
void showdetails()
{
cout<<custno<<"\t";
cout<<custname<<"\t";
cout<<custaddress<<"\n";
cout<<custname<<"is interested in following items\n";
for(int i=0;i<totalitems;i++)
{
itemsinterested[i].showdetails();
}
}
};
customer::~customer()
{
cout<<"\nDestructor called for"<<"\t"<<custname<<"\n";
delete[]itemsinterested;
}
void main()
{
item itemarray[]=
{
item(3,"sandwitch"),item(4,"paperbags"),item(5,"napkins"),item(6,"toys"),item(10,"bana
na"),item(9,"pen"),item(1,"pencil"),item(2,"rubber")
};
9.VIRTUAL FUNCTIONS
#include<iostream.h>
#include<conio.h>
#include<time.h>
#include<stdlib.h>
class figure;
class point
{
int x,y;
public:
point(int tempx=0,int tempy=0)
{
x=tempx;
y=tempy;
}
int getx()const
{
return x;
}
int gety()const
{
return y;
}
friend ostream &operator<<(ostream &tempout,point &temppoint);
};
ostream &operator<<(ostream &tempout,point &temppoint)
{
tempout<<"("<<temppoint.getx()<<";"<<temppoint.gety()<<")";
return tempout;
}
class shape
{
point position;
int colour;
virtual void draw()
{
cout<<"shape drawn";
}
friend figure;
};
class square:public shape
{
point leftbottom;
public:
square(point templeftbottom)
{
leftbottom=templeftbottom;
}
void draw()
{
cout<<"square is drawn at"<<leftbottom;
}
};
class triangle:public shape
{
point avertex,bvertex,cvertex;
public:
triangle(point tempavertex,point tempbvertex,point tempcvertex)
{
avertex=tempavertex;
bvertex=tempbvertex;
cvertex=tempcvertex;
}
void draw()
{
cout<<"\n"<<"triangle is drawn at"<<avertex<<""<<bvertex<<""<<cvertex<<"\n";
}
};
class circle:public shape
{
point center;
public:
circle(point tempcenter)
{
center=tempcenter;
}
void draw()
{
cout<<"\n"<<"circle is drawn at"<<center;
}
};
class figure
{
int choice;
shape *images[10];
public:
figure()
{
srand((unsigned)time(NULL));
for(int i=0;i<10;++i)
{
int randomvalues[6];
for(int j=0;j<6;++j)
{
randomvalues[j]=rand()%50;
}
point position1(randomvalues[0],randomvalues[1]);
point position2(randomvalues[2],randomvalues[3]);
point position3(randomvalues[4],randomvalues[5]);
choice=rand()%3;
switch(choice)
{
case 0:
images[i]=new square(position1);
break;
case 1:
images[i]=new triangle(position1,position2,position3);
break;
case 2:
images[i]=new circle(position1);
break;
default:
cout<<choice<<"is a wrong choice";
}}}
void draw()
{
for(int i=0;i<10;i++)
{
images[i]->draw();
}}};
void main()
{
figure o;
o.draw();
}
OUTPUT
square is drawn at(45;40)
circle is drawn at(36;13)
triangle is drawn at(36;28)(19;32)(11;38)
triangle is drawn at(29;32)(10;9)(42;40)
circle is drawn at(6;44)
circle is drawn at(27;28)
circle is drawn at(36;27)
circle is drawn at(20;24)
triangle is drawn at(2;35)(25;3)(7;10)
triangle is drawn at(35;23)(7;26)(19;1)
Press any key to continue
rectangle b;
ps=&b;
cout<<pointer is pointing to<<"\n"<<typeid(*ps).name();
ps=new dot;
cout<<pointer is pointing to<<"\n"<<typeid(*ps).name();
}
OUTPUT
Shape is drawn
Circle is drawn
Pointer is pointing to class circle
Pointer is pointing to class rectangle
Pointer is pointing to class dot