You are on page 1of 104

VIVEKANANDA INSTITUTE OF PROFESSIONAL STUDIES

VIVEKANANDA SCHOOL OF INFORMATION TECHNOLOGY

BACHELOR OF COMPUTER APPLICATION


OOP FILE

Guru Gobind Singh Indraprastha University 


Sector - 16C, Dwarka, Delhi - 110078

SUBMITTED TO: SUBMITTED BY:


Dr. Neha Verma Malhotra Himanshu Tandon
Assistant Professor 08429802019
VSIT BCA-III (EB)

INDEX
Page | 1
Himanshu Tandon
08429802019
BCA 3EB
S.NO. Pg. No. Date Teacher’s
PROGRAM
Signature
Pick a small program that you would 6-7 22-08-20
like to solve via C++ program. Please
make the solution to the problem
contain the following attributes:
At least one function which takes in
1. arguments and/or return values.
Input from the user into a variable
using the cin construct.
Output to the user using the cout
construct
Write a program (WAP) that asks the 8-9 22-0820
user to enter a temperature in degrees
2. Fahrenheit, and then prints out the
equivalent temperature in degrees
Celsius.
Write a function called factorial that 10-11 29-08-20
returns the factorial of any positive
3. whole number. Your function is
required to use recursion.
WAP to implement student result. 12-17 29-08-20
4. [Hint: implement percentage () etc.]
WAP to implement friend function by 18-19 05-09-20
5 taking some real life example.
Write a function twodigitreverse() that 20-21 05-09-20
6 reverse the digit of a two-digit integer.
7 Write a class called CAccount which 22-27 12-09-20
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

Page | 2
Himanshu Tandon
08429802019
BCA 3EB
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.

Create a class employee which have 28-29 12-09-20


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
8
display the data in following format:
Name:
Age:
Address:
Define a class counter which contains 30-31 19-09-20
an int variable count defined as static
and a static function Display () to
display the value of count. Whenever
9 an object of this class is created count is
incremented by 1. Use this class in main
to create multiple objects of this class
and display value of count each time.

Write a function power () to raise n to 32-33 19-09-20


the power p which takes a double value
for n and int for p, and returns the
10 result as double. Use default argument
2 for p. Write a main() to test the
program.
WAP to multiply two complex 34-35 26-09-20
11 numbers.
Page | 3
Himanshu Tandon
08429802019
BCA 3EB
WAP to add two Minute and Hour 36-38 26-09-20
12 values.
WAP to implement call by reference by 39-40 03-10-20
13 creating Account class. [Hint. Assume
necessary functions]
WAP to implement Array of objects 41-43 03-10-20
14 using suitable problem statement.
WAP to implement operator 44-45 13-10-20
15 overloading with string class functions
(strcpy, strconcat etc.)
16 WAP to implement ‘Inline function’. 46-47 21-10-20
17 WAP to show the usage of ‘Destructor’. 48-49 27-10-20

WAP to implement Copy Constructor 50-51 27-10-20


18 to copy one string to another
WAP to implement Overloading of 52-53 05-11-20
19 Function.
Write separate programs to overload 54-60 05-11-20
Unary ++ and Unary -- operators (Both
20 using and without using friend
functions)
Write program to overload Binary + to 61-64 12-11-20
21 concatenate two strings.(Both using and
without using friend functions)

22 WAP to implement += operator. 65-66 12-11-20

23 WAP to implement = operator. 67-68 23-11-20

Implement the following class 69-73 23-11-20


hierarchy where class person contains
name and age of person and student
24 class contains enroll.no. of student and
the worker class contains social security
number of worker.
Declare two classes named 74-75 29-11-20
NewWindow and door. Serve a new
25 class called house from those two
classes. Take appropriate functions and
data members to show the relationship.

Page | 4
Himanshu Tandon
08429802019
BCA 3EB
Implement the following class 76-80 04-12-20
26 hierarchy considering appropriate data
members and member functions.
Implement the following hierarchy 81-85 04-12-20
considering appropriate data members
27 and member functions (use Virtual
functions).
Create a vehicle class hierarchy with 86-90 07-12-20
top most base having the following
specifications:
Data Members: int reg_no, int cost
Member Functions:
virtual void start () = 0;
28
virtual void stop ()=0;
virtual void show ();
Write a complete program having
derived classes such as heavy,
lightweight vehicle etc.

WAP to show the implementation of 91-92 07-12-20


29
‘containership’.
Implement the following data 93-101 14-12-20
structures using C++: (You can also use
30 templates)
Linked List, Stack and Queue
WAP to show swapping using template 102-103 14-12-20
31 function (Generic)

PRACTICAL #1
Page | 5
Himanshu Tandon
08429802019
BCA 3EB
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
Ans:
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>

int addition(int,int);
void display_add();

void main()
{
clrscr();
display_add();
}

void display_add()
{
int num1,num2;
int sum = 0;
char ch;
clrscr();
cout<<"\n\n\t\t THIS PROGRAM CALCULATES THE SUM OF 2 NUMBERS";
cout<<"\n\n\n\n\t\tEnter the 1st number: ";
cin>>num1;
cout<<"\n\n\t\tEnter the 2nd number: ";
cin>>num2;
sum = addition(num1,num2);
cout<<"\n\n\t\tThe addition of "<<num1<<" and "<<num2<<" is: "<<sum;
cout<<"\n\n\n\t\tPress 'ENTER' to repeat";
cout<<"\n\n\t\tPress any other key to exit";
ch=getch();
if(ch == 13)
display_add();
else
exit(0);
}

Page | 6
Himanshu Tandon
08429802019
BCA 3EB
int addition(int num1, int num2)
{
return num1 + num2;
}

