CPP Lab PDF
CPP Lab PDF
Programming Lab
18MCA16
abhishek mp
MCA
PART-A
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
#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:
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();
}