Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CPP Lab PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

C++

Programming Lab
18MCA16

abhishek mp
MCA
PART-A

1.Write a C++ program to find the sum for the given


variables using function with default arguments.
#include<iostream.h>
#include<conio.h>
void sum(int a,int b,int c=6,int d=10);
void main()
{
int a,b,c,d;
cout<<"enter the numbers\n";
cin>>a>>b;
sum(a,b);
getch();
}
void sum(int a1,int a2,int a3,int a4)
{
int temp;
temp=a1+a2+a3+a4;
cout<<"a:"<<a1<<endl;
cout<<"b:"<<a2<<endl;
cout<<"c:"<<a3<<endl;
cout<<"d:"<<a4<<endl;
cout<<"sum="<<temp<<endl;
}
2. Write a C++ program to swap the values of two
variables and demonstrates a function using call by
value.
#include<iostream.h>
#include<conio.h>
void swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\n inside swap function \n"<<"x="<<x<<"y="<<y<<endl;
}
void main()
{
clrscr();
int a,b;
cout<<"enter the value of a and b";
cin>>a>>b;
cout<<"a="<<a<<"b="<<b<<endl;
swap(a,b);
cout<<"\n after swap function \n"<<"a="<<a<<"b="<<b<<endl;
getch();
}
3. Write a C++ program to swap the values of two
variables and demonstrates a function using Call by
reference using reference type (&).
#include<iostream.h>
#include<conio.h>
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<"\n inside swap function \n"<<"x="<<x<<"y="<<y<<endl;
}
void main()
{
clrscr();
int a,b;
cout<<"enter the value of a and b";
cin>>a>>b;
cout<<"a="<<a<<"b="<<b<<endl;
swap(a,b);
cout<<"\n after swap function \n"<<"a="<<a<<"b="<<b<<endl;
getch();
}
4. Write a C++ program to swap the values of two
variables and demonstrates a function using Call by
reference using pointer (*).
#include<iostream.h>
#include<conio.h>
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
cout<<"\n inside swap function \n"<<"x="<<*x<<"y="<<*y<<endl;
}
void main()
{
clrscr();
int a,b;
cout<<"enter the value of a and b";
cin>>a>>b;
cout<<"a="<<a<<"b="<<b<<endl;
swap(&a,&b);
cout<<"\n after swap function \n"<<"a="<<a<<"b="<<b<<endl;
getch();
}
7. 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<iostream.h>
#include<conio.h>
float vol(int,int);
int vol(int);
float val(float);
void main()
{
clrscr();
int r,a,h;
float r1;
cout<<"enter radius and heigth of cylender"<<endl;
cin>>r>>h;
cout<<"enter side of cube"<<endl;
cin>>a;
cout<<"enter radius of spear"<<endl;
cin>>r1;
cout<<"vloumn of cylender is : "<<vol(r,h)<<endl;
cout<<"vloumn of cube is : "<<vol(a)<<endl;
cout<<"vloumn of cylender is : "<<val(r1)<<endl;
getch();
}
float vol(int r,int h)
{
return (3.14*r*r*h);
}
float val(float r1)
{
return ((4.0/3.0)*3.14*(r1*r1*r1));
}
int vol(int a)
{
return (a*a*a);
}
8. Write a C++ program to create a template
function for Bubble Sort and demonstrate sorting
of integers and doubles.
#include<iostream.h>
#include<conio.h>
template <class bubble>
void bubble(bubble a[],int n)
{
int i,j;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
bubble temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void main()
{
int a[6]={1,5,6,2,9,8};
char b[4]={'s','b','o','c'};
clrscr();
bubble(a,6);
cout<<"\n sorted order of integer "<<endl;
for(int i=0;i<6;i++)
cout<<a[i]<<endl;
bubble(b,4);
cout<<"\n sorted order of character"<<endl;
for(int j=0;j<4;j++)
cout<<b[j]<<endl;
getch();
}
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<iostream.h>
#include<conio.h>
class student
{
private:
char name[10];
char usn[10];
float avg,m1,m2,m3;
public:
void readdata()
{
cout<<"__________________________________________"<<endl;
cout<<"Enter the Student Name\n";
cin>>name;
cout<<"Enter the Student USN \n";
cin>>usn;
cout<<"Enter the Student MARKS of 3 subjects\n";
cin>>m1;
cin>>m2;
cin>>m3;
cout<<"__________________________________________"<<endl;
}
void compute()
{
float sum;
if(m1>m3&&m2>m3)
sum=m1+m2;
else if(m2>m1 && m3>m1)
sum=m2+m3;
else
sum=m1+m3;
avg=sum/2;
}
void display()
{
cout<<"__________________________________________"<<endl;
cout<<"name "<<name<<endl;
cout<<"usn "<<usn<<endl;
cout<<"marks 1 "<<m1<<endl;
cout<<"marks 2 "<<m2<<endl;
cout<<"marks 3 "<<m3<<endl;
cout<<"avg "<<avg<<endl;
cout<<"__________________________________________"<<endl;
}
};
void main()
{
student s[10];
int i,n;
clrscr();
cout<<"enter number ofstudents "<<endl;
cin>>n;

cout<<"------------------------------------------------\n";
for(i=0;i<n;i++)
{
s[i].readdata();
s[i].compute();
}
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
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<iostream.h>
#include<conio.h>
#include<stdlib.h>
class complex
{
private:
int rp,ip;
public:
void read()
{
cout<<"enter a complex number \n";
cout<<"enter real part\n";
cin>>rp;
cout<<"enter imaginary part\n";
cin>>ip;
}
complex add(complex s1,complex s2)
{
complex c;
c.rp=s1.rp+s2.rp;
c.ip=s1.ip+s2.ip;
return c;
}
complex add(int ival,complex s2)
{
complex c;
c.rp=ival+s2.rp;
c.ip=s2.ip;
return c;
}
void display(complex t)
{
cout<<"result";
cout<<"\n"<<t.rp<<"+i"<<t.ip<<"\n";
}
};
void main()
{
complex c1,c2,c3,c4,t;
int ival;
int ch;
clrscr();
cout<<"\n complex numbers\n";
cout<<"1: add two complex numbers\n";
cout<<"2: add a integer and a complex number\n";
cout<<"3: exit";
cout<<"\n enter your choice\n";
cin>>ch;
switch(ch)
{
case 1:c1.read();
t.display(c1);
c2.read();
t.display(c2);
c3=t.add(c1,c2);
t.display(c3);
break;
case 2:cout<<"enter an integer number\n";
cin>>ival;
c1.read();
t.display(c1);
c4=t.add(ival,c1);
t.display(c4);
break;
case 3:exit(0);
break;
}
getch();
}
3. Friend functions and friend classes:

a) Write a program to define class name HUSBAND and WIFE that


holds the income respectively. Calculate and display the total
income of a family using Friend function.

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().

3a.cpp
#include<iostream.h>
#include<conio.h>
class wife;
class husband
{
double sal;
char hname[10];
public:
void getdata();
void display();
friend int totalincome(husband,wife);
};
class wife
{
double income2;
char wname[10];
public:
void getdata1();
void display1();
friend int totalincome(husband,wife);
};
int totalincome(husband h,wife w)
{
return(h.sal+w.income2);
}
void husband::getdata()
{
cout<<"enter the husband name"<<endl;
cin>>hname;
cout<<"enter the husband salary"<<endl;
cin>>sal;
}
void wife::getdata1()
{
cout<<"enter the wife name"<<endl;
cin>>wname;
cout<<"enter the wife salary"<<endl;
cin>>income2;
}
void husband::display()
{
cout<<"Husband Name: "<<hname<<endl;
cout<<"Husband Salary: "<<sal<<endl;
}
void wife::display1()
{
cout<<"wife name : "<<wname<<endl;
cout<<"wife income: "<<income2<<endl;
}
void main()
{
int d;
clrscr();
husband h;
wife w;
h.getdata();
w.getdata1();
cout<<"____________________________________________"<<endl;
cout<<"family name: happy family"<<endl;
cout<<"____________________________________________"<<endl;
h.display();
w.display1();
cout<<"total income of family= "<<totalincome(h,w)<<endl;
getch();
}
3b.cpp
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class student
{
char name[20];
int m1,m2,m3;
public:
void get_data();
void display();
friend class avg;
};
void student::get_data()
{
cout<<"Enter the name : "<<endl;
cin>>name;
cout<<"Enter three marks :"<<endl;
cin>>m1>>m2>>m3;
}
void student::display()
{
cout<<"______________________________________________"<<endl;
cout<<"student name marks1 marks2 marks3 "<<endl;
cout<<"______________________________________________"<<endl;
cout<<name<<" "<<m1<<" "<<m2<<" "<<m3<<endl;
}
class avg
{
public:
int marks_avg(student s1);
};
int avg::marks_avg(student s1)
{
return((s1.m1+s1.m2+s1.m3)/3);
}
void main()
{
clrscr();
student s;
avg fs;
s.get_data();
s.display();
cout<<"the average of three is "<<fs.marks_avg(s)<<endl;
getch();
}
4. Create a class called MATRIX using two-dimensional array of integers.
Implement the following operations by overloading the operator == which checks
the compatibility of two matrices to be added and subtracted. Perform the
addition and subtraction by overloading the + and – operators respectively.
Display the results by overloading the operator <<. If (m1== m2) then m3 = m1+m2
and m4 = m1- m2 else display error.

#include<iostream.h>
#include<conio.h>
class matrix
{
private :
int m[5][5];
int r,c;
public :
matrix()
{
r=0;c=0;
}
void read();
friend int operator ==(matrix,matrix);
friend matrix operator +(matrix,matrix);
friend matrix operator -(matrix,matrix);
friend ostream& operator << (ostream&,matrix&);
};
void matrix::read()
{
cout<<"\nEnter the order of the matrix (r&c) : ";
cin>>r>>c;
cout<<"\nEnter the elements : \n";
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>m[i][j];
}
int operator ==(matrix a,matrix b)
{
if((a.r==b.r) && (a.c==b.c))
return 1;
else
return 0;
}
matrix operator +(matrix a,matrix b)
{
matrix t;
for(int i=0;i<a.r;i++)
for(int j=0;j<a.c;j++)
t.m[i][j]=a.m[i][j]+b.m[i][j];
t.r=a.r;
t.c=a.c;
return t;
}
matrix operator -(matrix a,matrix b)
{
matrix t;
for(int i=0;i<a.r;i++)
for(int j=0;j<a.c;j++)
t.m[i][j]=a.m[i][j]-b.m[i][j];
t.r=a.r;
t.c=a.c;
return t;
}
ostream& operator << (ostream& os,matrix& a)
{
for(int i=0;i<a.r;i++)
{
for(int j=0;j<a.c;j++)
os<<a.m[i][j]<<" ";
cout<<"\n";
}
return os;
}
void main()
{
clrscr();
matrix a,b,c,d;
a.read();
b.read();
if(a==b)
{
c=a+b;
cout<<"\nThe sum of two matrices is : \n";
cout<<c; d=a-b;
cout<<"\nThe difference of two matrices is : \n";
cout<<d;
}
else
cout<<"\nThe matrix addition & subtraction is not possible.";
getch();
}
5. Write a program to create an HUMAN class with features as number of Head,
Legs, Hands.(NOTE: Number of Head, Legs and Hands are of integer 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. All Humans die after their lifetime.
(Destructor)

#include<iostream.h>
#include<conio.h>
class Human
{
private:
int head,hands,legs;
public:
Human()
{
head=1;
hands=2;
legs=2;
}
Human(Human &h)
{
head=h.head;
hands=h.hands;
legs=h.legs;
}
Human(int h, int leg=4,int hand=5)
{
head=h;
hands=hand;
legs=leg;
}
~Human()
{
cout<<" humans are dead \n";
getch();
}
void display()
{
cout<<"head="<<head<<endl;
cout<<"hands="<<hands<<endl;
cout<<"legs="<<legs<<endl;
}
};
void main()
{
clrscr();
Human man1;
Human man2(3,4,5);
Human man3=man1;
Human man4(1);
cout<<"human 1 values"<<endl;
man1.display();
cout<<endl<<"human 2 values"<<endl;
man2.display();
cout<<endl<<"human 3 values"<<endl;
man3.display();
cout<<endl<<"human 4 values"<<endl;
man4.display();
getch();
}
6. Demonstrate Simple Inheritance concept by creating a base class FATHER with
data members SurName 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. (Hint : While
creating S1 object, call Father base class parameterized constructor through
derived class by sending values)

#include<iostream.h>
#include<conio.h>
class father
{

char name[20];
char dob[10];
protected:
char sname[20];
float bal;
public:
father();
dispfather();
};
father::father()
{
cout<<"***details of father"<<endl;
cout<<"enter the father name "<<endl;
cin>>name;
cout<<"enter the surname"<<endl;
cin>>sname;
cout<<"enter the dob"<<endl;
cin>>dob;
cout<<"enter the balance"<<endl;
cin>>bal;
}
father::dispfather()
{
cout<<" Father's Name :"<<name;
cout<<"surname "<<sname<<endl;
cout<<" Date of birth :"<<dob<<endl;
cout<<" Bank Balance :"<<bal<<endl;
}
class son:public father
{
private:
char name[20];
char dob[10];
public:
void getsondetails();
void dispson();
};
void son::getsondetails()
{
cout<<"enter the name of son"<<endl;
cin>>name;
cout<<"enter the dob"<<endl;
cin>>dob;
}
void son::dispson()
{
cout<<" Son's Name :"<<name<<" "<<sname<<"\n";
cout<<" Date of birth :"<<dob<<"\n";
cout<<" Bank Balance :"<<bal<<"\n";
}
void main()
{
clrscr();
son s;
s.getsondetails();
s.dispfather();
cout<<"inherited surname and bal from fatehr"<<endl;
s.dispson();
getch();
}
7. 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 it implements run-time polymorphism.

#include<iostream.h>
#include<conio.h>
class Employee
{
private:
char name[20];
char eid[20];
public:
float bsal;
virtual void cal_sal()=0;
Employee()
{
bsal=0;
}
virtual void getdata()
{
cout<<"enter the name:"<<endl;
cin>>name;
cout<<"enter empid"<<endl;
cin>>eid;
cout<<"enter the basic sal:"<<endl;
cin>>bsal;
}
virtual void display()
{
cout<<"employee name:"<<name<<endl;
cout<<" empid"<<eid<<endl;
cout<<"the basic sal:"<<bsal<<endl;
}
};
class Manager:public Employee
{
private:
float da,hra,netsal;
public:
void getdata()
{
Employee::getdata();
cout<<"enter da:"<<endl;
cin>>da;
cout<<"enter hra"<<endl;
cin>>hra;
}
void cal_sal()
{
netsal=bsal+da+hra;
}
void display()
{
cout<<"______manager details______"<<endl;
Employee::display();
cout<<"da="<<da<<endl;
cout<<"hra="<<hra<<endl;
cout<<"netsal="<<netsal<<endl;
}
};
class salesmen:public Employee
{
private:
float da,hra,ta,netsal;
void getdata()
{
Employee::getdata();
cout<<"enter da:"<<endl;
cin>>da;
cout<<"enter hra"<<endl;
cin>>hra;
cout<<"ta"<<endl;
cin>>ta;
}
void cal_sal()
{
netsal=bsal+da+hra+ta;
}

void display()
{
cout<<"______salesemen details______"<<endl;
Employee::display();
cout<<"da="<<da<<endl;
cout<<"hra="<<hra<<endl;
cout<<"ta="<<ta<<endl;
cout<<"netsal="<<netsal<<endl;
}
};
void main()
{
clrscr();
Employee *p[10];
Manager m[5];
salesmen s[5];
int n,i,etype;
cout<<"enter the number os employees"<<endl;
cin>>n;
for(i=0;i<n;i++)
{
cout<<"0-> manager ,1->salsemen"<<endl;
cin>>etype;
if(etype==0)
p[i]=new Manager;
else
p[i]=new salesmen;
p[i]-> getdata();
}
for(i=0;i<n;i++)
p[i]-> cal_sal();
for(i=0;i<n;i++)
p[i]->display();
getch();
}

You might also like