Output

PRACTICAL #2

Page | 7
Himanshu Tandon
08429802019
BCA 3EB
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.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void fahrenheit();
void main()
{
clrscr();
fahrenheit();
getch();
}
void fahrenheit()
{
clrscr();
cout<<"\n\n\n\t\t\tConvert celsius to fahrenheit";
float cel;
double fare;
cout<<"\n\n\n\tEmter temperature in clesius: ";
cin>>cel;
fare = cel * 9/5 + 32;
cout<<"\n\n\tTemparature in fahrenheit is: "<<fare;
}

Page | 8
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 9
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #3
3. Write a function called factorial that returns the factorial of any positive
whole number. Your function is required to use recursion.

Ans.
Source Code
#include<conio.h>
#include<stdio.h>
#include<iostream.h>

float factorial(int);

void main()
{
int n,i,result,x=29;
clrscr();
gotoxy(30,4);
cout<<"\n\t\t\t----- FACTORIAL PRINTING -----";
gotoxy(13,8);
cout<<"Enter number of which you want to find factorial: ";
cin>>n;
result = factorial(n);
gotoxy(24,11);
cout<<n<<"! = ";
do{
gotoxy(x,11);
cout<<n<<" * ";
x+=4;
n--;
if(n==1)
{
gotoxy(x,11);
cout<<"1 = "<<result;
}
else
{
continue;
}
}while(n!=1);
getch();
}

float factorial(int n)

Page | 10
Himanshu Tandon
08429802019
BCA 3EB
{
if((n==0)||(n==1))
{
return 1;
}
else
return(n*factorial(n-1));
}

Output

Page | 11
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #4
4. WAP to implement student result. [Hint: implement percentage () etc.]

Ans. Source Code


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<dos.h>

int marks[10];

class student
{
private:

long int rollno;


float avg,perc;
int subnum,age;
int sum;
char name[30];

public:

void enterdata()
{
clrscr();
int i;
int y = 16;
gotoxy(25,3);
cout<<"---- ENTER STUDENT DETAILS ---- ";

gotoxy(27,6);
cout<<"Enter name: ";
gotoxy(27,8);
cout<<"Enter roll number: ";
gotoxy(27,10);
cout<<"Enter Age: ";

gotoxy(47,6);
gets(name);
gotoxy(47,8);
cin>>rollno;

Page | 12
Himanshu Tandon
08429802019
BCA 3EB
gotoxy(47,10);
cin>>age;

gotoxy(27,12);
cout<<"Marks Obtained [Out of 100]";
gotoxy(26,13);
cout<<"----------------------------";
gotoxy(20,14);
cout<<"Enter the number of subjects: ";
gotoxy(51,14);
cin>>subnum;
for(i=1;i <= subnum;i++)
{
gotoxy(20,y);
cout<<"Enter the marks of student of subject "<<i<<": ";
cin>>marks[i];
y+=2;
}
}
void display()
{
clrscr();
int i;
int y = 14;
gotoxy(25,3);
cout<<"---- DISPLAY STUDENT DETAILS ---- ";
gotoxy(27,6);
cout<<"Student name: ";
gotoxy(27,8);
cout<<"Student roll number: ";
gotoxy(27,10);
cout<<"Student Age: ";

gotoxy(48,6);
cout<<name;
gotoxy(48,8);
cout<<rollno;
gotoxy(48,10);
cout<<age;

gotoxy(27,12);
cout<<"Marks Obtained [Out of 100]";
gotoxy(26,13);
cout<<"----------------------------";
for(i=1;i <= subnum;i++)
{
gotoxy(20,y);
cout<<"Marks of "<<name<<" of subject "<<i<<" are : ";
cout<<marks[i];
y+=2;
Page | 13
Himanshu Tandon
08429802019
BCA 3EB
}
getch();
}
void calc()
{
clrscr();
sum = 0;
avg = 0;
perc = 0;
int total = 0;
int i;
int y = 8;
gotoxy(25,3);
cout<<"---- Percentage of marks ---- ";
for(i=1;i<= subnum;i++)
{
sum += marks[i];
}
perc = (sum / subnum);
gotoxy(27,6);
cout<<"Marks Obtained [Out of 100]";
gotoxy(26,7);
cout<<"----------------------------";
for(i=1;i <= subnum;i++)
{
gotoxy(20,y);
cout<<"Marks of "<<name<<" of subject "<<i<<" are : ";
cout<<marks[i];
y+=2;
}
y+=2;
gotoxy(20,y);
cout<<"The percentage obtained by "<<name<<" is : "<<perc<<" %";
y+=2;
if(perc >= 33)
{
for(i=0; i<=5;i++)
{
gotoxy(34,y);
cout<<"P A S S E D !!!";
delay(1000);
gotoxy(34,y);
cout<<" ";
delay(500);
}
gotoxy(34,y);
cout<<"P A S S E D !!!";
}
else
{
Page | 14
Himanshu Tandon
08429802019
BCA 3EB
for(i=0; i<=5;i++)
{
gotoxy(34,y);
cout<<"F A I L E D !!!";
delay(1000);
gotoxy(34,y);
cout<<" ";
delay(500);
}
gotoxy(34,y);
cout<<"F A I L E D !!!";
}

getch();
}
};

