You are on page 1of 17

C++ Program for funtion overloading.

#include<iostream.h>
#include<conio.h>
int area(int);
int area(int,int);
float area(float,double=3.14);
float area(float,float,float=0.5);
void main()
{
int s,l,b;
float r,bas,h;
clrscr();
cout<<"\n\nAREA OF SQUARE CIRCLE TRIANGLE AND RECTANGLE\n\n";
cout<<"Enter Square,Length,Breadth :";
cin>>s>>l>>b;
cout<<"\nEnter Rec,Base,Height : ";
cin>>r>>bas>>h;
int sqr=area(s);
cout<<"\nThe Area of Square : "<<sqr;
int rect=area(l,b);
cout<<"\nThe Area of Rectangle : "<<rect;
float circle=area(r);
cout<<"\nThe Area of Circle : "<<circle;
float tri=area(bas,h);
cout<<"\nThe Area of Triangle : "<<tri;
getch();
}
int area(int a)
{
return(a*a);
}
int area(int x,int y)
{
return(x*y);
}
float area(float b,double c)
{
return(c*b*b);
}
float area(float a,float b,float c)
{
return(a*b*c);
}

C++ Program for Friend Functions.

#include<iostream.h>
#include<conio.h>
class Operand2; //forward declaration
class Operand1
{
int a;
public :
void get()
{
cout<<"\n\n\t\tEnter the value of Operand1 : ";
cin>>a;
}
friend void add(Operand1,Operand2);
};
class Operand2
{
int b;
public :
void get()
{
cout<<"\n\n\t\tEnter the value of Operand2 : ";
cin>>b;
}
friend void add(Operand1,Operand2);
};
void add(Operand1 op1, Operand2 op2)
{
/* friend fn to add two numbers*/
cout<<"\n\t\t"<<op1.a<<" + "<<op2.b<<" = "<<op1.a+op2.b;
}
void main()
{
Operand1 op1;
Operand2 op2;
clrscr();
cout<<"\n\n\t\tPROGRAM TO ADD TWO NUMBERS USNIG FRIEND FUNCTION\n\n ";
cout<<"\n\t\tINPUT\n\n";
op1.get();
op2.get();
cout<<"\n\n\t\tOUTPUT\n\n";
add(op1,op2);
getch();
}

C++ Program for add two complex no using friend function.

#include<iostream.h>
#include<conio.h>
class Cmplx1
{
int real,imagin;
public :
void get()
{
cout<<"\n\n\tENTER THE REAL PART : ";
cin>>real;
cout<<"\n\n\tENTER THE IMAGINARY PART : ";
cin>>imagin;
}
friend void sum(Cmplx1,Cmplx1);
};
void sum(Cmplx1 c1,Cmplx1 c2)
{
cout<<"\n\t\tRESULT : ";
cout<<"\n\n\t["<<c1.real<<" + i "<<c1.imagin;
cout<<" ] + [ "<<c2.real<<" + i "<<c2.imagin;
cout<<" ] = "<<c1.real+c2.real<<" + i "<<c1.imagin+c2.imagin;
}
void main()
{
Cmplx1 op1,op2;
clrscr();
cout<<"\n\n\tADDITION OF TWO COMPLEX NUMBERS USING FRIEND FUNCTIONS\n\n";
cout<<"\n\tINPUT\n\n\t\tOPERAND 1";
op1.get();
cout<<"\n\n\t\tOPERAND 2";
op2.get();
sum(op1,op2);
getch();
}

C++ Program for to read the content of the file sort.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char c[25],str[20][20],d[20];
int j,count;
j=count=0;
ifstream ifs("d:\\first1.txt");
ofstream sort("d:\sort.txt");
cout<<"\n\n\t\tFILE CONTENT BEFORE SORTING\n\n";
while(!ifs.eof())
{
ifs.getline(c,20);//READING FROM THE FILE
strcpy(str[count],c);
cout<<c<<endl;
if(count)
{
/*sorting*/
strcpy(str[count],c);
for(j=0;j<=count;j++)
{
if(strcmpi(str[count],str[j])<0)
{
strcpy(d,str[j]);
strcpy(str[j],str[count]);
strcpy(str[count],d);
}
}
}
count++;
}
cout<<"\n\nFILE CONTENT AFTER SORTING";
for(j=0;j<count;j++)
{
sort<<str[j]<<endl;//writing
cout<<"\n"<<str[j];
}
getch();
}

