Sie sind auf Seite 1von 26

1.

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

4.OVERLOADING UNARY OPERATORS (NEW & DELETE):


#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#define TABLE_SPACE_SIZE 32
class test
{
float i;
public:
static char *tablespace;
static char *currptr;
static char *oldptr;
static int first_time;
test()
{
i=0;
cout<<"\nConstructor is called";
}
~test()
{
cout<<"\nDestructor is called";
}
void *operator new(size_t size);
void operator delete(void *p,size_t size);
};
char *test::currptr;
char *test::oldptr;
char *test::tablespace;
int test::first_time=1;
void *test::operator new(size_t size)
{
if(test::first_time)
{
test::tablespace=(char*)::new char[TABLE_SPACE_SIZE];
test::currptr=test::tablespace;
printf("\nTablespace pointer is %u",test::tablespace);
printf("\ncurrent pointer is %u",test::currptr);
printf("\nold pointer is %u",test::oldptr);
getch();
test::first_time=0;
}
if((test::currptr+size)>(test::tablespace+TABLE_SPACE_SIZE))
{
cout<<"\nMemory allocation failure";
getchar();
exit(0);
}
test::oldptr=test::currptr;
test::currptr=test::currptr+size;
cout<<"\nMemory allocated";
printf("\nCurrent pointer is %u",test::currptr);
printf("\nold pointer is %u",test::oldptr);
getch();
return test::oldptr;

}
void test::operator delete(void *p,size_t size)
{
::delete p,size;
}
void main()
{clrscr();
while(1)
new test;
}
Output:

Tablespace pointer is 4463920


current pointer is 4463920
old pointer is 0
Current pointer is 4463924
old pointer is 4463920
Current pointer is 4463928
old pointer is 4463924
Current pointer is 4463932
old pointer is 4463928
Current pointer is 4463936
old pointer is 4463932
Current pointer is 4463940
old pointer is 4463936
Current pointer is 4463944
old pointer is 4463940
Current pointer is 4463948
old pointer is 4463944
Current pointer is 4463952
old pointer is 4463948
Memory allocated
Constructor is called
Memory allocated
Constructor is called
Memory allocated
Constructor is called
Memory allocated
Constructor is called
Memory allocated
Constructor is called
Memory allocated
Constructor is called
Memory allocated
Constructor is called
Memory allocated
Constructor is called
Memory allocation failure

3.DYNAMIC MEMORY ALLOCATION:


#include<iostream.h>
#include<conio.h>
class matrix
{
int **p;
int d1,d2;
public:
matrix(){}
matrix(int x,int y);
void get_element(int i,int j,int value)
{
p[i][j]=value;
}
int put_element(int i,int j)
{
return p[i][j];
}
matrix(matrix &x)
{
cout<<"\nInvoking constructor...\n";
d1=x.d1;
d2=x.d2;
for(int i=0;i<d1;i++)
for(int j=0;j<d2;j++)
p[i][j]=x.p[i][j];
}
};
matrix ::matrix(int x,int y)
{
d1=x;d2=y;
p=new int *[d1];
for(int i=0;i<d2;i++)
p[i]=new int [d2];
}
void main()
{
int m,n;
clrscr();
cout<<"\nEnter the order of matrix";
cin>>m>>n;
matrix A(m,n);
cout<<"\nEnter the elements row by row";
int i,j,value;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
cin>>value;
A.get_element(i,j,value);
}
matrix B(A);
matrix C=B;
matrix D;
D=C;
cout<<"\nThe elements of the matrix A...\n";

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

Enter the size of the matrix : 3 3


Enter the elements row by row :
1
2
3
4
5
6
7
8
9
1 2 3 4 5 6 7 8 9

6.FUNCTION TEMPLATES(BUBBLE SORT):