void mainmenu()
{
clrscr();
student s;
char ch;
int h = 1;
int y = 9;
do{
clrscr();
gotoxy(35,6);
cout<<"| MAIN MENU |";
gotoxy(33,7);
cout<<"_________________";
gotoxy(23,y);
cout<<"þ";
gotoxy(25,9);
cout<<"Enter student details and marks";
gotoxy(25,11);
cout<<"Display student details and marks";
gotoxy(25,13);
cout<<"Calculate and Display percentage of student marks";
gotoxy(25,15);
cout<<"Exit the program";

ch = getch();
if(ch == 80)
{
h++;
y += 2;
}
else if(ch == 72)
{
h--;
Page | 15
Himanshu Tandon
08429802019
BCA 3EB
y -= 2;
}
if(h > 4)
{
h = 1;
y = 9;
}
else if(h < 1)
{
h = 4;
y = 15;
}
if(ch == 13)
{
switch(h)
{
case 1: s.enterdata();
break;

case 2: s.display();
break;

case 3: s.calc();
break;

case 4: exit(0);
}
}
}while(ch!=13 || h!=4);
}

void main()
{
clrscr();
mainmenu();
getch();
}

Page | 16
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 17
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #5
5. WAP to implement friend function by taking some real-life example.

Ans. Source Code


#include<iostream.h>
#include<conio.h>

class Distance
{
private:

int meter;
friend int addDist(Distance);

public:

Distance()
{
meter = 0;
}
};

int addDist(Distance d)
{
d.meter += 6;
return d.meter;
}

void main()
{
clrscr();
cout<<"\n\n\n\t\t\t Friend Function";
Distance D;
cout<<"\n\n\t\t Distance using friend function is: "<<addDist(D);
getch();
}

Page | 18
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 19
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #6
6. Write a function twodigitreverse() that reverse the digit of a two-digit
integer.

Ans. Source Code


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class reverse
{
private:

int digit,temp;
int reverse;

public:

void reverse2()
{
clrscr();
reverse = 0;
cout<<"\n\n\n\t\t\tREVERSE 2 DIGIT NUMBER";
cout<<"\n\n\n\tEnter the 2 digit number: ";
cin>>digit;
temp = digit;
if(digit > 9 && digit < 100)
{
while(digit != 0)
{
reverse = reverse * 10;
reverse = reverse + (digit % 10);
digit = digit / 10;
}
cout<<"\n\n\tThe reversal of "<<temp<<" is: "<<reverse;
}
else
{
cout<<"\n\n\tPlease enter a two digit number..!!";
getch();
reverse2();
}
getch();
}

Page | 20
Himanshu Tandon
08429802019
BCA 3EB
};

