C++ File Sahil Pathak Bca 3eb 088
C++ File Sahil Pathak Bca 3eb 088
1
INDEX
S.NO Pg. Teacher’s
. PROGRAM No. Date Signature
Pick a small program that you would like to solve 7 22-08-20
via C++ program. Please make the solution to the
problem contain the following attributes:
2
a default constructor that prompts for the input of
the values for the above data numbers.
Age:
Address:
Define a class counter which contains an int 23 19-09-20
variable count defined as static and a static
function Display () to display the value of count.
Whenever an object of this class is created count is
9 incremented by 1. Use this class in main to create
multiple objects of this class and display value of
count each time.
3
WAP to implement call by reference by creating 31 03-10-20
13 Account class. [Hint. Assume necessary functions]
WAP to implement Array of objects using suitable 33 03-10-20
14 problem statement.
WAP to implement operator overloading with 35 13-10-20
15 string class functions (strcpy, strconcat etc.)
16 WAP to implement ‘Inline function’. 37 21-10-20
4
Serve a new class called house from those two
classes. Take appropriate functions and data
members to show the relationship.
Implement the following class hierarchy 62 04-12-20
considering appropriate data members and
member functions.
26
27
Member Functions:
5
‘containership’.
Implement the following data structures using C+ 74 14-12-20
+: (You can also use templates)
30
Linked List, Stack and Queue
WAP to show swapping using template function 80 14-12-20
31 (Generic)
PRACTICAL-1
1. Pick a small program that you would like to solve via C++ program. Please make the solution to
the problem contain the following attributes:
• At least one function which takes in arguments and/or return values.
• Input from the user into a variable using the cin construct.
• Output to the user using the cout construct
CODE
6
#include<iostream.h>
#include<conio.h>
class student
{
private:
int rollno;
int age;
public:
void enterdata()
{
cout<<"enter rollno-";
cin>>rollno;
cout<<"enter age-";
cin>>age;
}
void setdata(int a,int b)
{
rollno=a;
age=b;
}
void display()
{
cout<<"the roll no is " <<rollno<< " and age is "<<age<<"\n";
}
};
void main()
{
student s,p;
clrscr();
s.enterdata();
p.setdata(33,13);
s.display();
p.display();
getch();
}
OUTPUT
7
PRACTICAL-2
8
2. Write a program (WAP) that asks the user to enter a temperature in degrees Fahrenheit, and then
prints out the equivalent temperature in degrees Celsius.
CODE
#include<iostream.h>
#include<conio.h>
class temperature
{
private:
float cels,farh;
public:
void enterdata()
{
cout<<"enter temperature in farheneit : ";
cin>>farh;
}
void display()
{
cout<<"farheneit : "<<farh<<"to celsius : "<<cels;
}
void conversion()
{
cels=(farh-32)*5/9;
}
};
void main()
{
temperature t;
clrscr();
t.enterdata();
t.conversion();
t.display();
getch();
}
OUTPUT
9
PRACTICAL-3
10
3. Write a function called factorial that returns the factorial of any positive whole number. Your
function is required to use recursion.
CODE
#include<iostream.h>
#include<conio.h>
class factorial
{
private:
int n,f;
public:
void enterdata()
{
cout<<"\nEnter any number : ";
cin>>n;
}
int fact(int n)
{
if(n==0||n==1)
return 1;
else
return n*fact(n-1);
}
void display()
{
cout<<"\nFactorial of [ "<<n<<" ] is : "<<fact(n);
}
};
void main ()
{
factorial f;
int n;
clrscr();
11
f.enterdata();
f.fact(n);
f.display();
getch();
}
OUTPUT
PRACTICAL-4
4. WAP to implement student result. [Hint: implement percentage () etc.]
12
CODE
#include<iostream.h>
#include<conio.h>
class student
{
private:
int marks1,marks2,marks3,sum;
float perc;
char name[10];
public:
void enterdata()
{
cout<<"enter student name-";
cin>>name;
cout<<"enter marks-";
cin>>marks1>>marks2>>marks3;
}
void percentage()
{
sum=marks1+marks2+marks3;
perc=sum/3;
}
void display()
{
cout<<"name of student is -" <<name<<"\npercentage- "<<perc;
}
};
void main()
{
student s;
clrscr();
s.enterdata();
s.percentage();
s.display();
getch();
}
13
OUTPUT
PRACTICAL-5
14
5. WAP to implement friend function by taking some real life example.
CODE
#include <iostream.h>
#include<conio.h>
class B;
class A
{
int x;
public:
void setdata(int i)
{
x=i;
}
friend void sum(A,B);
};
class B
{
int y;
public:
void setdata(int i)
{
y=i;
}
friend void sum(A,B);
};
void sum(A a,B b)
{
int c;
c=a.x+b.y;
cout<<"sum is "<<c;
}
void main()
{
A a;
B b;
clrscr();
a.setdata(10);
b.setdata(20);
sum(a,b);
getch();
}
15
OUTPUT
PRACTICAL-6
16
6. Write a function twodigitreverse() that reverse the digit of a two-digit integer.
CODE
#include<iostream.h>
#include<conio.h>
class digit
{
private:
int number,revnumber;
public:
void enterdata()
{
cout<<"enter number - ";
cin>>number;
}
void display()
{
cout<<"reversed number is "<<revnumber ;
}
void reversedigit()
{
int s;
revnumber=0;
while (number!=0)
{
s=number%10;
revnumber=revnumber*10+s;
number=number/10;
}
}
};
void main()
{
digit d;
clrscr();
d.enterdata();
d.reversedigit();
d.display();
17
getch();
}
OUTPUT
18
PRACTICAL-7
7. Write a class called CAccount which contains two private data elements, an integer
accountNumber and a floating point accountBalance, and three member functions:
• A constructor that allows the user to set initial values for accountNumber and accountBalance and a
default constructor that prompts for the input of the values for the above data numbers.
• A function called inputTransaction, which reads a character value for transactionType(‘D’ for
deposit and ‘W’ for withdrawal), and a floating point value for transactionAmount, which updates
accountBalance.
• A function called printBalance, which prints on the screen the accountNumber and accountBalance.
CODE
#include<iostream.h>
#include<conio.h>
class CAccount
{
private:
int accountNumber;
float accountBalance;
public:
CAccount()
{
cout<<"enter account number : ";
cin>>accountNumber;
cout<<"enter account balance : ";
cin>>accountBalance;
}
void printBalance()
{
cout<<"account number: "<<accountNumber<<"\n";
cout<<"account balance: "<<accountBalance<<"\n";
}
void inputTransaction()
{
char transtype;
float transamount;
cout<<"enter transaction type: ";
cin>>transtype;
cout<<"enter amount: ";
cin>>transamount;
switch(transtype)
{
case 'w':
19
accountBalance=accountBalance-transamount;
break;
case 'd':
accountBalance=accountBalance+transamount;
break;
default:
cout<<"wrong input";
}
}
};
void main()
{
clrscr();
CAccount a1;
a1.inputTransaction();
a1.printBalance();
a1.inputTransaction();
a1.printBalance();
getch();
}
OUTPUT
PRACTICAL-8
20
8. Create a class employee which have name, age and address of employee, include functions
getdata() and showdata(). Getdata() takes the input from the user and inserts in a file, showdata()
reads the same file to diplay the data in following format:
Name:
Address:
Age:
CODE
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class employee
{
private:
char name[20],address[40];
int age;
public:
void getdata()
{
ofstream outf("employee.doc");
cout<<"enter name : ";
cin>>name;
cout<<"enter age : ";
cin>>age;
cout<<"enter address : ";
cin>>address;
outf<<name<<"\n";
outf<<age<<"\n";
outf<<address<<"\n";
outf.close();
}
void showdata()
{
ifstream inf("employee.doc");
inf>>name;
inf>>age;
inf>>address;
cout<<"name: "<<name;
cout<<"\nage: "<<age;
cout<<"\naddress: "<<address;
}
};
void main()
{
clrscr();
21
employee e1;
e1.getdata();
e1.showdata();
getch();
}
OUTPUT
PRACTICAL-9
9. Define a class counter which contains an int variable count defined as static and a static function
Display () to display the value of count. Whenever an object of this class is created count is
22
incremented by 1. Use this class in main to create multiple objects of this class and display value of
count each time.
CODE
#include<iostream.h>
#include<conio.h>
class digit
{
private:
static int cont;
public:
static void total()
{
cout<<"no of total object : "<<cont<<"\n";
}
digit()
{
cont++;
cout<<"no of object created: "<<cont<<"\n";
}
~digit()
{
cout<<"no of object destroyed: "<<cont<<"\n";
cont--;
}
};
int digit::cont=0;
void main()
{
clrscr();
digit a,b,c;
digit::total();
getch();
}
OUTPUT
23
PRACTICAL-10
24
10. Write a function power () to raise n to the power p which takes a double value for n and int for p,
and returns the result as double. Use default argument 2 for p. Write a main() to test the program.
CODE
#include<iostream.h>
#include<conio.h>
class digit
{
private:
double number,sol;
int pow;
public:
void enterdata(double n,int p=2)
{
number=n;
pow=p;
}
void display()
{
cout<<number<<" ^ "<<pow<<" is "<<sol<<"\n";
}
void power()
{
sol=1;
int po;
po=pow;
while(po!=0)
{
sol=sol*number;
po--;
}
}
};
void main()
{
digit d,e;
clrscr();
d.enterdata(8);
d.power();
d.display();
e.enterdata(2,3);
e.power();
e.display();
getch();
25
}
OUTPUT
PRACTICAL-11
26
11. WAP to multiply two complex numbers.
CODE\
#include<iostream.h>
#include<conio.h>
class number
{
private:
int real,imag;
public:
void enterdata()
{
cout<<"enter real no : ";
cin>>real;
cout<<"enter imaginary no : ";
cin>>imag;
}
void display()
{
cout<<"product is: "<<real<<" + "<<imag<<" i ";
}
void product(number a,number b)
{
real=a.real*b.real-a.imag*b.imag;
imag=a.real*b.imag+a.imag*b.real;
}
};
void main()
{
number a,b,c;
clrscr();
a.enterdata();
b.enterdata();
c.sum(a,b);
c.display();
getch();
}
OUTPUT
27
PRACTICAL-12
28
12. WAP to add two Minute and Hour values.
CODE
#include<iostream.h>
#include<conio.h>
class time
{
private:
int hour,min;
public:
void enterdata()
{
cout<<"enter hours : ";
cin>>hour;
cout<<"enter mins : ";
cin>>min;
}
void display()
{
cout<<"sum is: "<<hour<<" hours "<<min<<" minutes ";
}
void sum(time a,time b)
{
hour=a.hour+b.hour;
min=a.min+b.min;
if (min>=60)
{
min=min-60;
hour=hour+1;
}
}
};
void main()
{
time a,b,c;
clrscr();
a.enterdata();
b.enterdata();
c.sum(a,b);
c.display();
getch();
}
29
OUTPUT
PRACTICAL-13
30
13. WAP to implement call by reference by creating Account class. [Hint. Assume necessary
functions]
CODE
#include<iostream.h>
#include<conio.h>
class CAccount
{
private:
int accountNumber;
float accountBalance;
public:
CAccount(int &accno,float &accbal)
{
accountNumber=accno;
accountBalance=accbal;
}
void printBalance()
{
cout<<"account number: "<<accountNumber<<"\n";
cout<<"account balance: "<<accountBalance<<"\n";
}
void inputTransaction()
{
char transtype;
float transamount;
cout<<"enter transaction type: ";
cin>>transtype;
cout<<"enter amount: ";
cin>>transamount;
switch(transtype)
{
case 'w':
accountBalance=accountBalance-transamount;
break;
case 'd':
accountBalance=accountBalance+transamount;
break;
default:
31
cout<<"wrong input";
}
}
};
void main()
{
clrscr();
CAccount a1(1,500);
a1.printBalance();
a1.inputTransaction();
a1.printBalance();
a1.inputTransaction();
a1.printBalance();
getch();
}
OUTPUT
PRACTICAL-14
32
14. WAP to implement Array of objects using suitable problem statement.
CODE
#include<iostream.h>
#include<conio.h>
class student
{
private:
char name[10];
int rollno;
public:
student()
{
cout<<"enter name : ";
cin>>name;
cout<<"enter roll no: ";
cin>>rollno;
}
};
void main()
{
clrscr();
student st[3];
getch();
}
OUTPUT
33
PRACTICAL-15
34
15. WAP to implement operator overloading with string class functions (strcpy, strconcat etc.)
CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size=81;
class concat
{
char str[size];
public:
concat()
{
strcpy(str,"");
}
concat(char s[])
{
strcpy(str,s);
}
void display()
{
cout<<str;
}
friend concat operator +(concat s1,concat s2);
};
concat operator +(concat s1,concat s2)
{
concat temp;
strcpy(temp.str,s1.str);
strcat(temp.str,s2.str);
return temp;
}
void main()
{
clrscr();
concat s1="sahil";
concat s2=" pathak";
concat s3;
s3=s1+s2;
s3.display();
getch();
35
}
OUTPUT
36
PRACTICAL-16
CODE
#include<iostream.h>
#include<conio.h>
class date
{
private:
int day,month,year;
public:
void set(int d,int m,int y);
void show();
};
inline void date::set(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
inline void date::show()
{
cout<<day<<"-"<<month<<"-"<<year;
}
void main()
{
date d1;
clrscr();
cout<<"date of birth : ";
d1.set(8,04,2001);
d1.show();
getch();
}
37
OUTPUT
38
PRACTICAL-17
CODE
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"no of objects created: "<<count<<"\n";
}
~alpha()
{
count--;
cout<<"no of objects destroyed: "<<count<<"\n";
}
};
void main()
{
alpha a,b,c;
clrscr();
getch();
}
OUTPUT
39
40
PRACTICAL-18
CODE
#include<iostream.h>
#include<conio.h>
class garment
{
private:
int length;
int waist;
public:
garment(int l,int w)
{
length=l;
waist=w;
}
garment(garment &g)
{
length=g.length;
waist=g.waist;
}
void display()
{
cout<<"length: "<<length<<"\n";
cout<<"waist: "<<waist<<"\n";
}
};
void main()
{
clrscr();
garment b1(42,32);
garment b2(46,30);
garment b3=b1;
b1.display();
b2.display();
b3.display();
41
getch();
}
OUTPUT
42
PRACTICAL-19
CODE
#include<iostream.h>
#include<conio.h>
class overloading
{
private:
float c;
public:
void sum(int a,int b)
{
c=a+b;
}
void sum(double a,float b)
{
c=a+b;
}
void sum(float a,float b)
{
c=a+b;
}
void show()
{
cout<<"sum is "<<c<<"\n";
}
};
void main()
{
overloading a,b,c;
clrscr();
a.sum(5.00,7.5);
b.sum(5,10);
c.sum(5.8,2.3);
a.show();
43
b.show();
c.show();
getch();
}
OUTPUT
44
PRACTICAL-20
20. Write separate programs to overload Unary ++ and Unary -- operators (Both using and without
using friend functions)
A) WITHOUT FRIEND
CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
class unary
{
int kilo,gram;
public:
unary()
{
kilo=0;
gram=0;
}
unary(int k,int g)
{
kilo=k;
gram=g;
}
void display()
{
cout<<kilo<<"kg"<<gram<<"g\n";
}
void operator ++()
{
kilo++;
gram++;
}
void operator --()
{
kilo--;
gram--;
}
};
void main()
{
clrscr();
45
unary u1(5,200);
unary u2(2,300);
cout<<"real weights\n";
u1.display();
u2.display();
u1++;
u2++;
cout<<"incremented weight\n";
u1.display();
u2.display();
u1--;
u2--;
cout<<"decremented weight\n";
u1.display();
u2.display();
getch();
}
OUTPUT
46
B)WITH FRIEND
CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
class unary
{
int kilo,gram;
public:
unary()
{
kilo=0;
gram=0;
}
unary(int k,int g)
{
kilo=k;
gram=g;
}
void display()
{
cout<<kilo<<"kg"<<gram<<"g\n";
}
friend void operator ++(unary &u1);
friend void operator --(unary &u1);
};
void operator ++(unary &u1)
{
u1.kilo++;
u1.gram++;
}
void operator --(unary &u1)
{
u1.kilo--;
u1.gram--;
}
void main()
{
clrscr();
unary u1(5,200);
47
unary u2(2,300);
unary u3(5,200);
unary u4(2,300);
cout<<"real weights\n";
u1.display();
u2.display();
++u1;
++u2;
cout<<"incremented weight\n";
u1.display();
u2.display();
cout<<"decremented weight\n";
--u3;
--u4;
u3.display();
u4.display();
getch();
}
OUTPUT
48
PRACTICAL-21
21. Write program to overload Binary + to concatenate two strings.(Both using and without using
friend functions)
A).WITHOUT FRIEND
CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size=81;
class concat
{
char str[size];
public:
concat()
{
strcpy(str,"");
}
concat(char s[])
{
strcpy(str,s);
}
void display()
{
cout<<str;
}
concat operator +(concat s1,concat s2)
{
concat temp;
strcpy(temp.str,s1.str);
strcat(temp.str,s2.str);
return temp;
}
};
void main()
{
clrscr();
concat s1="sahil";
concat s2=" pathak";
concat s3;
49
s3=s1+s2;
s3.display();
getch();
}
OUTPUT
50
B).WITH FRIEND
CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size=81;
class concat
{
char str[size];
public:
concat()
{
strcpy(str,"");
}
concat(char s[])
{
strcpy(str,s);
}
void display()
{
cout<<str;
}
friend concat operator +(concat s1,concat s2);
};
concat operator +(concat s1,concat s2)
{
concat temp;
strcpy(temp.str,s1.str);
strcat(temp.str,s2.str);
return temp;
}
void main()
{
clrscr();
concat s1="sahil";
concat s2=" pathak";
concat s3;
s3=s1+s2;
s3.display();
getch();
}
51
OUTPUT
52
PRACTICAL-22
CODE
#include<iostream.h>
#include<conio.h>
class binary
{
int kilo;
public:
binary()
{
kilo=0;
}
binary(int k)
{
kilo=k;
}
void display()
{
cout<<kilo<<"kg\n";
}
void operator +=(binary u2)
{
kilo+=u2.kilo;
}
};
void main()
{
clrscr();
binary u1(5);
binary u2(6);
cout<<"first weight\n";
u1.display();
cout<<"second weight\n";
53
u2.display();
u1+=u2;
cout<<"output weight\n";
u1.display();
getch();
}
OUTPUT
54
PRACTICAL-23
CODE
#include<iostream.h>
#include<conio.h>
class binary
{
int kilo;
public:
binary()
{
kilo=0;
}
binary(int k)
{
kilo=k;
}
void display()
{
cout<<kilo<<"kg\n";
}
void operator =(binary u2)
{
kilo=u2.kilo;
}
};
void main()
{
clrscr();
binary u1(5);
binary u2(6);
cout<<"first weight\n";
u1.display();
cout<<"second weight\n";
55
u2.display();
u1=u2;
cout<<"output weight\n";
u1.display();
getch();
}
OUTPUT
56
PRACTICAL-24
24. Implement the following class hierarchy where class person contains name and age of person and
student class contains enroll.no. of student and the worker class contains social security number of
worker.
CODE
#include<iostream.h>
#include<conio.h>
class person
{
char name[20];
int age;
public:
void enterdata()
{
cout<<"\nenter name: ";
cin>>name;
cout<<"\nenter age: ";
cin>>age;
}
void display()
{
cout<<"\nname: "<<name<<"\nage: "<<age;
}
};
class student:virtual public person
{
int enrollno;
public:
void enterdata()
{
cout<<"\nenter enrollment number: ";
cin>>enrollno;
}
void display()
57
{
cout<<"\nenrollment number: "<<enrollno;
}
};
class worker:virtual public person
{
int ssnumber;
public:
void enterdata()
{
cout<<"\nenter social security number: ";
cin>>ssnumber;
}
void display()
{
cout<<"\nsocial security number: "<<ssnumber;
}
};
class student_worker:public student,public worker
{
int worker_wages,stud_hrs_studied;
public:
void enterdata()
{
cout<<"\nenter wage: ";
cin>>worker_wages;
cout<<"\nenter no of study hours: ";
cin>>stud_hrs_studied;
}
void display()
{
cout<<"\nno of hours studied: "<<stud_hrs_studied<<"\nwage of worker
: "<<worker_wages;
}
};
void main()
{
clrscr();
student_worker sw1;
sw1.person::enterdata();
sw1.student::enterdata();
sw1.worker::enterdata();
58
sw1.enterdata();
sw1.person::display();
sw1.student::display();
sw1.worker::display();
sw1.display();
getch();
}
OUTPUT
59
PRACTICAL-25
25. Declare two classes named NewWindow and door. Serve a new class called house from those
two classes. Take appropriate functions and data members to show the relationship.
CODE
#include<iostream.h>
#include<conio.h>
class newwindow
{
int winno;
public:
void enterdata()
{
cout<<"\nenter no of windows: ";
cin>>winno;
}
void display()
{
cout<<"\nno of windows: "<<winno;
}
};
class door
{
int doorno;
public:
void enterdata()
{
cout<<"\nenter no of doors: ";
cin>>doorno;
}
void display()
{
cout<<"\nno of doors: "<<doorno;
}
};
class house:public newwindow,public door
{
int length,breadth,height;
float area1;
public:
60
void enterdata()
{
newwindow::enterdata();
door::enterdata();
cout<<"\nenter length,breadth and height: ";
cin>>length>>breadth>>height;
}
void area()
{
area1=length*breadth*height;
}
void display()
{
newwindow::display();
door::display();
cout<<"\narea of house : "<<area1<<"sqfeet\n";
}
};
void main()
{
clrscr();
house h;
h.enterdata();
h.area();
h.display();
getch();
}
OUTPUT
61
PRACTICAL-26
26. Implement the following class hierarchy considering appropriate data members and member
functions.
CODE
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
char name[20];
public:
void readdata()
{
cout<<"\nEnter the roll number of the sudent: ";
cin>>rno;
cout<<"\nEnter the name of the student: ";
cin>>name;
}
void display()
{
cout<<"\nRoll Number: "<<rno;
cout<<"\nName: "<<name;
}
};
class test : virtual public student
{
int marks1, marks2,marks3;
public:
void readdata()
62
{
cout<<"\nEnter marks 1: ";
cin>>marks1;
cout<<"\nEnter marks 2: ";
cin>>marks2;
cout<<"\nEnter marks 3: ";
cin>>marks3;
}
void display()
{
cout<<"\nSubject 1: "<<marks1;
cout<<"\nSubject 2: "<<marks2;
cout<<"\nSubject 3: "<<marks3;
cout<<"\nTotal score: "<<total();
}
int total()
{
return marks1+marks2+marks3;
}
};
class sports : virtual public student
{
char sport[10];
int score;
public:
void readdata()
{
cout<<"\nEnter the sport played by the student: ";
cin>>sport;
cout<<"\nEnter score earned in "<<sport<<" : ";
cin>>score;
}
int tscore()
{
return score;
}
void display()
{
cout<<"\nSport : "<<sport;
cout<<"\nscore in "<<sport<<" : "<<score;
63
}
};
class performance : public test, public sports
{
public:
int total_points()
{
return total()+tscore();
}
};
void main()
{
clrscr();
performance p;
p.student::readdata();
p.test::readdata();
p.sports::readdata();
p.student::display();
p.test::display();
p.sports::display();
cout<<"\nTotal Score: "<<p.total_points()<<" out of 400";
getch();
}
OUTPUT
64
PRACTICAL-27
27. Implement the following hierarchy considering appropriate data members and member functions
(use Virtual functions).
CODE
#include<iostream.h>
#include<conio.h>
class shape
{
int sides;
char colour[20];
public:
void enterdata()
{
cout<<"enter no of sides: ";
cin>>sides;
cout<<"enter color of shape: ";
cin>>colour;
}
void display()
{
cout<<"\nsides: "<<sides<<"\ncolor: "<<colour;
}
};
class triangle:virtual public shape
{
float b,h,a_t;
public:
void enterdata()
{
shape::enterdata();
cout<<" enter base and height: ";
cin>>b>>h;
65
}
void display()
{
shape::display();
cout<<"\narea of triangle : "<<area();
}
int area()
{
return a_t=(b*h)/2;
}
};
class rectangle:virtual public shape
{
float br,l,a_r;
public:
void enterdata()
{
shape::enterdata();
cout<<" enter length and breadth: ";
cin>>l>>br;
}
void display()
{
shape::display();
cout<<"\narea of rectangle : "<<area();
}
int area()
{
return a_r=l*br;
}
};
class circle:virtual public shape
{
float pi,r,a_c;
public:
void enterdata()
{
shape::enterdata();
cout<<" enter radius: ";
cin>>r;
pi=3.14;
}
void display()
{
66
shape::display();
cout<<"\narea of circle : "<<area();
}
int area()
{
return a_c=pi*r*r;
}
};
void main()
{
clrscr();
circle c;
triangle t;
rectangle r;
c.enterdata();
r.enterdata();
t.enterdata();
c.display();
r.display();
t.display();
getch();
}
OUTPUT
67
PRACTICAL-28
28. Create a vehicle class hierarchy with top most base having the following specifications:
Data Members: int reg_no, int cost
Member Functions: virtual void start () = 0; virtual void stop ()=0; virtual void show ();
Write a complete program having derived classes such as heavy, lightweight vehicle etc.
CODE
#include<iostream.h>
#include<conio.h>
class vehicle
{
int regno,cost;
public:
virtual void start()=0;
virtual void stop()=0;
virtual void show()
{
cout<<"\nenter register number: "<<regno<<"\nenter cost: "<<cost;
}
vehicle()
{
cout<<"enter register number: ";
cin>>regno;
cout<<"enter cost: ";
cin>>cost;
}
};
class lightweight:public vehicle
{
int speedlimit;
public:
virtual void start()
{
cout<<"\nvehicle starts";
}
virtual void stop()
{
cout<<"\nvehicle stops";
68
}
virtual void show()
{
vehicle::show();
cout<<"\nspeed limit: "<<speedlimit;
}
lightweight()
{
cout<<"enter speed limit: ";
cin>>speedlimit;
}
};
class heavyweight:public vehicle
{
int loadcapacity;
public:
virtual void start()
{
cout<<"\nvehicle starts";
}
virtual void stop()
{
cout<<"\nvehicle stops";
}
virtual void show()
{
vehicle::show();
cout<<"\nload capacity: "<<loadcapacity;
}
heavyweight()
{
cout<<"enter load capacity: ";
cin>>loadcapacity;
}
};
class gearmotor:public lightweight
{
int noofgears;
public:
virtual void start()
{
cout<<"\nvehicle starts";
}
69
virtual void stop()
{
cout<<"\nvehicle stops";
}
virtual void show()
{
lightweight::show();
cout<<"\nno of gears: "<<noofgears;
}
gearmotor()
{
cout<<"enter no of gears: ";
cin>>noofgears;
}
};
class nongearmotor:public lightweight
{
public:
virtual void start()
{
cout<<"\nvehicle starts";
}
virtual void stop()
{
cout<<"\nvehicle stops";
}
virtual void show()
{
lightweight::show();
}
nongearmotor();
};
class passengers:public heavyweight
{
int noofpass;
public:
virtual void start()
{
cout<<"\nvehicle starts";
}
70
virtual void stop()
{
cout<<"\nvehicle stops";
}
virtual void show()
{
heavyweight::show();
cout<<"\nno of passengers: "<<noofpass;
}
passengers()
{
cout<<"enter no of passengers: ";
cin>>noofpass;
}
};
class goods:public heavyweight
{
public:
virtual void start()
{
cout<<"\nvehicle starts";
}
virtual void stop()
{
cout<<"\nvehicle stops";
}
virtual void show()
{
heavyweight::show();
}
goods();
};
void main()
{
clrscr();
passengers p1;
p1.start();
p1.show();
71
p1.stop();
gearmotor g1;
g1.start();
g1.show();
g1.stop();
getch();
}
OUTPUT
72
PRACTICAL-29
29. WAP to show the implementation of ‘containership’.
CODE
#include <iostream.h>
#include<conio.h>
class first {
public:
void show()
{
cout << "Hello from first class\n";
}
};
class second {
first f;
public:
second()
{
f.show();
}
};
void main()
{
clrscr();
second s;
getch();
}
OUTPUT
73
PRACTICAL-30
30. Implement the following data structures using C++: (You can also use templates)
Linked List, Stack and Queue
STACK
CODE
#include <iostream.h>
#include<conio.h>
template <class T>
class Stack
{
private:
int top;
T st[100];
public:
Stack();
void push(T i);
T pop();
void display();
};
template <class T>
Stack<T>::Stack()
{
top = -1;
}
template <class T>
void Stack<T>::push(T i)
{
st[++top] = i;
}
template <class T>
T Stack<T>::pop()
{
return st[top--];
}
template <class T>
void Stack<T>::display()
{
cout<<"\nstack is ";
for (int i = 0; i <= top; i++)
{
cout<<st[i]<<"\t";
74
}
}
void main ()
{
clrscr();
Stack<int> st1;
st1.push(34);
st1.push(39);
st1.push(38);
st1.push(67);
st1.display();
st1.pop();
st1.display();
getch();
}
OUTPUT
75
QUEUE
CODE
#include <iostream.h>
#include<conio.h>
template <class T>
class queue
{
private:
int front,rear;
T q[100];
public:
queue();
void insert(T i);
T remove();
void display();
};
template <class T>
queue<T>::queue()
{
rear = -1;
front=0;
}
template <class T>
void queue<T>::insert(T i)
{
q[++rear] = i;
}
template <class T>
T queue<T>::remove()
{
return q[front++];
}
template <class T>
void queue<T>::display()
{
cout<<"\nqueue is ";
for (int i = front; i <= rear; i++)
{
cout<<q[i]<<"\t";
}
}
void main ()
{
76
clrscr();
queue<int> q1;
q1.insert(34);
q1.insert(39);
q1.insert(38);
q1.insert(67);
q1.display();
q1.remove();
q1.display();
getch();
}
OUTPUT
77
LINKED LIST
CODE
#include <iostream.h>
#include<conio.h>
template <class T>
class linklist
{
private:
int front,rear;
T ll[100];
public:
linklist();
void insert(T i);
void remove();
void display();
};
template <class T>
linklist<T>::linklist()
{
rear = -1;
front=0;
}
template <class T>
void linklist<T>::insert(T i)
{
ll[++rear] = i;
}
template <class T>
void linklist<T>::remove()
{
for(int k=front; k<=rear; k++)
{
ll[k]=ll[k+1];
}
rear--;
}
template <class T>
void linklist<T>::display()
{
cout<<"\nlinked list is ";
for (int i = front; i <= rear; i++)
{
cout<<ll[i]<<"\t";
78
}
}
void main ()
{
clrscr();
linklist<int> ll1;
ll1.insert(34);
ll1.insert(39);
ll1.insert(38);
ll1.insert(67);
ll1.display();
ll1.remove();
ll1.display();
getch();
}
OUTPUT
79
PRACTICAL-31
CODE
#include <iostream.h>
#include<conio.h>
template <class T>
void swap(T x,T y)
{
cout<<"\n before swap: "<<x<<"\t"<<y;
T temp;
temp=x;
x=y;
y=temp;
cout<<"\n after swap : "<<x<<"\t"<<y;
}
void main()
{
clrscr();
swap(10,5);
swap(11.3,7.4);
swap("a","b");
getch();
}
OUTPUT
80