CPP Programs
CPP Programs
// Stack.cpp
#include<iostream.h>
#include<process.h>
#include<conio.h>
#define size 30
class Stack
{ int top;
int s[size];
public:
Stack()
{ top=-1;
}
int stackEmpty(void);
int stackFull(void);
void push(int);
int pop(void);
void disp(void);
};
int Stack::stackEmpty(void)
{
if(top==-1)
return 1;
else
return 0;
}
int Stack::stackFull(void)
{
if(top==size)
return 1;
else
return 0;
}
1
void Stack::push(int item)
{
if(!stackFull())
{
s[++top] = item;
cout<<"Item is inserted"<<endl;
}
else
{
cout<<endl<<"Stack is full"<<endl;
}
}
int Stack::pop(void)
{
int t;
if(!stackEmpty())
{
cout<<"Popped item is\n";
t = s[top--];
}
else
{
cout<<endl<<"Stack is Empty\n"<<endl;
cout<<"Number of elements remaining in stack is ";
}
return t;
}
void Stack::disp(void)
{
if(top==-1)
cout<<"No items are there in the stack";
else
{
cout<<"\nItems are\n";
for(int j=0;j<=top;j++)
cout<<s[j]<<" ";
cout<<endl;
}
}
2
void main()
{
Stack ob;
char ch='y';
int op,i;
clrscr();
do{
cout<<endl<<"1.Push"<<endl;
cout<<"2.Pop";
cout<<endl<<"3.Display Items"<<endl;
cout<<"Enter Your Choice\n";
cin>>op;
switch(op)
{
case 1: cout<<"\nEnter the item\n";
cin>>i;
ob.push(i);
break;
case 2: cout<<ob.pop()<<endl;
break;
case 3: ob.disp();
break;
default: cout<<"\nWrong Option!"<<endl;
}
cout<<"\nDo you what to continue(y/n)\n";
cin>>ch;
}while(ch=='y');
}
3
Output
1.Push
2.Pop
3.Display Items
Enter Your Choice
1
1.Push
2.Pop
3.Display Items
Enter Your Choice
1
1.Push
2.Pop
3.Display Items
Enter Your Choice
3
Items are
11 22
4
Do you what to continue(y/n)
y
1.Push
2.Pop
3.Display Items
Enter Your Choice
2
Popped item is
22
5
//2. Program performing complex number addition using
//friend functions
//Friend.cpp
#include<iostream.h>
class complex
{
float x;
float y;
public:
void input(float real,float imag)
{
x=real;
y=imag;
}
friend complex sum(complex,complex);
void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return(c3);
}
void complex::show(complex c)
{
cout<<c.x<<"+i"<<c.y<<"\n";
}
int main()
{
complex A,B,C;
A.input(3.1,5.65);
B.input(2.75,1.2);
C=sum(A,B);
cout<<"A=";A.show(A);
cout<<"B=";B.show(B);
cout<<"C=";C.show(C);
return 0;
6
}
Output
A=3.1+i5.65
B=2.75+i1.2
C=5.85+i6.85
A=3.1+i5.65
B=2.75+i1.2
C=5.85+i6.85
7
//3. Program for complex number addition using operator
//overloading
// comadd.cpp
#include<iostream.h>
class complex
{
float x;
float y;
public:
complex(){}
complex(float real,float imag)
{x=real;y=imag;}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+i"<<y<<"\n";
}
int main()
{
complex C1,C2,C3;
C1=complex(2.5,3.5);
C2=complex(1.6,2.7);
C3=C1+C2;
cout<<"C1=";C1.display();
cout<<"C2=";C2.display();
cout<<"C3=";C3.display();
return 0;
8
}
Output
A=3.1+i5.65
B=2.75+i1.2
C=5.85+i6.85
A=3.1+i5.65
B=2.75+i1.2
C=5.85+i6.85
C1=2.5+i3.5
C2=1.6+i2.7
C3=4.1+i6.2
C1=2.5+i3.5
C2=1.6+i2.7
C3=4.1+i6.2
9
//4. Program to perform string operations by overloading
//operators
// stringoo.cpp
#include<string.h>
#include<iostream.h>
class string
{
char *p;
int len;
public:
string() {len=0;p=0;}
string(const char *s);
string(const string &s);
~string(){delete p;}
10
strcat(temp.p,t.p);
return(temp);
}
int operator<=(const string &s,const string &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m<=n)
return(1);
else
return(0);
}
void show(const string s)
{
cout<<s.p;
}
int main()
{
string s1="New";
string s2="York";
string s3="Delhi";
string t1,t2,t3;
t1=s1;
t2=s2;
t3=s1+s3;
cout<<"\nt1= ";show(t1);
cout<<"\nt2= ";show(t2);
cout<<"\n";
cout<<"\nt3= ";show(t3);
cout<<"\n\n";
if(t1<=t3)
{
show(t1);
cout<<" smaller than ";
show(t3);
cout<<"\n";
}
return 0;
}
11
Output
A=3.1+i5.65
B=2.75+i1.2
C=5.85+i6.85
A=3.1+i5.65
B=2.75+i1.2
C=5.85+i6.85
C1=2.5+i3.5
C2=1.6+i2.7
C3=4.1+i6.2
C1=2.5+i3.5
C2=1.6+i2.7
C3=4.1+i6.2
t1= New
t2= York
t3= NewDelhi
t1= New
t2= York
t3= NewDelhi
12
//5. Program on hierarchical inheritance showing public,
// private and protected inheritances.
//hirarcal.cpp
// Hirarchical inheritence
#include<iostream.h>
#include<conio.h>
class account
{
private:
int accno;
char name[10];
public:
void getaccno()
{
cout << "\nEnter your account no \n";
cin >> accno;
}
void getname()
{
cout << "\nEnter your account name\n";
cin >> name;
}
void getdetails()
{
cout << "\nAccount no and name are \n" << accno << " "<< name;
}
};
class savings:public account
{
public:
void display()
{
cout << "\nYour mode is savings \n";
13
}
};
class fixed: private account
{
public: void display();
};
void fixed::display()
{
cout << "\nYour mode is fixed \n";
getaccno();
getname();
getdetails();
}
14
p.getname();
p.getdetails();
}
else if(n == 2)
{
fixed p;
p.display();
}
else
{
current p;
p.display();
}
getch();
}
15
Output
Enter your account mode
Savings(1)
Fixed(2)
Current(3)
2
16
//6. Program for computation of student’s result using hybrid
//inheritance
//hybrid.cpp
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<"Roll No: "<<roll_number<<"\n";
}
};
class test:public student
{
protected:
float part1,part2;
public:
void get_marks(float x,float y)
{
part1=x;
part2=y;
}
void put_marks(void)
{
cout<<"Marks obtained: "<<"\n"<<"Part1=
"<<part1<<"\n"<<"Part2= "<<part2<<"\n";
}
};
class sports
17
{
protected:
float score;
public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<"Sports wt: "<<score<<"\n\n";
}
};
class result:public test,public sports
{
float total;
public:
void display(void);
};
void result::display(void)
{
total=part1+part2+score;
put_number();
put_marks();
put_score();
cout<<"Total Score: "<<total<<"\n";
}
int main()
{
clrscr();
result student_1;
student_1.get_number(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
return 0;
}
18
Output
19
//7. Program implementing bubble-sort using templates
//bubble.cpp
#include<iostream.h>
template<class T>
void bubble(T a[],int n)
{
for(int i=0;i<n-1;i++)
for(int j=n-1;i<j;j--)
if(a[j]<a[j-1])
{
swap(a[j],a[j-1]);
}
}
template<class x>
void swap(x &a,x &b)
{
x temp=a;
a=b;
b=temp;
}
int main()
{
int x[5]={10,50,40,30,20};
float y[5]={1.1,5.5,3.3,4.4,2.2};
bubble(x,5);
bubble(y,5);
cout<<"sorted x-array:";
for(int i=0;i<5;i++)
cout<<x[i]<<" ";
cout<<endl;
cout<<"sorted y-array:";
for(int j=0;j<5;j++)
cout<<y[j]<<" ";
cout<<endl;
return 0;
}
20
Output
21
//8. Program on virtual functions
//virtual.cpp
#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";}
};
int main()
{
base B;
derived D;
base *bptr;
cout<<"\n bptr points to base \n";
bptr=&B;
bptr->display();
bptr->show();
cout<<"\n \n bptr points to derived \n";
bptr=&D;
bptr->display();
bptr->show();
return 0;
}
22
Output
Roll No: 1234
Marks obtained:
Part1= 27.5
Part2= 33
Sports wt: 6
display base
show base
display base
show derived
23
// 9. Program for handling PushOnFull and PopOnEmpty
// Exceptions for a Stack
//StackExce.cpp
#include<iostream.h>
#include<process.h>
#include<conio.h>
#define size 10
class StackExce
{ int top;
int s[size];
public:
StackExce()
{ top=-1;
}
int stackEmpty(void);
int stackFull(void);
void push(int);
int pop(void);
void disp(void);
};
int StackExce::stackEmpty(void)
{
if(top==-1)
return 1;
else
return 0;
}
int StackExce::stackFull(void)
{
if(top==size-1)
return 1;
24
else
return 0;
}
try
{
int t=0;
if(!stackEmpty())
{
cout<<"Popped item is\n";
t = s[top--];
}
25
else
{
t=0;
throw(t);
}
return t;
}
catch(int x)
{
cout<<endl<<"Stack is Empty\n"<<endl;
exit(0);
}
void StackExce::disp(void)
{
if(top==-1)
cout<<"No items are there in the stack";
else
{
cout<<"\nItems are\n";
for(int j=0;j<=top;j++)
cout<<s[j]<<" ";
cout<<endl;
}
}
void main()
{
StackExce ob;
char ch='y';
int op,i;
do{
26
cout<<endl<<"1.Push"<<endl;
cout<<"2.Pop";
cout<<endl<<"3.Display Items"<<endl;
cout<<"Enter Your Choice\n";
cin>>op;
switch(op)
{
case 1:
cout<<"\nEnter the item\n";
cin>>i;
ob.push(i);
break;
case 2:
cout<<ob.pop()<<endl;
break;
case 3:
ob.disp();
break;
default:
cout<<"\nWrong Option!"<<endl;
}
cout<<"\nDo you what to continue(y/n)\n";
cin>>ch;
}while(ch=='y');
}
27
Output
1.Push
2.Pop
3.Display Items
Enter Your Choice
1
1.Push
2.Pop
3.Display Items
Enter Your Choice
1
1.Push
2.Pop
3.Display Items
Enter Your Choice
3
Items are
11 22
28
1.Push
2.Pop
3.Display Items
Enter Your Choice
2
Popped item is
22
29
// 10. Program for copying one file to another file using streams
// Filetest.cpp
#include <iostream.h>
#include <fstream.h>
#include<conio.h>
void main()
{
clrscr();
ofstream fout;
fout.open("country");
fout << "United States of America \n";
fout << "United Kingdom \n";
fout << "South Korea \n";
fout.close();
ifstream fin;
fin.open("country");
fout.open("cities");
fin.open("cities");
cout << "\n Contents of cities file \n";
while(fin)
{
fin.getline(line,N);
30
cout << line<<"\n";
}
fin.close();
getch();
}
31
Output
Contents of cities file
United States of America
United Kingdom
South Korea
32
// 11. Program for writing and reading a class object to a file
//readwrit.cpp
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
{
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
void INVENTORY::readdata(void)
{
cout<<"enter name: ";cin>>name;
cout<<"enter code: ";cin>>code;
cout<<"enter cost: ";cin>>cost;
}
void INVENTORY::writedata(void)
{
cout<<setiosflags(ios::left)
<<setw(10)<<name
<<setiosflags(ios::right)
<<setw(10)<<code
<<setprecision(2)
<<setw(10)<<cost
<<endl;
}
int main()
{
INVENTORY item[3];
fstream file;
file.open("STOCK.DAT",ios::in|ios::out);
cout<<"ENTER DETAILS FOR THREE ITEMS \n";
for(int i=0;i<3;i++)
{
33
item[i].readdata();
file.write((char*)&item[i],sizeof(item[i]));
}
file.seekg(0);
cout<<"\nOUTPUT\n\n";
for(i=0;i<3;i++)
{
file.read((char*)&item[i],sizeof(item[i]));
item[i].
writedata();
}
file.close();
return 0;
}
Output
ENTER DETAILS FOR THREE ITEMS
enter name: x
enter code: 001
enter cost: 100
enter name: y
enter code: 002
enter cost: 200
enter name: z
enter code: 003
enter cost: 300
OUTPUT
x 1 100
y 2 200
z 3 300
34