void main()
{
clrscr();
reverse r;
r.reverse2();

Output

Page | 21
Himanshu Tandon
08429802019
BCA 3EB
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.

Ans. Source Code


#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class CAccount

Page | 22
Himanshu Tandon
08429802019
BCA 3EB
{
private:
long int accnum;
float accbal;
public:
CAccount()
{
clrscr();
cout<<"\BANK DETAILS";
cout<<"\nENTER THE ACCOUNT NUMNBER: ";
cin>>accnum;
cout<<"\nENTER THE INITIAL ACCOUNT BALANCE: ";
cin>>accbal;
}
CAccount(long int num, float bal)
{
accnum = num;
accbal = bal;
}
void inputTransaction()
{
clrscr();
long int amtd,amtw;
char transactionType;
cout<<"\n TRANSACTONS";
cout<<"\nEnter D for Deposit or W for Withdrawl: ";
cin>>transactionType;
if(transactionType == 'D')
{
Page | 23
Himanshu Tandon
08429802019
BCA 3EB
cout<<"\nENTER THE AMOUNT FOR DEPOSIT: ";
cin>>amtd;
accbal = accbal + amtd;
}
else if(transactionType == 'W')
{
cout<<"\nENTER THE AMOUNT FOR WITHDRAWL: ";
cin>>amtw;
accbal = accbal - amtw;
}
else
{
cout<<"\nPLEASE ENTER THE RIGTH CHOICE..!!";
getch();
inputTransaction();
}
}
void printBalance()
{
clrscr();
cout<<"\nBALANCE INFORMATION";
cout<<"\nACCOUNT NUMBER: "<<accnum;
cout<<"\nCURRENT ACCOUNT BALANCE: "<<accbal;
getch();
}
};
void main()
{
CAccount c;
Page | 24
Himanshu Tandon
08429802019
BCA 3EB
c.inputTransaction();
c.printBalance();
}

Output

Page | 25
Himanshu Tandon
08429802019
BCA 3EB
Page | 26
Himanshu Tandon
08429802019
BCA 3EB
Page | 27
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #8
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 display the data in
following format:
Name:
Age:
Address:

Ans.
Source Code

#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
class employee
{
char name[30];
int age;
char address[50];
public:
Page | 28
Himanshu Tandon
08429802019
BCA 3EB
void getdata()
{
cout<<"Enter name:\n";
gets(name);
cout<<"Enter age:\n";
cin>>age;
cout<<"Enter address:\n";
gets(address);
}
void showdata()
{
cout<<"Name:\n "<<name<<endl;
cout<<"Age:\n "<<age<<endl;
cout<<"Address:\n "<<address<<endl;
}
};
void main()
{
clrscr();
employee e1;
ofstream file;
file.open("data.txt",ios::out);
cout<<"File created successfully"<<endl;
e1.getdata();
file.write((char*)&e1,sizeof(e1));
file.close();
cout<<"File created and saved successfully"<<endl;
ifstream file1;
file1.open("data.txt",ios::in);
file1.read((char*)&e1,sizeof(e1));
e1.showdata();
file1.close();
getch();
}

Output

Page | 29
Himanshu Tandon
08429802019
BCA 3EB
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 incremented by 1. Use
this class in main to create multiple objects of this class and display value
of count each time.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
class counter
{
private:
static int i;

public:
static void count()
Page | 30
Himanshu Tandon
08429802019
BCA 3EB
{

i++;
}
static void display()
{
cout<<"no of object are = "<<i<<"\n";
}
};
int counter::i=0;
void main()
{
clrscr();
counter x,y,z;
x.count();
x.display();
y.count();
y.display();
z.count();
z.display();
getch();
}

Output

Page | 31
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #10
Page | 32
Himanshu Tandon
08429802019
BCA 3EB
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.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

long power(int, double);

long power(double n, int p = 2)


{
int sol = 1;
int i;
i = p;
while(i != 0)
{
sol = sol * n;
i--;
}
return sol;
}

void main()
{
clrscr();
float sum;
int p;
double n;
cout<<"\n\n\n\t\t\t\tRaise N to power P";
cout<<"\n\n\t\tEnter the number to be raised: ";
cin>>n;
cout<<"\n\t\tEnter the power: ";
cin>>p;
sum = power(n,p);
cout<<"\n\n\t\t"<<n<<" ^ "<<p<<" = "<<sum;
getch();
}

Output

Page | 33
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #11
Page | 34
Himanshu Tandon
08429802019
BCA 3EB
11. WAP to multiply two complex numbers.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class complex
{
private:

int x1,y1;
int x2,y2;
int x3,y3;

public:

void getdata()
{
clrscr();
cout<<"\n\n\n\t\t\tMULTIPLY 2 COMPLEX NUMBERS";
cout<<"\n\n\n\t Enter the first complex number: ";
gotoxy(42,7);
cin>>x1;
gotoxy(44,7);
cout<<"+";
gotoxy(46,7);
cin>>y1;
gotoxy(47,7);
cout<<"i";

cout<<"\n\n\t Enter the second complex number: ";


gotoxy(43,9);
cin>>x2;
gotoxy(45,9);
cout<<"+";
gotoxy(47,9);
cin>>y2;
gotoxy(48,9);
cout<<"i";
}

void multiply()
{
x3 = x1 * x2 - y1 * y2;
y3 = x1 * y2 + y1 * x2;
gotoxy(10,12);
cout<<"("<<x1<<" + "<<y1<<"i) X ("<<x2<<" + "<<y2<<"i) = "<<x3<<" + "<<y3<<"i";
Page | 35
Himanshu Tandon
08429802019
BCA 3EB
}

};

void main()
{
complex c;
c.getdata();
c.multiply();
getch();
}
Output

PRACTICAL #12
Page | 36
Himanshu Tandon
08429802019
BCA 3EB
12. WAP to add two Minute and Hour values.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class time
{
private:

int h1,m1,s1;
int h2,m2,s2;
int h3,m3,s3;

public:

void getdata()
{
clrscr();
cout<<"\n\n\n\t\t\tADD 2 DIFFERENT TIMES";
cout<<"\n\n\n\t Enter the first time: ";
gotoxy(32,7);
cin>>h1;
gotoxy(34,7);
cout<<":";
gotoxy(35,7);
cin>>m1;
gotoxy(37,7);
cout<<":";
gotoxy(38,7);
cin>>s1;

cout<<"\n\n\t Enter the second time: ";


gotoxy(33,10);
cin>>h2;
gotoxy(35,10);
cout<<":";
gotoxy(36,10);
cin>>m2;
gotoxy(38,10);
cout<<":";
gotoxy(39,10);
cin>>s2;
}

void add()

Page | 37
Himanshu Tandon
08429802019
BCA 3EB
{
s3 = s1 + s2;
m3 = m1 + m2 + (s3 / 60);
h3 = h1 + h2 + (m3 / 60);
m3 = m3 % 60;
s3 = s3 % 60;
gotoxy(10,15);
cout<<"Total time = "<<h3<<" hrs "<<m3<<" min "<<s3<<" sec";
}

};

void main()
{
time t;
t.getdata();
t.add();
getch();
}

Output

PRACTICAL #13
Page | 38
Himanshu Tandon
08429802019
BCA 3EB
13. WAP to implement call by reference by creating Account class. [Hint.
Assume necessary functions]

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
class account
{
private:
int number;
float balance;
public:
account(int &accno,float &accbal)
{
number=accno;
balance=accbal;
}
void display()
{
cout<<"account number= "<<number<<"\n";
cout<<"account balance= "<<balance<<"\n";
}
};

void main()
{
clrscr();
account a(7,1000);
a.display();
getch();
}

Page | 39
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 40
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #14
14. WAP to implement Array of objects using suitable problem statement.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class student
{
private:

char name[30];
int marks;

public:

void getname()
{
cin>>name;
}

void getmarks()
{
cin>>marks;
}

void display()
{
cout<<"\n\n\t\t\tName of student: ";
cout<<name;
cout<<"\n\t\t\tTotal marks (out of 100): ";
cout<<marks;
cout<<"\n";
}
};

void main()
{
clrscr();
int size;
cout<<"\n\n\t\t\t ARRAY OF OBJECTS";
cout<<"\n\t\t\t (input data)";
cout<<"\n\n\t\t Enter the number of students: ";
Page | 41
Himanshu Tandon
08429802019
BCA 3EB
cin>>size;
student st[5];
for(int i = 0; i < size; i++)
{
cout<<"\n\t\t STUDENT "<<i + 1;
cout<<"\n\n\t\t Enter the name of student: ";
st[i].getname();
cout<<"\t\t Enter total marks (out of 100): ";
st[i].getmarks();
}
clrscr();
cout<<"\n\n\n\t\t\t ARRAY OF OBJECTS";
cout<<"\n\t\t\t (display data)";

for(int j = 0; j < size; j++)


{
cout<<"\n\n\t\t\tSTUDENT "<<j + 1;
st[j].display();
}
getch();
}

Output

Page | 42
Himanshu Tandon
08429802019
BCA 3EB
Page | 43
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #15
15. WAP to implement operator overloading with string class functions (strcpy,
strconcat etc.)

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size = 100;
class concat
{
char str[size];
public:
concat ()
{
strcpy (str, "");

}
concat (char s[])
{
strcpy (str, s);
}
void display ()
{
cout << str;
}
friend concat operator + (concat c1, concat c2);
};
concat operator + (concat c1, concat c2)
{
Page | 44
Himanshu Tandon
08429802019
BCA 3EB
concat temp;
strcpy (temp.str, c1.str);
strcat (temp.str, c2.str);
return temp;
}
void main ()
{
clrscr ();
concat c1 = "Himanshu ";
concat c2 = "Tandon";
concat c3;
c3 = c1 + c2;
c3.display ();
getch ();
}

Page | 45
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 46
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #16
16. WAP to implement ‘Inline function’.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class inlineFun
{
private:

int a,b,sumtotal;

public:

void getdata();
void sum();
};

inline void inlineFun::getdata()


{
clrscr();
cout<<"\n\n\t\t\t INLINE FUNCTION";
cout<<"\n\t\t\t (input data)";
cout<<"\n\n\n\t\t Enter the first digit: ";
cin>>a;
cout<<"\n\t\t Enter the second digit: ";
cin>>b;
}

inline void inlineFun::sum()


{
sumtotal = a + b;
cout<<"\n\n\t\t\t INLINE FUNCTION";
cout<<"\n\t\t\t (display data)";
cout<<"\n\n\n\t\t The sum of "<<a<<" and "<<b<<" = "<<sumtotal;
}

void main()
{
inlineFun s;
s.getdata();
s.sum();
Page | 47
Himanshu Tandon
08429802019
BCA 3EB
getch();
}

Output

Page | 48
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #17
17. WAP to show the usage of ‘Destructor’.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class dest
{
public:

dest()
{
cout<<"\n\n\n\n\t\t\t Hey..!! I am in constructor";
}

~dest()
{
cout<<"\n\n\t\t\t Hey..!! I am in destructor";
getch();
}

};

void main()
{
clrscr();
cout<<"\n\n\t\t\t DESTRUCTOR IMPLEMENTATION";
dest d;
cout<<"\n\n\t\t\t Program is terminating..!!";
}

Page | 49
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 50
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #18
18. WAP to show the usage of ‘Destructor’.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>

class copy
{
public:

int orig;
int copyval;

copy(int a)
{
orig = a;
}

copy(copy &i)
{
copyval = i.orig;
}
};

void main()
{
clrscr();
int value;
cout<<"\n\n\n\t\t\t COPY CONSTRUCTOR";
cout<<"\n\n\n\t\tEnter the value in parameterised constructor: ";
cin>>value;
copy c1(value);
copy c2(c1);
cout<<"\n\n\n\t\t The copied value using copy constructor is "<<c2.copyval;
getch();
}

Page | 51
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 52
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #19
19. WAP to implement Overloading of Function.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>

void sum(int,int);
void sum(int, int, int);
void sum(float, double);

void sum(int x,int y)


{
cout<<"\n\n\n\t\t Default Function";
cout<<"\n\n\t\t The sum of "<<x<<" and "<<y<<" is "<<x + y;
}

void sum(int a,int b,int c)


{
cout<<"\n\n\n\t\t Overloading by parameter";
cout<<"\n\n\t\t The sum of "<<a<<", "<<b<<" and "<<c<<" is "<<a + b + c;
}

void sum(float p,double q)


{
cout<<"\n\n\n\t\t Overloading by different data type";
cout<<"\n\n\t\t The sum of "<<p<<" and "<<q<<" is "<<p + q;
getch();
}

void main()
{
clrscr();
cout<<"\n\n\n\t\t\t FUNCTION OVERLOADING";
sum(10,20);
sum(10,20,30);
sum(10.5,9.5);
}

Output
Page | 53
Himanshu Tandon
08429802019
BCA 3EB
Page | 54
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #20
20. Write separate programs to overload Unary ++ and Unary -- operators (Both
using and without using friend functions)

Ans.
Source Code

#include<iostream.h>
#include<conio.h>
class check
{
private:
int i;
public:
check()
{
i= 11;
}
void operator ++()
{
++i;
}
void operator --()
{
--i;
}
void display()
{

Page | 55
Himanshu Tandon
08429802019
BCA 3EB
cout<<"\n i = " <<i<<endl;
}
};
void main()
{
clrscr();
cout<<"\t UNARY OPERATOR OVERLOADING";
check obj;
obj.display();
cout<<"\n INCREMENT OPERATOR";
++obj;
obj.display();
cout<<"\n DECREMENT OPERATOR";
--obj;
obj.display();
getch();
}

Output
Page | 56
Himanshu Tandon
08429802019
BCA 3EB
Source Code

UNARRY – OPERATOR WITHOUT FRIEND


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class decrement
{
private:
int marks1,marks2,marks3;
public:
void enterdata()
{
cout<<"enter marks1\n";
cin>>marks1;
cout<<"enter marks2\n";

Page | 57
Himanshu Tandon
08429802019
BCA 3EB
cin>>marks2;
cout<<"enter marks3\n";
cin>>marks3;
}
void display()
{
cout<<"marks1= "<<marks1<<"\n";
cout<<"marks2= "<<marks2<<"\n";
cout<<"marks3= "<<marks3<<"\n";
}
void operator --();
};
void decrement:: operator --()
{
marks1--;
marks2--;
marks3--;
}
void main()
{
decrement d;
clrscr();
d.enterdata();
--d;
d.display();
getch();
}

Output OPERATOR WITHOUT FRIEND

Page | 58
Himanshu Tandon
08429802019
BCA 3EB
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class decrement
{
private:
int marks1,marks2,marks3;
public:
void enterdata()
{
cout<<"enter marks1\n";
cin>>marks1;
cout<<"enter marks2\n";
cin>>marks2;
Page | 59
Himanshu Tandon
08429802019
BCA 3EB
cout<<"enter marks3\n";
cin>>marks3;
}
void display()
{
cout<<"marks1= "<<marks1<<"\n";
cout<<"marks2= "<<marks2<<"\n";
cout<<"marks3= "<<marks3<<"\n";
}
friend void operator --(decrement &m);
};
void operator --(decrement &m)
{
m.marks1--;
m.marks2--;
m.marks3--;
}
void main()
{
decrement d;
clrscr();
d.enterdata();
--d;
d.display();
getch();
}

Output UNARRY OPERATOR WITH FRIEND


Page | 60
Himanshu Tandon
08429802019
BCA 3EB
Page | 61
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #21
21. Write program to overload Binary + to concatenate two strings.(Both using
and without using friend functions)

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size=100;
class concat
{
public:
char str[size];
public:
concat()
{
strcpy(str,"");
}
concat(char s[])
{
strcpy(str,s);
}
void display()
{
cout<<str;
}
friend concat operator +(concat c1,concat c2);
};
concat operator +(concat c1,concat c2)
{
Page | 62
Himanshu Tandon
08429802019
BCA 3EB
concat temp;
strcpy(temp.str,c1.str);
strcat(temp.str,c2.str);
return temp;
}
void main()
{
clrscr();
concat c1="Himanshu ";
concat c2="Tandon";
concat c3;
c3=c1+c2;
c3.display();
getch(); }

Output

Page | 63
Himanshu Tandon
08429802019
BCA 3EB
CODE- WITHOUT FRIEND
#include<iostream.h>
#include<conio.h>
#include<string.h>
const int size=81;
class concat
{
public:
char str[size];
public:
concat()
{
strcpy(str,"");
}
concat(char s[])
{
strcpy(str,s);
}
void display()
{
cout<<str;
}
concat operator +(concat c2);
};
concat concat::operator +(concat c2)
{
concat temp;
strcpy(temp.str,str);
strcat(temp.str,c2.str);
return temp;

Page | 64
Himanshu Tandon
08429802019
BCA 3EB
}
void main()
{
clrscr();
concat c1="himanshu ";
concat c2="tandon";
concat c3;
c3=c1+c2;
c3.display();
getch();
}

Output WITHOUT FRIEND

Page | 65
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #22
22. WAP to implement += operator.

Ans. Source Code


#include<iostream.h>
#include<conio.h>
class weight
{
private:
int kilogram;
public:
weight ()
{
kilogram = 0;
}
weight (int kg)
{
kilogram = kg;

}
void display ()
{
cout << kilogram << "kg\n";
}
void operator += (weight w2)
{
kilogram += w2.kilogram;
}
};
void main ()

Page | 66
Himanshu Tandon
08429802019
BCA 3EB
{
clrscr ();
weight w1 (8);
weight w2 (6);
cout << "kg1= ";
w1.display ();
cout << "kg2= ";
w2.display ();
w1 += w2;
cout << "output weight= ";
w1.display ();
getch ();
}

Output

Page | 67
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #23
23. WAP to implement = operator.

Ans. Source Code


#include<iostream.h>
#include<conio.h>
class weight
{
private:
int kilogram;
public:
weight()
{
kilogram=0;
}
weight(int kg)
{
kilogram=kg;
}
void display()
{
cout<<kilogram<<"kg\n";
}
void operator =(weight w2)
{
kilogram =w2.kilogram;
}
};
void main()
{

Page | 68
Himanshu Tandon
08429802019
BCA 3EB
clrscr();
weight w1(8);
weight w2(6);
cout<<"kg1= ";
w1.display();
cout<<"kg2= ";
w2.display();
w1=w2;
cout<<"output weight= ";
w1.display();
getch();
}

Output

Page | 69
Himanshu Tandon
08429802019
BCA 3EB
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.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
class person
{
protected:
char name[20];
int age;
char sex;
public:
void ReadPerson()
{
cout<<"ENTER DETAILS OF PERSON"<<endl;
cout<<"Enter name?";
cin>>name;
cout<<"Enter age?";
cin>>age;
cout<<"Enter sex?";

Page | 70
Himanshu Tandon
08429802019
BCA 3EB
cin>>sex;
}
void DisplayPerson()
{
cout<<"Display details of person"<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Age : "<<age<<endl;
cout<<"Sex : "<<sex<<endl;
}
};
class student: public virtual person
{
protected:
int enroll;
char branch[20];
public:
void ReadData()
{
cout<<"ENTER DETAILS OF STUDENT"<<endl;
cout<<"Enter enrollment number ? ";
cin>>enroll;
cout<<"Enter branch studying?";
cin>>branch;
}
void DisplayData()
{
cout<<"DISPLAY DETAILS OF STUDENT ... "<<endl;
cout<<"Enrollment number : "<<enroll<<endl;

Page | 71
Himanshu Tandon
08429802019
BCA 3EB
cout<<"Branch : "<<branch<<endl;
}
};
class worker:public virtual person
{
protected:
int securityno;
char wing;
public:
void ReadData()
{
cout<<"ENTER DETAILS OF WORKER CLASS"<<endl;
cout<<"Enter security number?";
cin>>securityno;
cout<<"Enter the wing ? ";
cin>>wing;
}
void DisplayData()
{
cout<<"DISPLAY DETAILS OF WORKER ... "<<endl;
cout<<"Security number : " <<securityno<<endl;
cout<<"Wing : "<<wing;
}
};
class student_worker:public student,public worker
{
public:
void ReadData()

Page | 72
Himanshu Tandon
08429802019
BCA 3EB
{
ReadPerson();
student::ReadData();
worker::ReadData();
}
void DisplayData()
{
DisplayPerson();
student::DisplayData();
worker::DisplayData();
}
};
void main()
{
clrscr();
student_worker st;
cout<<"ENTER DATA"<<endl;
st.ReadData();
cout<<"DISPLAYING......."<<endl;
st.DisplayData();
getch();
}

Page | 73
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 74
Himanshu Tandon
08429802019
BCA 3EB
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.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class newwindow
{
public:
newwindow()
{
cout<<"the quantity of windows is 15"<<"\n";
cout<<"the shape of window is square"<<"\n";
}
};
class door
{
public:
door()
{
cout<<"the quantity of doors is 20"<<"\n";
cout<<"the colour of doors is blue"<<"\n";
}
};
class house:public newwindow,public door
{
public:
Page | 75
Himanshu Tandon
08429802019
BCA 3EB
house()
{
cout<<"the house is situated in delhi"<<"\n";
cout<<"it is a 5bhk apartment"<<"\n";
}
};
void main()
{
clrscr();
house h;
getch();
}

Output

Page | 76
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #26
26. Implement the following class hierarchy considering appropriate data
members and member functions.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student
{
int roll;
char name[30];
public:
void enterdata ()
{
cout << "Enter the roll number of the sudent:\n ";
cin >> roll;
cout << "Enter the name of the student:\n ";
gets(name);
}
void display ()
{
cout << "Roll Number: " << roll << "\n";
cout << "Name: " << name << "\n";

Page | 77
Himanshu Tandon
08429802019
BCA 3EB
}
};
class test:virtual public student
{
int m1, m2, m3;
public:
void enterdata ()
{
cout << "Enter marks 1:\n ";
cin >> m1;
cout << "Enter marks 2:\n ";
cin >> m2;
cout << "Enter marks 3:\n ";
cin >> m3;
}
void display ()
{
cout << "Marks 1: " << m1 << "\n";
cout << "Marks 2: " << m2 << "\n";
cout << "Marks 3: " << m3 << "\n";
cout << "Total score: " << sum () << "\n";
}
int sum ()
{
return m1 + m2 + m3;
}
};

class sports:virtual public student


{
Page | 78
Himanshu Tandon
08429802019
BCA 3EB
char name[10];
int score;
public:
void enterdata ()
{
cout << "Enter the name of sport that the student plays:\n ";
gets(name);
cout << "\nEnter marks earned:\n";
cin >> score;
}
int totalscore ()
{
return score;
}
void display ()
{
cout << "Sport : " << name << "\n";
cout << "Marks: " << score << "\n";
}
};
class performance:public test, public sports
{
public:
int totalpoints ()
{
return sum () + totalscore ();
}

};
void main ()
Page | 79
Himanshu Tandon
08429802019
BCA 3EB
{
clrscr ();
performance p;
p.student::enterdata ();
p.test::enterdata ();
p.sports::enterdata ();
p.student::display ();
p.test::display ();
p.sports::display ();
cout << "Total Score: " << p.totalpoints ();
getch ();
}

Output

Page | 80
Himanshu Tandon
08429802019
BCA 3EB
Page | 81
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #27
27. Implement the following hierarchy considering appropriate data members
and member functions (use Virtual functions).

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
class shape
{
int no;
char colour[20];
public:
void enterdata()
{
cout<<"enter no of sides:\n ";
cin>>no;
cout<<"enter color of shape:\n ";
cin>>colour;

}
void display()
{
cout<<"sides: "<<no<<"\n";
cout<<"color: "<<colour<<"\n";
}

Page | 82
Himanshu Tandon
08429802019
BCA 3EB
};
class triangle:virtual public shape
{
float base,height,a;
public:
void enterdata()
{
shape::enterdata();
cout<<" enter base and height:\n ";
cin>>base>>height;
}
void display()
{
shape::display();
cout<<"\narea of triangle : "<<area();
}
int area()
{
return a=(base*height)/2;
}
};
class rectangle:virtual public shape
{
float breadth,length,ar;
public:
void enterdata()
{
shape::enterdata();
cout<<" enter length and breadth:\n ";
cin>>length>>breadth;

Page | 83
Himanshu Tandon
08429802019
BCA 3EB
}
void display()
{
shape::display();
cout<<"area of rectangle : "<<area()<<"\n";
}
int area()
{
return ar=length*breadth;
}
};
class circle:virtual public shape
{
float r,ac,pi;

public:
void enterdata()
{
shape::enterdata();
cout<<" enter radius:\n ";
cin>>r;
pi=3.14;
}
void display()
{
shape::display();
cout<<"area of circle : "<<area()<<"\n";
}
int area()
{

Page | 84
Himanshu Tandon
08429802019
BCA 3EB
return ac=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();
}

Page | 85
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 86
Himanshu Tandon
08429802019
BCA 3EB
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.

Ans.
Source Code
#include<iostream.h>
#include<conio.h>
class vehicle
{
int rno,cost;
public:
virtual void start()=0;
virtual void stop()=0;
virtual void show()
{
cout<<"\nenter register number:\n "<<rno;
cout<<"\nenter cost:\n "<<cost;
}
vehicle()
{
cout<<"\nenter register number:\n ";
cin>>rno;
cout<<"\nenter cost:\n ";
Page | 87
Himanshu Tandon
08429802019
BCA 3EB
cin>>cost;
}

};
class lightweight:public vehicle
{
int sl;
public:
virtual void start()
{

cout<<"\nvehicle engine on\n";


}
virtual void stop()
{

cout<<"\nvehicle engine off\n";


}
virtual void show()
{
vehicle::show();
cout<<"\nspeed limit\n"<<sl;
}
lightweight()
{
cout<<"\nenter speed limit:\n ";
cin>>sl;
}

};

Page | 88
Himanshu Tandon
08429802019
BCA 3EB
class heavyweight:public vehicle
{
int weight;
public:
virtual void start()
{
cout<<"\nvehicle engine on\n";
}
virtual void stop()
{
cout<<"\nvehicle engine off\n";
}
virtual void show()
{
vehicle::show();
cout<<"\nweight the vehivle can carry:
"<<weight<<"\n";
}
heavyweight()
{
cout<<"\nenter weight: \n ";
cin>>weight;
}

};
void main()
{
clrscr();
heavyweight h;
lightweight l;

Page | 89
Himanshu Tandon
08429802019
BCA 3EB
h.start();
h.show();
h.stop();
l.start();
l.show();
l.stop();
getch();
}

Output

Page | 90
Himanshu Tandon
08429802019
BCA 3EB
Page | 91
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #29
29. WAP to show the implementation of ‘containership’.
Ans.
Source Code
#include<iostream.h>
#include<conio.h>
class a
{
public:
void show()
{
cout << "my name is HIMANSHU TANDON\n";
}
};
class b
{
a x;
public:
b()
{
x.show();
}
};
void main()
{
clrscr();
b t;
getch();
}

Output
Page | 92
Himanshu Tandon
08429802019
BCA 3EB
Page | 93
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #30
30. Implement the following data structures using C++: (You can also use
templates)
Linked List, Stack and Queue

Ans.
Source Code
\\STACK
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
template<class T>
class stack
{
private:
T s[10];
int top,n;
public:
stack()
{
top=-1;
cout<<"enter the stack size: ";
cin>>n;
}
void push(T element)
{
if(top<n-1)
s[++top]=element;
else
cout<<"stack is full.Can't insert "<<element<<"\n";

Page | 94
Himanshu Tandon
08429802019
BCA 3EB
}
void pop()
{
if(top<0)
cout<<"stack is empty\n";
else
cout<<"poped element: "<<s[top--];

}
void operation();
};
template<class T>
void stack<T>::operation()
{
int choice,i;
choice=1;
T element;
while(choice>0&&choice<3)
{
cout<<"\n1.push\t";
cout<<"\n2.pop\t";
cout<<"\n3.exit\t";
cout<<"\nchoice:";
cin>>choice;
switch(choice)
{
case 1:
cout<<"enter the element to push:";
cin>>element;
push(element);

Page | 95
Himanshu Tandon
08429802019
BCA 3EB
cout<<"stack:\n";
for(i=0;i<=top;i++)
cout<<s[i];
break;
case 2:
pop();
cout<<"stack:\n";
for(i=0;i<=top;i++)
cout<<s[i];
break;
}
}
}
void main()
{
clrscr();
cout<<"\nstack operation using template\n";
stack<int>s1;
s1.operation();
getch();
}

Output STACK

Page | 96
Himanshu Tandon
08429802019
BCA 3EB
Source Code

QUEUE
#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 ();

Page | 97
Himanshu Tandon
08429802019
BCA 3EB
};

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 \n";
for (int i = front; i <= rear; i++)
{
cout << q[i] << "\t";

}
}
void main ()
{

Page | 98
Himanshu Tandon
08429802019
BCA 3EB
clrscr ();
queue < int >q1;
q1.insert (55);
q1.insert (67);
q1.insert (28);
q1.insert (109);
q1.display ();
q1.remove ();
q1.display ();
getch ();
}