#include<iostream.h>
#include<conio.h>
int i,j;
class emp
{
int sal;
char *name;
char *dept;
public:
emp(int a,char *b,char *c)
{ sal=a; name=b; dept=c; }
int operator<(emp&o)
{
if(sal<o.sal)
return 1;
else
return 0;
}
void get_det(int i)
{
cout<<"\nEmployee"<<i+1<<":";
cin>>sal>>name>>dept;
}
friend ostream& operator<<(ostream &t,emp &d);
};
ostream& operator<<(ostream &t,emp &d)
{
t<<d.sal<<"\t";
t<<d.name<<"\t";
t<<d.dept<<"\t";
return t;
}
template <class t>
void gen_bub_sort(t a[])
{
for(i=0;i<5;i++)
{ for(j=i+1;j<5;j++)
{
if(a[j]<a[i])
{
t temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}
void main()
{
int a[]={3,2,6,1,4};
float b[]={3.5,2.6,6.2,1.1,0.4};
char c[]="zbcat";
clrscr();

cout<<"\nSorting the employees 2_a_cse, 5_b_cse, 4_c_eie, 1_d_set,


3_e_fet\n\n";
emp
e[5]={emp(2,"a","cse"),emp(5,"b","cse"),emp(4,"c","eie"),emp(1,"d","set"
),emp(3,"e","fet")};
/* cout<<"\nEnter the details of the employees!";
for(i=0;i<5;i++)
{ e[i].get_det(i); }
*/ gen_bub_sort (e);
for(i=0;i<5;i++)
{ cout<<e[i]; cout<<"\n"; }
cout<<"\nSorting the integers 3..2..6..1..4...\n\n";
gen_bub_sort (a);
for(i=0;i<5;i++)
cout<<a[i]<<"\t";
cout<<"\n";
cout<<"\nSorting the Numbers 3.5\t2.6\t6.2\t1.1\t0.4\n\n";
gen_bub_sort (b);
for(i=0;i<5;i++)
cout<<b[i]<<"\t";
cout<<"\n";
cout<<"\nSorting the characters z..b..c..a..t...\n\n";
gen_bub_sort (c);
for(i=0;i<5;i++)
cout<<c[i]<<"\t";
cout<<"\n";
getch();

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

Sorting the integers 3..2..6..1..4...


1

Sorting the Numbers 3.5 2.6

6.2

0.4

6.2

1.1

2.6

3.5

Sorting the characters z..b..c..a..t...


a

1.1

0.4

7.CLASS TEMPLATES(LINKED LISTS):


#include<iostream.h>
#include<conio.h>
template <class t>
class linked_list
{
t a[10];
t val;
int p;
public:
linked_list(){p=0;}
void insertion()
{
cout<<"\nEnter the value:";
cin>>val;
a[p]=val;
p++;
cout<<"\nElement inserted\n";
}
void deletion()
{
int i;
cout<<"\nEnter the value:";
cin>>val;
for(i=0;i<p;i++)
{ if(a[i]==val)
break;
}
if(a[i]!=val)
{ cout<<"\nElement to be deleted is not found";
}
else
{
for(i=i;i<p;i++)
{ a[i]=a[i+1]; }
p--;
cout<<"\nElement deleted";
}
}
int find()
{
cout<<"\nEnter the value";
cin>>val;
for(int i=0;i<p;i++)
{
if(val==a[i])
break;
}
return (val==a[i]);
}
void display()
{if(p!=0)
{ cout<<"\n Values are...";
for(int i=0;i<p;i++)

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

Enter the choice1.Insert


2.Delete
3.Find
4.Display
Enter your choice
1
Enter the value:25
Element inserted
Enter the choice
1.Insert
2.Delete
3.Find
4.Display
Enter your choice
4
Values are...23
Enter the choice
1.Insert
2.Delete
3.Find
4.Display
Enter your choice
2
Enter the value:23

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

Enter the operation to be performed...


1.push
2.pop
3.Display
Enter your choice:2
Element Deleted
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:3
The elements in the stack are..
24
23
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:1
Stack Overflow
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:2
Element Deleted
Enter the operation to be performed...
1.push
2.pop
3.Display
Enter your choice:3
The elements in the stack are..
23

5.OVERLOADING BINARY OPERATOR(ASSIGNMENT)


#include<iostream.h>
#include<conio.h>
#include<string.h>
class item
{
int itemno;
char *itemname;
public:
item()
{
itemno=0;
itemname="";
}
item(int tempitemno,char *tempitemname)
{
itemno=tempitemno;
itemname=tempitemname;
}
void showdetails()
{
cout<<itemno<<"\n";
cout<<itemname<<"\n";
}
};
class customer
{
int custno;
char *custname;
char *custaddress;
item *itemsinterested;
int totalitems;
public:
customer()
{
custno=0;
itemsinterested=0;
totalitems=0;
}
~customer();
void operator=(customer &custref)
{
custno=custref.custno;
custname=custref.custname;
custaddress=custref.custaddress;

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

customer steve(1,"stevewaugh","australia",itemarray +2,6);


steve.showdetails();
customer mark(2,"mark waugh","australia",itemarray +2,3);
mark.showdetails();
customer bechkam;
bechkam=customer(3,"David Bechkam","England",itemarray,5);
bechkam.showdetails();
getch();
}
OUTPUT
Parameterized constructor is called forstevewaugh
1
stevewaugh
australia
stevewaughis interested in following items
5
napkins
6
toys
10
banana
9
pen
1
pencil
2
rubber
Parameterized constructor is called formark waugh
2
mark waugh
australia
mark waughis interested in following items
5
napkins
6
toys
10
banana
Parameterized constructor is called forDavid Bechkam
Destructor called for David Bechkam
3
David Bechkam England
David Bechkam is interested in following items
3
sandwitch
4
paperbags
5
napkins
6
Toys 1

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

10.RTTI(Run time type information)


#include<iostream.h>
#include<conio.h>
#include<typeinfo.h>
class shape
{
int ls,fc;
public:
virtual void draw()
{ cout<<"Shape is drawn\n"; }
};
class circle:public shape
{
int r,cpx,cpy;
public:
void draw()
{ cout<<"Circle is drawn\n"; }
};
class dot:public shape
{
int density;
public:
void some_other_func()
{}
};
class rectangle:public shape
{
int ltx,lty,rbx,rby;
public:
void some_other_func()
{}
};
void main()
{
shape s,*ps;
circle r;
r.draw();
s.draw();
ps=&r;
cout<<pointer is pointing to <<"\n"<<typeid(*ps).name();

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

Das könnte Ihnen auch gefallen