C++ Program for pure virtual function.

#include<iostream.h>
#include<conio.h>
class Rectangle
{
public:
float length,breadth;
virtual void get()=0;
virtual void calculate()=0;
};
class Area : public Rectangle
{
public:
void get()
{
/*Fn to get i/p*/
cout<<"\n\n\t\tEnter the Length of the Rectangle : ";
cin>>length;
cout<<"\n\n\t\tEnter the Breadth of the Rectangle : ";
cin>>breadth;
}
void calculate()
{
/*Fn to calc. area & perimeter*/
cout<<"\n\n\t\tArea of the Rectangle : "<<length*breadth;
cout<<"\n\n\t\tPerimeter of the Rectangle : "<<2*(length+breadth);
}
};
void main()
{
Rectangle *ptr;
Area obj;
clrscr();
cout<<"\n\n\tPROGRAM TO CALCULATE AREA & PERIMETER USING VIRTUAL FUNCTION\n\n";
ptr=&obj;
ptr->get();
ptr->calculate();
getch();
}

C++ Program for READING CONTENT FROM THE FILE.

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
void main()
{
clrscr();
char c[25];
ifstream ifs("h:\\first1.txt");
cout<<"\n\n\t\tFILE CONTENT\n\n";
while(!ifs.eof())
{
cout<<"\nPosition : "<<ifs.tellg();
ifs.getline(c,20);//READING FROM THE FILE
cout<<"\nPosition : "<<ifs.tellg()<<"\tcontent : ";
cout<<c<<endl;
}
getch();
}

C++ Program for Add two time variables using constructor destructor.

#include<iostream.h>
#include<conio.h>
class Time
{
int minutes,hours,a;
static int i;
public:
Time(int a)
{
this->a=hours=a;
this->a+=5;
minutes=i++;
cout<<"\nObj address : "<<this;
cout<<"\nAddress of i : "<<&i;
cout<<"\na= "<<this->a<<"\t\t"<<a;
getch();
}
~Time()
{
cout<<endl<<"\t\t"<<hours<<" : "<<minutes;
getch();
}
};
int Time ::i;
void main()
{
clrscr();
Time t3(10),t2(1);
}

C++ Program for set operations using operator overloading.