Output QUEUE

Page | 99
Himanshu Tandon
08429802019
BCA 3EB
Source Code

LINKED LIST
#include <iostream.h>
#include<conio.h>
template < class T >
class linklist
{
private:
int front, rear;
T l[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)
{
l[++rear] = i;
}
template < class T >
void linklist < T >::remove ()
Page | 100
Himanshu Tandon
08429802019
BCA 3EB
{
for (int k = front; k <= rear; k++)
{
l[k] = l[k + 1];
}
rear--;
}
template < class T >
void linklist < T >::display ()
{
cout << "\nlinked list is \n";
for (int i = front; i <= rear; i++)
{
cout << l[i] << "\t";

}
}
void main ()
{
clrscr ();
linklist < int >l1;
l1.insert (370);
l1.insert (832);
l1.insert (957);
l1.insert (126);
l1.display ();
l1.remove ();
l1.display ();
getch ();
}

Page | 101
Himanshu Tandon
08429802019
BCA 3EB
Output LINKED LIST

Page | 102
Himanshu Tandon
08429802019
BCA 3EB
PRACTICAL #31
31. WAP to show swapping using template function (Generic).

Ans.
Source Code
#include <iostream.h>
#include<conio.h>
template <class T>
void swap(T a,T b)
{
cout<<"value of a before swap="<<a<<"\n";
cout<<"value of b before swap="<<b<<"\n";
T temp;
temp=a;
a=b;
b=temp;
cout<<"value of a after swap= "<<a<<"\n";
cout<<"value of b after swap= "<<b<<"\n";
}
void main()
{
clrscr();
swap(2,83);
swap(14.2,73.8);
swap("b","z");
getch();}

Page | 103
Himanshu Tandon
08429802019
BCA 3EB
Output

Page | 104
Himanshu Tandon
08429802019
BCA 3EB

You might also like