C++ Lab Manual
C++ Lab Manual
PART-A
1. Write a C++ program to find the sum for the given variables using function with default
arguments.
#include "stdafx.h"
#include<iostream>
using namespace std;
int sum(int a=10,int b=20)
{
int c=a+b;
return c;
}
int _tmain(int argc, _TCHAR* argv[])
{
int c=sum(6,5);
cout<<"sum =="<<c<<endl;
int d=sum();
cout<<"sum =="<<d<<endl;
return 0;
}
Output :-
sum ==11
sum ==30
2. Write a C++ program to swap the values of two variables and demonstrates a function
using call by value.
#include "stdafx.h"
#include<iostream>
using namespace std;
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<"after swap a and b\t"<<endl;
cout<<a<<"\t"<<b<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a=10,b=20;
cout<<"before swap a and b\t"<<endl;
cout<<a<<"\t"<<b<<endl;
swap(a,b);
return 0;
}
Output:-
3. Write a C++ program the swap the values of two variables and demonstrates a function
using Call by reference.
#include "stdafx.h"
#include<iostream>
using namespace std;
void swap(int &a,int &b)
{
int temp;
temp=a;
a=b;
b=temp;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a=10,b=20;
cout<<"before swap a and b\t"<<endl;
cout<<a<<"\t"<<b<<endl;
swap(a,b);
cout<<"after swap a and b\t"<<endl;
cout<<a<<"\t"<<b<<endl;
return 0;
}
Output:-
#include "stdafx.h"
#include<iostream>
using namespace std;
int x=100,y=2000,z=500;
inline int MAX()
{
int big=x;
if(big<y)
big=y;
if(big<z)
big=z;
return big;
}
inline int MIN()
{
int sml=x;
if(sml>y)
sml=y;
if(sml>z)
sml=z;
return sml;
}
int sec()
{
if(x>y||z>y)
if(x<y||z<y)
return y;
if(y>x||z>x)
if(y<x||z<x)
return x;
if(x>z||y>z)
if(x<z||y<z)
return z;
}
void main()
{
int a=MAX();
int b=MIN();
int c=sec();
cout<<"Max="<<a<<endl;
cout<<"Min="<<b<<endl;
cout<<"Second large="<<c<<endl;
}
Output:-
Max=2000
Min=100
Second large=500
5. Write a program to calculate the volume of different geometric shapes like cube, cylinder
and sphere and hence implement the concept of Function Overloading.
#include "stdafx.h"
#include<iostream>
using namespace std;
int volume(int a)
{
return(a*a*a);
}
float volume(int r,int h,float pi)
{
return(pi*r*r*h);
}
float volume(int r,float pi)
{
float s=1.44*(pi)*(r*r*r);
return s;
}
void main()
{
int a=2,r=5,h=3;
float pi=3.14;
int x=volume(a);
float y=volume(r,h,pi);
float z=volume(r,pi);
cout<<"cube:"<<x<<endl;
cout<<"cylinder:"<<y<<endl;
cout<<"sphere:"<<z<<endl;
}
Output:-
cube:8
cylinder:235.5
sphere:565.2
6. Write a C++ program to create a template function for Bubble Sort and demonstrate
sorting of integers and doubles.
#include "stdafx.h"
#include <iostream>
using namespace std;
template <typename q>
void sort(q s[],int n)
{
q a;
int i,j;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (s[i] > s[j])
{
a = s[i];
s[i] = s[j];
s[j] = a;
}
}
}
cout<<"The numbers arranged in ascending order are given below \n";
for (i = 0; i < n; ++i)
cout<<s[i]<<endl;
}
void main()
{
int i, a, n, s[30];
double r[30];
cout<<"Enter the value of n \n";
cin>>n;
cout<<"Enter int numbers \n";
for (i = 0; i < n; ++i)
cin>>s[i];
sort(s,n);
cout<<"Enter double numbers \n";
for (i = 0; i < n; ++i)
cin>>r[i];
sort(r,n);
}
Output:-
PART-B
1. Define a STUDENT class with USN, Name, and Marks in 3 tests of a subject. Declare an
array of 10 STUDENT objects. Using appropriate functions, find the average of the two
better marks for each student. Print the USN, Name and the average marks of all the
students.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Student
{
int usn;
char name[25];
int total;
int m1,m2,m3;
public:float avg;
public:void Read()
{
cout<<"enter student details usn ,name,m1,m2,m3"<<endl;
cin>>usn>>name>>m1>>m2>>m3;
}
public:void display()
{
cout<<"student details"<<endl;
cout<<"usn=="<<usn<<"\t"<<"Name=="<<name<<endl;
cout<<"m1=="<<m1<<"\t"<<"m2=="<<m2<<"\t"<<"m3=="<<m3<<"\t"<<endl;
cout<<"average marks=="<<avg<<endl;
}
public:void calc()
{
int small=m1;
if(small>m2)
small=m2;
if(small>m3)
small=m3;
total=m1+m2+m3-small;
avg=total/2;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int n,i;
Student S[10];
cout<<"enter many student"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
S[i].Read();
}
for(i=0;i<n;i++)
{
S[i].calc();
S[i].display();
}
return 0;
}
Output:-
Class diagram:-
2. Write a C++ program to create a class called COMPLEX and implement the following
overloading functions ADD that return a complex number:
(i) ADD (a, s2) – where ‘a’ is an integer (real part) and s2 is a complex number
(ii) ADD (s1, s2) – where s1 and s2 are complex numbers.
#include "stdafx.h"
#include<iostream>
using namespace std;
class COMPLEX
{
public:int real;
int imaginary;
COMPLEX()
{
}
COMPLEX (int a,int b)
{
real=a;
imaginary=b;
}
};
COMPLEX add(COMPLEX &A,COMPLEX &B)
{
COMPLEX C(0,0);
C.real=A.real+B.real;
C.imaginary=A.imaginary+B.imaginary;
return C;
}
COMPLEX add(int a,COMPLEX &A)
{
COMPLEX C(0,0);
C.real=a+A.real;
C.imaginary=A.imaginary;
return C;
}
void display (COMPLEX &C)
{
cout<<"Real number sum=="<<C.real<<endl;
cout<<"Imaginary number sum=="<<C.imaginary<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a=10;
COMPLEX C1(10,25);
COMPLEX C2(15,30);
cout<<"Add 2 complex numbers "<<endl;
COMPLEX C3=add(C1,C2);
display(C3);
cout<<"Add integer to complex number"<<endl;
COMPLEX C4=add(a,C1);
display(C4);
return 0;
}
Output:-
Add 2 complex numbers
Real number sum==25
Imaginary number sum==55
Add integer to complex number
Real number sum==20
Imaginary number sum==25
Class diagram:-
#include "stdafx.h"
#include<iostream>
using namespace std;
class hsal
{
int x;
hsal(int a)
{
x=a;
}
public:friend int add();
};
class wsal
{
int A;
wsal(int B)
{
A=B;
}
public:friend int add();
};
int add()
{
hsal ob1(20000);
wsal ob2(10000);
int fsal=ob1.x+ob2.A;
return fsal;
}
void main()
{
int fsal=add();
cout<<"family salary:"<<fsal<<endl;
}
Output:-
family salary:30000
Class diagram:-
b) Write a program to accept the student detail such as name and 3 different marks by
get_data() method and display the name and average of marks using display() method.
Define a friend class for calculating the average of marks using the method mark_avg().
#include "stdafx.h"
#include<iostream>
using namespace std;
class student
{
public:int m1,m2,m3,avg;
char n;
public: student(char r,int a,int b,int c)
{
n=r;
m1=a;
m2=b;
m3=c;
}
friend student mark_avg(student);
};
student mark_avg(student s2)
{
int tot;
tot=s2.m1+s2.m2+s2.m3;
s2.avg=tot/3;
return s2;
}
void main()
{
student s1('V',23,45,65);
student s2=mark_avg(s1);
cout<<"name=="<<s2.n<<endl;
cout<<"average of student=="<<s2.avg<<endl;
}
Output:-
Name==V
average of student==44
Class diagram:-
#include "stdafx.h"
#include<iostream>
using namespace std;
class matrix
{
int m,n;
int a[10][10];
int i,j;
public:matrix()
{
}
public:void read()
{
cout<<"enter order of matrix"<<endl;
cin>>m>>n;
cout<<"enter elements of matrix "<<endl;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
matrix operator+(matrix m2)
{
matrix m3;
m3.m=m;
m3.n=n;
for(int i=0;i<m3.m;i++)
{
for(int j=0;j<m3.n;j++)
{
m3.a[i][j]=a[i][j]+m2.a[i][j];
}
cout<<endl;
}
return m3;
}
matrix operator-(matrix m2)
{
matrix m3;
m3.m=m;
m3.n=n;
for(int i=0;i<m3.m;i++)
{
for(int j=0;j<m3.n;j++)
{
m3.a[i][j]=a[i][j]-m2.a[i][j];
}
cout<<endl;
}
return m3;
}
int operator==(matrix m2)
{
if(m==m2.m && n==m2.n)
return 1;
else
return 0;
}
friend void operator<<(ostream &str,matrix &m);
};
return 0;
}
Output:-
Class diagram:-
5. Define a class SET with Data members: array of int, int variable to indicate number of
elements in a SET object; and Member functions: to read element of a SET object, to print
elements of a SET object, to find union of 2 objects of SET using operator overloading
(S3=S1+S2), to find intersection of 2 objects of SET using operator overloading (S4=
S1*S2). S1, S2, S3 and S4 are objects of SET. Use this class in a main function to show the
above operations.
#include"stdafx.h"
#include<iostream>
using namespace std;
int i,j,count=0,k;
class set
{
public:int n;
int a[10];
set()
{
}
~set()
{
}
public:void read()
{
cout<<"enter size"<<endl;
cin>>n;
cout<<"enter set elements"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
}
public:void display()
{
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<endl;
}
set operator+(set s2)
{
set s3;
s3.n=n+s2.n;
int flag=0;
for(i=0;i<n;i++)
{
s3.a[i]=a[i];
count=count+1;
}
for(j=0;j<s2.n;j++)
{
flag=0;
for(k=0;k<count;k++)
{
if(s2.a[j]==s3.a[k])
{
flag=1;
break;
}
}
if(flag==0)
s3.a[count++]=s2.a[j];
}
s3.n=count;
return s3;
}
set operator*(set s2)
{
set s3;
s3.n=n;
int i,j,count=0,flag=0;
for(i=0;i<n;i++)
{
for(j=0;j<s2.n;j++)
{
if(a[i]==s2.a[j])
{
s3.a[count++]=a[i];
break;
}
}
}
s3.n=count;
return s3;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
set s1;
set s2;
s1.read();
s1.display();
s2.read();
s2.display();
cout<<"union of 2 set"<<endl;
set s3=s1+s2;
s3.display();
cout<<"intersection of 2 set"<<endl;
set s4=s1*s2;
s4.display();
return 0;
}
Output:-
enter size
6
enter set elements
1
2
3
4
5
6
1 2 3 4 5 6
enter size
4
enter set elements
1
2
3
7
1 2 3 7
Union of 2 set
1 2 3 4 5 6 7
Intersection of 2 set
1 2 3
Class diagram:-
6. Write a program to create an HUMAN class with features Head, Legs, Hands.(NOTE:
Head, Legs and Hands are of integer/float types)
a. Create an object HUMAN1 using default constructor. (Default features to have 1 Head, 2
Legs and 2 Hands)
b. Create an object HUMAN2 with customized inputs using Parameterized Constructor
C. Create an object HUMAN3 using existing object HUMAN1 (Copy Constructor).
D. Create an object HUMAN4 using Default Arguments Constructor (1 Head, 2 Legs and
2 Hands.
E. All Humans die after their lifetime.(Destructor)
#include "stdafx.h"
#include<iostream>
using namespace std;
class HUMAN
{
public:int noheads;
public:int nohands;
public:int nolegs;
/*public:HUMAN()
{
noheads=1;
nohands=2;
nolegs=2;
}*/
public:HUMAN(int nh,int nha,int nl)
{
noheads=nh;
nohands=nha;
nolegs=nl;
}
public:HUMAN(const HUMAN &H)
{
noheads=H.noheads;
nohands=H.nohands;
nolegs=H.nolegs;
}
public:HUMAN(float n=0,int nhs=1,int nhs1=2,int nl=2)
{
noheads=nhs;
nohands=nhs1;
nolegs=nl;
}
public:void display()
{
cout<<"number of heads=="<<noheads<<endl;
cout<<"number of hands=="<<nohands<<endl;
cout<<"number of legs=="<<nolegs<<endl;
}
~HUMAN()
{
}
};
/* we must comment one of the constructor default constructor or default argument comment
before run the program*/
Output:-
Default constructor
number of heads==1
number of hands==2
number of legs==2
parameterized constructor
number of heads==1
number of hands==2
number of legs==2
Copy constructor
number of heads==1
number of hands==2
number of legs==2
Default argument constructor
number of heads==1
number of hands==2
number of legs==2
Class diagram:-
7. Demonstrate Simple Inheritance concept by creating a base class FATHER with data
members FirstName, SurName, DOB and BankBalance and creating a derived class SON,
which inherits SurName and BankBalance feature from base class but provides its own
feature FirstName and DOB. Create and initialize F1 and S1 objects with appropriate
constructors and display the Father & Son details.
#include "stdafx.h"
#include<iostream>
using namespace std;
class Father
{
public:char fname;
char sname;
int DOB;
int bankbal;
public:Father()
{
fname='N';
sname='V';
DOB=1957;
bankbal=100000;
}
public:void display()
{
cout<<"Father details"<<endl;
cout<<"First name=="<<fname<<endl;
cout<<"Sur name=="<<sname<<endl;
cout<<"D O B=="<<DOB<<endl;
cout<<"Bank balance=="<<bankbal<<endl;
}
};
class Son:public Father
{
public:char fname;
int DOB;
public:Son()
{
fname='V';
DOB=1994;
}
public:void display()
{
cout<<"Son details"<<endl;
cout<<"First name=="<<fname<<endl;
cout<<"Sur name=="<<sname<<endl;
cout<<"D O B=="<<DOB<<endl;
cout<<"Bank balance=="<<bankbal<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Father f1;
f1.display();
Son s1;
s1.display();
return 0;
}
Output:-
Father details
First name==N
Sur name==V
D O B==1957
Bank balance==100000
Son details
First name==V
Sur name==V
D O B==1994
Bank balance==100000
Class diagram:-
8. Create an abstract base class EMPLOYEE with data members: Name, EmpID and
BasicSal and a pure virtual function Cal_Sal().Create two derived classes MANAGER
(with data members: DA and HRA and SALESMAN (with data members: DA, HRA and
TA). Write appropriate constructors and member functions to initialize the data, read and
write the data and to calculate the net salary. The main() function should create array of
base class pointers/references to invoke overridden functions and hence to implement run-
time polymorphism.
#include "stdafx.h"
#include<iostream>
using namespace std;
class Employee
{
public:char name;
int empid;
int bsal;
public:virtual void cal_sal()=0;
public:virtual void display()
{
}
};
class Maneger:public Employee
{
public:int da;
int hra;
int ta;
int ns;
Maneger(char v,int em,int bas)
{
name=v;
empid=em;
bsal=bas;
}
public:void cal_sal()
{
da=0.025*bsal;
hra=0.05*bsal;
ta=0.025*bsal;
ns=bsal+da+hra-ta;
}
public:void display()
{
cout<<”maneger datails”<<endl;
cout<<”da==”<<da<<”\t”<<”hra==”<<hra<<”\t”<<”ta==”<<ta<<”\t”<<”ns==”<
<ns<<”\t”<<endl;
}
};
class Salesman:public Employee
{
public:
int da;
int hra;
int ns;
int ta;
Salesman(char v,* n tem,int bas)
{
name=v;
empid=em;
bsal=bas;
}
public:void cal_sal()
{
da=0.025*bsal;
hra=0.05*bsal;
ta=0.25*bsal;
ns=bsal+da+hra-ta;
}
public:void display()
{
cout<<”Salesman datails”<<endl;
cout<<”da==”<<da<<”\t”<<”hra==”<<hra<<”\t”<<”ta==”<<ta<<”\t”<<”ns==”<
<ns<<”\t”<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Salesman S(‘V’,1,10000);
Maneger M(‘N’,10,20000);
Employee *pt[2]={&S,&M};
for(int i=0;i<2;i++)
{
pt[i]->cal_sal();
pt[i]->display();
}
return 0;
}
Output:-
Salesman datails
da==250 hra==500 ta==2500 ns==8250
maneger datails
da==500 hra==1000 ta==500 ns==21000
Class diagram:-
9. I/O streams and functions. Write a program to implement FILE I/O operations on
characters. I/O operations includes inputting a string, Calculating length of the string,
Storing the string in a file, fetching the stored characters from it, etc.
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str1;
cin>>str1;
int len=str1.size();
cout<<"length=="<<len<<endl;
cout<<"writing to file"<<endl;
ofstream ofs;
ofs.open("vinay.txt");
ofs<<str1<<endl;
ofs<<len<<endl;
ofs.close();
cout<<"reading from file"<<endl;
ifstream ifs;
ifs.open("vinay.txt");
ifs>>str1;
ifs.close();
cout<<str1<<endl;
cout<<len<<endl;
return 0;
}
Output:-
welcometomitmca
length==15
writing to file
reading from file
welcometomitmca
15
10. Write a program to implement Exception Handling with minimum 5 exceptions Classes
including two built-in exceptions.
#include "stdafx.h"
#include<iostream>
#include<exception>
using namespace std;
class insuffexp:public exception
{
public:void insu()
{
cout<<"Insufficiate balance"<<endl;
}
};
class min5exp:public exception
{
public:void min()
{
cout<<"Minimum balance 500"<<endl;
}
};
class account
{
public:int acno;
char name;
int bal;
account()
{
acno=895;
name='v';
bal=100;
}
account(int a,char b,int c)
{
acno=a;
name=b;
bal=c;
}
public:void withdrawn(int w)
{
try
{
if(bal<w)
{
insuffexp in;
throw in;
}
bal=bal-w;
}
catch(insuffexp e)
{
e.insu();
}
}
public:void deposite(int d)
{
try{
if(d<500)
{
min5exp m;
throw m;
}
bal=bal+d;
}
catch(min5exp e)
{
e.min();
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
account ac;
account a(123,'b',500);
ac.withdrawn(500);
a.deposite(400);
return 0;
}
Output:-
Balance=500 and deposite<500
Insufficiate balance
Minimum balance 500
Balance=500 and deposite>500
Insufficiate balance
Balance>500 and deposite<500
Minimum balance 500
Class diagram:-
11. Write a program to concatenate 2 strings using STL String class functions.
#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
string str1;
cin>>str1;
string str2;
cin>>str2;
string str3;
str3=str1+” “+str2;
cout<<str3<<endl;
return 0;
}
Output:-
vinay
kumar
vinay kumar
12. Write a simple C++ program to store and display integer elements using STL Vector
class.
#include "stdafx.h"
#include<iostream>
using namespace std;
#include<vector>
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vec;
vec.push_back(20);
vec.push_back(30);
vec.push_back(10);
vec.push_back(50);
vector<int>::iterator i=vec.begin();
vector<int>::iterator n=vec.end();
cout<<"Vector elements are:"<<endl;
for(i;i<n;i++)
{
cout<<*i<<endl;
}
return 0;
}
Output:-
Vector elements are:
20
30
10
50