#include<iostream.h>
#include<conio.h>
class Set
{
int a[15],n;
public:
Set(){}
Set(int n)
{
this->n=n;
}
void get();
void sort();
void show();
void operator +(Set);
void operator -(Set);
void operator <(Set); //superset
void operator >(Set); //subset
};
void Set :: operator <(Set s2)
{
Set s3;
}
void Set::get()
{
cout<<"\n\nEnter the Set Values : ";
for(int i=0;i<n;i++)
cin>>a[i];
sort();
}
void Set ::sort()
{
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(a[i]>a[j])
{
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
void Set::show()
{
for(int i=0;i<n;i++,cout<<"\t")
cout<<a[i];
}
void Set::operator +(Set s2)
{
// int unions[30];
Set s3;
int i=0,j=0,k;
for(k=0;i<n && j<s2.n;k++)
{
if(a[i]==s2.a[j])
{
s3.a[k]=a[i++];
j++;
}
else if(a[i]<s2.a[j])
s3.a[k]=a[i++];
else
s3.a[k]=a[j++];
}
while(i<n)
s3.a[k++]=a[i++];
while(j<s2.n)
s3.a[k++]=s2.a[j++];
cout<<"\n\n\t\tA U B\n\n\t";
s3.n=k;
s3.show();
}
void Set :: operator -(Set s2)
{
Set s3;
int i=0,j=0,k;
for(k=0;i<n && j<s2.n;)
{
if(a[i]==s2.a[j])
{
s3.a[k++]=a[i];
j++;
i++;
}
if(a[i]<s2.a[j])
j++;
else
i++;
}
cout<<"\n\n\t\tA INTERSECTION B\n\n\t";
s3.n=k;
s3.show();
}
void main()
{
clrscr();
int n,n1;
cout<<"\n\n\t\tSET OPERATIONS\n\n";
cout<<"\n\tEnter No.of elements in Set A & B : ";
cin>>n>>n1;
Set s1(n),s2(n1),s3;
s1.get();
s2.get();
clrscr();
cout<<"\n\n\t\tSet A\n\n\t";
s1.show();
cout<<"\n\n\t\tSet B \n\n\t";
s2.show();
s1+s2; //union
s1-s2;//intersection
getch();
}

C++ Program for to implement circular queue operation using template function.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class Type>
class Queue
{
Type s[10];
int rear,front,n;
public:
Queue()
{
cout<<"\n\tEnter the Queue Size : ";
cin>>n;
rear=front=n-1;
}
void insert(Type elt)
{
if((rear+1)%n!=front)
{
rear=(rear+1)%n;
s[rear]=elt;
}
else
cout<<"\n\tQueue is full.Can't insert "<<elt<<endl;
}
void remove()
{
if(front==rear)
cout<<"\n\tQueue is empty.\n";
else
{
front=(front+1)%n;
cout<<"\n\tRemoved elt : "<<s[front];
}
}
void que_operation();
void display();
};
template<class Type>
void Queue<Type> :: display()
{
if(rear!=front)
{
cout<<"\n\t\tQueue Content :\n\n\t";
for(int i=(front+1)%n;;i=(i+1)%n)
{
cout<<s[i]<<"\t";
if(i==rear) break;
}
}
else
cout<<"\n\tEmpty Queue Can't Be Print\n";
}
template<class Type>
void Queue<Type> :: que_operation()
{
int choice=1,i;
Type elt;
while(choice>0 && choice<3)
{
cout<<"\n\n\t1.Insert\t2.Remove\tAny Key To Exit\n\tChoice : ";
cin>>choice;
switch(choice)
{
case 1 : //insert
cout<<"\n\tEnter the Elt to insert : ";
cin>>elt;
insert(elt);
display();
break;
case 2 : //remove
remove();
display();
break;
}
}
}
void main()
{
clrscr();
cout<<"\n\t\tQUEUE OPERATION USING TEMPLATE\n\n";
cout<<"\n\t INT\n";
Queue<int> que1;
cout<<"\n\t FLOAT\n";
Queue<float> que2;
int ch;
while(1)
{
cout<<"\n\t\t\tQUEUE OPERATION \n\n";
cout<<"\t1.INT QUEUE\t2.FLOAT QUEUE\tAny Key To Exit\n\tChoice : ";
cin>>ch;
switch(ch)
{
case 1 : //perform queue operation on int queue
que1.que_operation();
break;
case 2 : //float
que2.que_operation();
break;
default : exit(0);
}
}
}
C++ Program for to implement stack operation using template function.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
template<class Type>
class Stack
{
Type s[10];
int top,n;
public:
Stack()
{
top=-1;
cout<<"\n\tEnter the Stack Size : ";
cin>>n;
}
void push(Type elt)
{
if(top<n-1)
s[++top]=elt;
else
cout<<"\n\tstack is full.Can't insert "<<elt<<endl;
}
void pop()
{
if(top<0)
cout<<"\n\tstack is empty.\n";
else
cout<<"\n\tPoped elt : "<<s[top--];
}
void stk_operation();
};
template<class Type>
void Stack<Type> :: stk_operation()
{
int choice=1,i;
Type elt;
while(choice>0 && choice<3)
{
cout<<"\n\n\t1.PUSH\t2.POP\tAny Key To Exit\n\tChoice : ";
cin>>choice;
switch(choice)
{
case 1 : //push
cout<<"\n\tEnter the Elt to push : ";
cin>>elt;
push(elt);
cout<<"\n\t\tstack content :\n\n\t";
for(i=0;i<=top;i++)
cout<<s[i]<<"\t";
break;
case 2 : //pop
pop();
cout<<"\n\t\tstack content :\n\n\t";
for(i=0;i<=top;i++)
cout<<s[i]<<"\t";
break;
}
}
}
void main()
{
clrscr();
cout<<"\n\t\tSTACK OPERATION USING TEMPLATE\n\n";
cout<<"\n\t INT\n";
Stack<int> stk1;
cout<<"\n\t FLOAT\n";
Stack<float> stk2;
int ch;
while(1)
{
cout<<"\n\t\t\tSTACK OPERATION \n\n";
cout<<"\t1.INT STACK\t2.FLOAT STK\tAny Key To Exit\n\tChoice : ";
cin>>ch;
switch(ch)
{
case 1 : //perform stk operation on int stk
stk1.stk_operation();
break;
case 2 : //float
stk2.stk_operation();
break;
default : exit(0);
}
}
}

C++ Program for virtual function to find area perimeter of the rectangle.

#include<iostream.h>
#include<conio.h>
class Rectangle
{
public :
float length,breadth;
Rectangle(){}
Rectangle(float t,float t1)
{
length=t;
breadth=t1;
}
void get()
{
cout<<"\n\n\t\tEnter the Length of the Rectangle :";
cin>>length;
cout<<"\n\n\t\tEnter the Breadth of the Rectangle : ";
cin>>breadth;
}
virtual void calculate()
{ /* fn. to calculate perimeter*/
float p=2.0;
float k=length+breadth;
p=p*k;
cout<<"\n\n\tPERIMETER OF THE RECTANGLE : "<<p;
}
};
class Area : public Rectangle
{
public:
void calculate()
{
/*fn. to return area of the rectangle*/
cout<<"\n\n\tAREA OF THE RECTANGLE : "<<length*breadth;
}
};
void main()
{
clrscr();
cout<<"\n\n\tPROGRAM TO CALCULATE AREA & PERIMETER OF THE RECTANGLE\n\n";
Area area;//derived
Rectangle *ptr;//base
ptr=&area;
ptr->get();
ptr->calculate();
Rectangle rect(ptr->length,ptr->breadth);
ptr=&rect;
ptr->calculate();
getch();
}

C++ Program for TO SWAP USING TEMPLATE FUNCTION.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
template<class S>
void swap(S &a,S &b)
{
S t;
t=a;
a=b;
b=t;
}
void main()
{
int a,b;
float c,d;
char e,f;
char *str1="AKILA",*str2="KRISHNA";
clrscr();
cout<<"\n\n\t\tPROGRAM TO SWAP USING TEMPLATE FUNCTION\n\n";
cout<<"\n\n\tEnter the value for a & b <int>: ";
cin>>a>>b;
cout<<"\n\n\tEnter the Value for c & d <float>: ";
cin>>c>>d;
cout<<"\n\n\tEnter the Value for e & f <char> : ";
fflush(stdin);
cin>>e>>f;
clrscr();
cout<<"\n\n\t\tSWAPPING USING TEMPLATE FUNCTION\n";
cout<<"\n\n\t\tINTEGER SWAPPING";
cout<<"\n\n\t\tBefore Swap : a = "<<a<<"\tb = "<<b;
swap(a,b);
cout<<"\n\n\t\tAfter Swap : a = "<<a<<"\tb = "<<b;
cout<<"\n\n\t\tFLOAT SWAPPING";
cout<<"\n\n\t\tBefore Swap : c = "<<c<<"\td = "<<d;
swap(c,d);
cout<<"\n\t\tAfter Swap : c = "<<c<<"\td = "<<d;
cout<<"\n\n\t\tCHARACTER SWAPPING";
cout<<"\n\n\t\tBefore Swap : e = "<<e<<"\tf = "<<f;
swap(e,f);
cout<<"\n\t\tAfter Swap : e = "<<e<<"\tf = "<<f;
cout<<"\n\n\t\tSTRING SWAPPING";
cout<<"\n\n\t\tBefore Swap : str1 = "<<str1<<"\tstr2 = "<<str2;
swap(str1,str2);
cout<<"\n\t\tAfter Swap : str1 = "<<str1<<"\tstr2 = "<<str2;
getch();
}

You might also like