0% found this document useful (0 votes)
233 views

OOPS Practical File

The document is a lab report for an Object Oriented Programming lab. It contains 34 experiments performed in the lab related to OOP concepts like classes, objects, inheritance, polymorphism, templates, exceptions etc. The first experiment involves writing a program to multiply two matrices using a Matrix class with member functions for input, multiplication and display of matrices.

Uploaded by

Aakash Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
233 views

OOPS Practical File

The document is a lab report for an Object Oriented Programming lab. It contains 34 experiments performed in the lab related to OOP concepts like classes, objects, inheritance, polymorphism, templates, exceptions etc. The first experiment involves writing a program to multiply two matrices using a Matrix class with member functions for input, multiplication and display of matrices.

Uploaded by

Aakash Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 94

OBJECT ORIENTED PROGRAMMING LAB

ETCS-258

Faculty Name: Ms. Karuna Middha Student Name: Priyank Rawat


Roll No. : 02596402719
Group : 4C12

Maharaja Agrasen Institute of Technology, PSP Area,


Sector – 22, Rohini, New Delhi – 110086
Index

Exp. Experiment Name Date of Marks Total Sign


no performance Marks
R1 R2 R3 R4 R5

Write a program for multiplication of


1 18 March 2021
two matrices using OOP.

Write a program to find whether a


2 18 March 2021
number is prime or not.
Write a program to take name, address
as character string, age as int, salary as
3 18 March 2021
float and contains inline function to set
the values and display it.
Using the concept of function
overloading, write a function for
4 18 March 2021
calculating area of triangle, circle and
rectangle.
Write a program to create a class
Student which have data members as
name, branch, roll no, age, sex and
5 18 March 2021
five subjects. Display the name of
student and his percentage who has
more than 70%.
Write a program to create a TIME
class with members hours, minutes
6 and seconds. Take input, add two-time 8 April 2021
objects and passing objects to function
and display the result.
Write a program to create a function
power to raise a number m to a power
n. The function takes double value for
7 m and integer value for n. use default 8 April 2021
value for n to make the function.
Calculate the squares when this
argument is omitted.
Write a program to find the greatest of
8 two given numbers in two different 15 April 2021
classes using friend function.
Write a program to find the biggest of
9 15 April 2021
three number using Friend Function.
Write a program to demonstrate the
10 use of friend function with Inline 15 April 2021
assignment.
Write a program to find the sum of
two numbers declared in a class and
11 15 April 2021
display the numbers and sum using
friend class.
Write a program to perform addition
of two complex numbers using
constructor overloading. The first
constructor which takes no argument
is used to create objects which are not
12 initialized, second which takes 22 April 2021
argument is used to initialize real and
imaginary parts to equal values and
third which takes two arguments is
used to initialized real and imaginary
to two different values.
Write a program to enter any number
13 22 April 2021
and find its factorial using constructor.
Write a program to generate a
14 Fibonacci series using Copy 22 April 2021
Constructor.
Implement a class string containing
the following functions:
Overload + operator to carry out the
concatenation of strings.
Overload = operator to carry out string
copy.
Overload <= operator to carry out the
15 comparison of strings. 26 April 2021
Function to display the length of
string.
Function tolower() to convert upper
case to lower case.
Function toupper() to convert lower
case letters to upper case.

Write a program to overload new and


16 3 May2021
delete operators
Write a program to overload unary
17 increment(++) and decrement(--) 3 May2021
operators
18 Create a base class basic_info with 10 May 2021
data members name, roll no, sex and
two member functions getdata and
display. Derive a
class physical_fit from basic_info whi
ch has data members height and
weight and member
functions getdata and display. Display
all the information using object of
derived class.
Create class first with data members
book no, book name and member
function getdata and putdata. Create a
class second with data members
author name, publisher and
19 members getdata and showdata. 10 May 2021
Derive a class third from first and
second with data member no of pages
and year of publication. Display all
this information using array of objects
of third class.
Design three classes STUDENT,
EXAM, and RESULT. The
STUDENT class has data members
such as roll no, name. Create a class
EXAM by inheriting the STUDENT
class. The EXAM class adds data
20 10 May 2021
members representing the marks
scored in six subjects. Derive the
RESULT from the EXAM class and
has its own data members such as
total marks. WAP to model this
relationship.
Create a class called LIST with two
pure virtual function store() and
retrieve().To store a value call store
21 17 May 2021
and to retrieve call retrieve function.
Derive two classes stack and queue
from it and override store and retrieve.
22 Create a base class called SHAPE. 17 May 2021
Use this class to store two double type
values. Derive two specific classes
called TRIANGLE and RECTANGLE
from the base class. Add to the base
class, a member function getdata to
initialize base class data members and
another member function display to
compute and display the area of
figures. Make display a virtual
function and redefine this function in
the derived classes to suit their
requirements. Using these three
classes design a program that will
accept driven of a TRIANGLE or
RECTANGLE interactively and
display the area.
Write a program to define the function
template for calculating the square of
23 17 May 2021
given numbers with different data
types.
Write a program to demonstrate the
use of special functions, constructor
24 and destructor in the class template. 24 May 2021
The program is used to find the largest
of two numbers entered
Write a program to define a function
template for swapping two items of
25 24 May 2021
the various data types such as integer,
float and character
Write a program to illustrate how
26 24 May 2021
template functions can be overloaded
Write a program to illustrate how to
define and declare a class template for
27 31 May 2021
reading two data items from the
keyboard and find their sum
Write a program to read a set of lines
28 from keyboard and store it on a 31 May 2021
specified file
Write a program to read a text file and
29 31 May 2021
display its contents on the screen
Write a program to copy the contents
30 6 June 2021
of a file into another
Write a program to read the class
object of student_info such as name,
age, sex, height and weight from the
keyboard and to store them in a file
31 6 June 2021
using read() and write() functions.
Again the same file is opened for
reading and displaying the contents of
the file on the screen
Write a program to perform the
deletion of white spaces such as
horizontal tab, vertical tab, space,
32 line feed, new line and carriage return 6 June 2021
from a text file and store the contents
of the file without the white spaces on
another file.
33 Write a program to raise an exception 13 June 2021
if any attempt is made to refer to an
element whose index is beyond the
array size.
Write a program to read two numbers
and then divide first no by second no
34 13 June 2021
and raise an exception if second
number is zero.
EXPERIMENT – 1

AIM
Write a program for multiplication of two matrices using OOP.
SOURCE CODE
#include <iostream>

using namespace std;

class Matrix {
int x[100][100];
int row;
int column;

public :

void input(int m,int n)


{
row = m;
column = n;
cout<<"Enter the matrix : ";
for(int i = 0;i<row;i++)
{
for(int j = 0;j<column;j++)
{
cin>>x[i][j];
}
}
}

void multiplication(Matrix a, Matrix b)


{
if(a.column == b.row)
{
row = a.row;
column = b.column;
for(int i = 0; i<row;i++)
{
for(int j =0;j<column;j++)
{
for(int k =0;k<a.column;k++)
{
x[i][j] = a.x[i][k] * b.x[k][j];
}
}
}
}
else
{
cout<<"Multiplication not possible.";
}
}

void display() {
cout<<"The matrix is : \n";
for(int i = 0;i<row;i++)
{
for(int j = 0;j<column;j++)
{
cout<<x[i][j]<<" ";
}
cout<<"\n";
}

}
};

int main() {

int l,m,n,o;
cout<<"Enter the number of rows and columns for matrix 1 :";
cin>>l>>m;

Matrix m1;
m1.input(l,m);

cout<<"Enter the number of rows and columns for matrix 2 :";


cin>>n>>o;

Matrix m2;
m2.input(n,o);

cout<<"\n\nMatrix 1\n";
m1.display();
cout<<"\n\nMatrix 2\n";
m2.display();

Matrix mult;
mult.multiplication(m1,m2);
mult.display();

return 0;
}

OUTPUT
EXPERIMENT – 2

AIM
Write a program to find whether a number is prime or not.

SOURCE CODE
#include<iostream>
using namespace std;

class Prime{
private:
int x;
public :
void input(int a)
{
x = a;
}

void isPrime()
{
int count = 0;
for(int i=2;i<=x/2;i++)
{
if(x%i==0)
{
count++;
break;
}
}

if(count==0)
cout<<x<<" is a prime number\n";
else
cout<<x<<" is not a prime number\n";
}
};
int main()
{
int n;

cout<<"\nEnter a number : ";


cin>>n;

Prime num;
num.input(n);

num.isPrime();

return 0;
}
OUTPUT
EXPERIMENT – 3

AIM
Write a program to take name, address as character string, age as int, salary as float and contains inline
function to set the values and display it.
SOURCE CODE
#include<iostream>
using namespace std;

class details {
private:
int age;
char name[20];
char address[40];
float salary;
public:
inline void inputData();
inline void displayData();
};

void details::inputData(){

cout<<"\nEnter Name : ";


cin>>name;
cout<<"\nEnter Age : ";
cin>>age;
cout<<"Enter Salary : ";
cin>>salary;
cout<<"Enter Address : ";
cin>>address;
}

void details::displayData(){

cout<<"\nThe Name is : "<<name;


cout<<"\nThe Age is : "<<age;
cout<<"\nThe Salary is : "<<salary;
cout<<"\nThe Address is : "<<address;
}

int main()
{
details a,b;
cout<<"\nEnter the details of a:";
a.inputData();
cout<<"\nEnter the details of b:";
b.inputData();

cout<<"\n\nDetails of a are :";


a.displayData();
cout<<"\n\nDetails of b are :";
b.displayData();

return 0;
}
OUTPUT
EXPERIMENT – 4

AIM
Using the concept of function overloading, write a function for calculating area of triangle, circle
and rectangle.
SOURCE CODE
#include<iostream>
using namespace std;

int area(int len,int bre){


return (len*bre);
}

float area(float rad){


return (3.14*rad*rad);
}

float area(float bas, float hei){


return(0.5*hei*bas);
}

int main()
{
int length,breadth;
float radius,base,height;
int choice;
cout<<"Enter choice(1 for rectangle 2 for circle 3 for triangle) : ";
cin>>choice;

switch(choice)
{

case 1 :cout<<"\n\tEnter the length and breadth of a rectangle : ";


cin>>length>>breadth;
cout<<"\tArea of rectangle : "<<area(length,breadth)<<endl;
break;

case 2 :cout<<"\n\tEnter the radius of circle : ";


cin>>radius;
cout<<"\tArea of circle : "<<area(radius)<<endl;
break;

case 3 :cout<<"\n\tEnter the base and height of triangle : ";


cin>>base>>height;
cout<<"\tArea of triangle : "<<area(base,height)<<endl;
break;

default:cout<<"Enter a valid choice .";


}
return 0;
}
OUTPUT
EXPERIMENT – 5

AIM
Write a program to create a class Student which have data members as name, branch, roll no, age,
sex and five subjects. Display the name of student and his percentage who has more than 70%.
SOURCE CODE
#include<iostream>
using namespace std;

class Student {
private :

int subjects[5];
char name[20];
char branch[10];
float rollno;
int age;
char sex;

public :

void getData(){

cout<<"\nEnter Name : ";


cin>>name;
cout<<"Enter Rollno : ";
cin>>rollno;
cout<<"Enter Branch : ";
cin>>branch;
cout<<"Enter Marks in 5 subjects :";
for(int i=0;i<5;i++)
cin>>subjects[i];
}
void check(){
int total=0;
for(int i=0;i<5;i++)
{
total+=subjects[i];
}
showData();
if(total/5 >= 70)
showPercentage(total);

void showData(){

cout<<"\n\n\n\nThe Name is : "<<name;


cout<<"\nThe Rollno is : "<<rollno;
cout<<"\nThe Branch is : "<<branch;
}

void showPercentage(int total)


{
cout<<"\nThe Percentage is : "<<total/5;
}
};

int main()
{
Student s1,s2;
cout<<"\nEnter the details of Student1 :";
s1.getData();
cout<<"\nEnter the details of Student2 :";
s2.getData();

cout<<"\nDetails of the Students are :";


s1.check();
s2.check();
return 0;
}
OUTPUT
EXPERIMENT – 6

AIM
Write a program to create a TIME class with members hours, minutes and seconds. Take input, add
two-time objects and passing objects to function and display the result.
SOURCE CODE
#include <iostream>

using namespace std;

class time
{
private:

int hours;
int minutes;
int seconds;

public:

void inputTime()
{
cout << "\n Enter time :";
cout << " \nEnter Hours :";
cin>>hours;
cout << "\nEnter Minutes :";
cin>>minutes;
cout << "\nEnter Seconds :";
cin>>seconds;
}

void showTime()
{
cout << "\nTime after addition :";
cout << hours << ":" << minutes << ":" << seconds << endl;
}

void addTime(time T1,time T2)


{
seconds=T1.seconds+T2.seconds;
minutes=T1.minutes+T2.minutes + this->seconds/60;;
hours= T1.hours+T2.hours + (this->minutes/60);
minutes %=60;
seconds %=60;
}
};
int main()
{
time T1,T2,T3;
cout<<"Program to add two different Times ";
T1.inputTime();
T2.inputTime();
T3.addTime(T1,T2);
T3.showTime();
return 0;
}
OUTPUT
EXPERIMENT – 7

AIM
Write a program to create a function power to raise a number m to a power n. The function takes
double value for m and integer value for n. use default value for n to make the function. Calculate
the squares when this argument is omitted.

SOURCE CODE
#include <iostream>

using namespace std;

double Power(double m,int n=2)


{

double power = 1;

for(int i=0; i<n; i++)


{
power *= m;
}

return power;
}

int main()
{
double m;
int n,choice;

cout<<"Enter choice(1 for power 2 for default value of n) : ";


cin>>choice;

switch(choice)
{
case 1 : cout<<"Enter the number:";
cin>>m;
cout<<"Enter the power :";
cin>>n;
cout<<"\n"<<m<<"^"<<n<<"="<<Power(m,n);
break;

case 2: cout<<"Enter the number : ";


cin>>m;
cout<<"\n"<<m<<"^"<<2<<"="<<Power(m);

return 0;
}
}
OUTPUT
EXPERIMENT – 8

AIM
Write a program to find the greatest of two given numbers in two different classes using friend
function.
SOURCE CODE
#include <iostream>

using namespace std;

class number1;
class number2;

class number1
{
int a;
public :

void input(int number)


{
a = number;
}

friend int Largest(number1 a, number2 b);


};

class number2
{
int b;
public :

void input(int number)


{
b = number;
}
friend int Largest(number1 a, number2 b);
};

int Largest(number1 x,number2 y)


{
if(x.a>y.b)
{
return x.a;
}
else
{
return y.b;
}
}

int main()
{
number1 x;
number2 y;
int num1,num2;
cout<<"Enter the first number : ";
cin>>num1;
x.input(num1);
cout<<"Enter the second number : ";
cin>>num2;
y.input(num2);
cout<<"The largest number is :"<<Largest(x,y);
return 0;
}
OUTPUT
EXPERIMENT – 9

AIM
Write a program to find the biggest of three number using Friend Function.
SOURCE CODE
#include <iostream>

using namespace std;

class num1;
class num2;
class num3;

class num1
{
int a;
public :
void input(int x)
{
a=x;
}

friend int Cal(num1 d,num2 e,num3 f);


};

class num2
{
int b;
public :
void input(int y)
{
b=y;
}

friend int Cal(num1 d,num2 e,num3 f);


};

class num3
{
int c;
public :
void input(int z)
{
c=z;
}

friend int Cal(num1 d,num2 e,num3 f);


};

int Cal(num1 d,num2 e,num3 f)


{
if(d.a>e.b)
{
if(d.a>f.c)
{
return d.a;
}
else
{
return f.c;
}
}
else
{
if(e.b>f.c)
{
return e.b;
}
else
{
return f.c;
}
}
}

int main()
{
num1 a;
num2 b;
num3 c;
int x,y,z;
cout<<"Enter the three numbers : ";
cin>>x>>y>>z;
a.input(x);
b.input(y);
c.input(z);
cout<<"The largest number is : "<<Cal(a,b,c);
return 0;
}
OUTPUT
EXPERIMENT – 10

AIM
Write a program to demonstrate the use of friend function with Inline assignment.
SOURCE CODE
#include <iostream>

using namespace std;

class number
{
int a;
public :
void input(int x)
{
a=x;
}

inline friend int Cal(number num);


};

int Cal(number num)


{
return num.a;
}

int main()
{
number a;
int x;
cout<<"Enter the number : ";
cin>>x;
a.input(x);
cout<<"The private number using friend function is : "<<Cal(a);
return 0;
}
OUTPUT
EXPERIMENT – 11

AIM
Write a program to find the sum of two numbers declared in a class and display the numbers and sum
using friend class.

SOURCE CODE
#include <iostream>

using namespace std;

class number1;
class number2;

class number1
{
int a;
public :

void input(int number)


{
a = number;
}

friend int Sum(number1 a, number2 b);


};

class number2
{
int b;
public :

void input(int number)


{
b = number;
}

friend int Sum(number1 a, number2 b);


};

int Sum(number1 x,number2 y)


{
return (x.a + y.b);
}

int main()
{
number1 x;
number2 y;
int num1,num2;
cout<<"Enter the first number : ";
cin>>num1;
x.input(num1);
cout<<"Enter the second number : ";
cin>>num2;
y.input(num2);
cout<<"The sum is :"<<Sum(x,y);
return 0;
}
OUTPUT
EXPERIMENT – 12

AIM
Write a program to perform addition of two complex numbers using constructor overloading. The
first constructor which takes no argument is used to create objects which are not initialized, second
which takes argument is used to initialize real and imaginary parts to equal values and third which
takes two arguments is used to initialized real and imaginary to two different values.
SOURCE CODE
#include <iostream>

using namespace std;

class Complex
{
int real;
int imaginary;

public:
Complex()
{
real = 0;
imaginary =0;

Complex(int x)
{
real = x;
imaginary = x;
}

Complex(int x,int y)
{
real = x;
imaginary = y;
}

void addNumbers(Complex C1,Complex C2)


{
real = C1.real + C2.real;
imaginary = C1.imaginary + C2.imaginary;
}

int getReal()
{
return real;
}

int getImaginary()
{
return imaginary;
}

};

int main()
{
int e,f,g,h;
cout<<"Enter values for complex number a : ";
cin>>e>>f;
Complex a(e,f);
cout<<"Enter values for complex number b : ";
cin>>g>>h;
Complex b(g,h);
Complex add;
add.addNumbers(a,b);

cout<<"The sum of complex numbers is : "<<add.getReal()<<"+"<<add.getImaginary()<<"j";


return 0;
}
OUTPUT
EXPERIMENT – 13

AIM
Write a program to enter any number and find its factorial using constructor.
SOURCE CODE
#include <iostream>

using namespace std;


class factorial
{
int x,fact;
public :

factorial()
{
x = 0;
fact = 1;
}

factorial(int a)
{
if(a<=0)

{
fact = 1;

}
else

fact = 1 ;

for(int i=1;i<=a;i++)
{
fact = fact*i;
}
}

int getFact()
{
return fact;
}

};

int main()
{
int a;
cout << "Enter number : ";
cin>>a;
factorial fact(a);

cout<<"The factotial of number is :"<<fact.getFact();

return 0;
}
OUTPUT
EXPERIMENT – 14

AIM
Write a program to generate a Fibonacci series using Copy Constructor.
SOURCE CODE
#include <iostream>
using namespace std;

class fibonacci
{
private:
int f,f1,f2,m;
public:

fibonacci(int n)
{
m=n;
}

fibonacci( fibonacci &x)


{
x.f1=0;
x.f2=1;
cout<<"\n The Fibonacci series is : \n\n ";
cout<<" "<<x.f1<<" "<<x.f2;

for(int i=2;i<=x.m;i++)
{
x.f=x.f1+x.f2;
cout<<" "<<x.f;
x.f1=x.f2;
x.f2=x.f;
}
}
};
int main()
{
int n;
cout<<"\n Enter the number of elements in series:";
cin>>n;

fibonacci A(n);
fibonacci B(A);
return 0;

}
OUTPUT
EXPERIMENT – 15

AIM
Implement a class string containing the following functions:
• Overload + operator to carry out the concatenation of strings.
• Overload = operator to carry out string copy.
• Overload <= operator to carry out the comparison of strings.
• Function to display the length of string.
• Function tolower() to convert upper case to lower case.
• Function toupper() to convert lower case letters to upper case.
SOURCE CODE
• Overload + operator to carry out the concatenation of strings.
#include <iostream>

using namespace std;

class String
{
private:
string Word;

public :

String()
{
Word = "";
}

String(string a )
{
Word = a;
}

String operator + (String s1)


{
String res;
res.Word = Word + s1.Word;
return res;
}

void display()
{
cout<<"The string is : "<<Word;
}
};

int main()
{
string x,y;
cout<<"Enter first string : ";
getline(cin,x);
cout<<"Enter second string : ";
getline(cin,y);
String a(x);
String b(y);
String c = a + b;
c.display();

}
• Overload = operator to carry out string copy.
#include <iostream>

using namespace std;

class String
{
private:
string Word;

public :

String()
{
Word = "";
}

String(string a )
{
Word = a;
}

String operator = (String s1)


{
String res;
res.Word = s1.Word;
return res;
}

void display()
{
cout<<"The string is : "<<Word;
}
};

int main()
{
string x;
cout<<"Enter first string : ";
getline(cin,x);
String a(x);
String c = a ;
c.display();
}
• Overload <= operator to carry out the comparison of strings.
#include <iostream>

using namespace std;

class String
{
private:
string Word;

public :

String()
{
Word = "";
}

String(string a )
{
Word = a;
}

void operator <= (String s1)


{
if (Word == s1.Word)
{
cout<<"The strings are same.";
}
else
{
cout<<"The strings are not same";
}
}

void display()
{
cout<<"The string is : "<<Word;
}
};

int main()
{
string x,y;
cout<<"Enter first string : ";
getline(cin,x);
cout<<"Enter second string : ";
getline(cin,y);
String a(x);
String b(y);
a<=b;
}
• Function to display the length of string.
include <iostream>

using namespace std;

class String
{
private:
string Word;

public :

String()
{
Word = "";
}

String(string a)
{
Word = a;
}

void Length()
{
int count;
for (int i = 0;Word[i];i++)
{
count++;
}
cout<<"The length is :"<<count;
}

void display()
{
cout<<"The string is : "<<Word<<"\n";
}
};

int main()
{
string x,y;
cout<<"Enter first string : ";
getline(cin,x);
String a(x);
a.display();
a.Length();

}
• Function tolower() to convert upper case to lower case.
#include <iostream>
#include<cstring>

using namespace std;

class String
{
private:
char Word[100];
int Size;

public :

String(int len)
{
Size = len;
cout<<"Enter string : ";
cin.getline(Word,Size);
}

String tolower(String s1)


{
for(int i = 0;Word[i];i++)
{
if(Word[i]>=65 && Word[i]<=90)
{
Word[i] = Word[i] + 32 ;
}
}
}

void display()
{
cout<<"The string is : "<<Word<<"\n";
}
};

int main()
{
char x[30];
int Size;
cout<<"Enter size of String :";
cin>>Size;
cin.ignore();
String a(Size);
a.tolower(a);
a.display();
}
• Function toupper() to convert lower case letters to upper case.
#include <iostream>
#include<cstring>

using namespace std;

class String
{
private:
char Word[100];
int Size;

public :

String(int len)
{
Size = len;
cout<<"Enter string : ";
cin.getline(Word,Size);
}

String toupper(String s1)


{
for(int i = 0;Word[i];i++)
{
if(Word[i]>=97 && Word[i]<=122)
{
Word[i] = Word[i] - 32 ;
}
}
}

void display()
{
cout<<"The string is : "<<Word<<"\n";
}
};

int main()
{
char x[30];
int Size;
cout<<"Enter size of String :";
cin>>Size;
cin.ignore();
String a(Size);
a.toupper(a);
a.display();
}

OUTPUT
• Overload + operator to carry out the concatenation of strings.

• Overload = operator to carry out string copy.

• Overload <= operator to carry out the comparison of strings.

• Function to display the length of string.

• Function tolower() to convert upper case to lower case.

• F
u
n
ct
io
n
to
upper() to convert lower case letters to upper case.
EXPERIMENT – 16

AIM
Write a program to overload new and delete operators.
SOURCE CODE
#include <iostream>

using namespace std;

class Data
{
private :
int number;

public :
Data()
{
cout<<"\nConstructor is called.";
}

Data(int n)
{
number = n;
}

void display()
{
cout<<"\nThe data is : "<<number<<"\n";
}

void * operator new(size_t size)


{
cout<<"\nOverloading new operator with size : "<<size<<"\n";
void * p = malloc(size);
return p;
}

void operator delete(void * p)


{
cout<<"Overloading delete operator.\n";
free(p);
}
};

int main()
{
Data * p = new Data(10);
p->display();
delete p;
}
OUTPUT
EXPERIMENT – 17

AIM
Write a program to overload unary increment(++) and decrement(--) operators
SOURCE CODE
#include <iostream>

using namespace std;

class Data
{
public :
int number;
Data(int a)
{
number = a;
}

int operator ++(int number)


{
number = number+1;
}

int operator --(int number)


{
number = number-1;
}

void display()
{
cout<<"The number is : "<<number;
}
};

int main()
{
int x;
cout<<"\nEnter number : ";
cin>>x;
Data D(x);
D.number++;
D.display();
D.number--;
D.display();
return 0;
}
OUTPUT
EXPERIMENT – 18

AIM
Create a base class basic_info with data members name, roll no, sex and two member
functions getdata and display. Derive a class physical_fit from basic_info which has data members height
and weight and member functions getdata and display. Display all the information using object of derived
class.
SOURCE CODE
#include <iostream>

using namespace std;

class basic_info
{
private :
string name;
int rollno;
char sex;

public :
void getdata()
{
cout<<"\nEnter name : ";
getline(cin,name);
cout<<"\nEnter roll No. : ";
cin>>rollno;
cout<<"\nEnter gender(M/F) :";
cin>>sex;
}

void display()
{
cout<<"\nThe name is : "<<name;
cout<<"\nThe roll no. is : "<<rollno;
cout<<"\nThe gender is : "<<sex;
}
};

class physical_fit : public basic_info


{
private:
float height;
float weight;
public :
void getdata()
{
basic_info::getdata();
cout<<"\nEnter height : ";
cin>>height;
cout<<"\nEnter weight : ";
cin>>weight;
}

void display()
{
basic_info::display();
cout<<"\nThe height is : "<<height;
cout<<"\nThe weight is : "<<weight;
}
};

int main()
{
physical_fit p;
p.getdata();
p.display();
}
OUTPUT
EXPERIMENT – 19

AIM
Create class first with data members book no, book name and member function getdata and putdata.
Create a class second with data members author name, publisher and members getdata and showdata.
Derive a class third from first and second with data member no of pages and year of publication. Display
all this information using array of objects of third class.
SOURCE CODE

#include <iostream> using


namespace std;

class first
{
private:
char bname[50]; int bno;

public:
void getdata ()
{
cout << "Enter Book No. : "; cin >> bno;
cout << "Enter Book name : "; cin >> bname;
}
void putdata()
{
cout << "Book No. : " << bno << endl; cout << "Book
Name : " << bname << endl;
}
};
class second
{
private:
char author[20]; char
publisher[20];
public:
void indata()
{
cout << "Enter Author : "; cin >> author;
cout << "Enter Publisher : "; cin >> publisher;

}
void showdata()
{
cout << "Author : "; cout <<
author;
cout << "Publisher : "; cout <<
publisher;
}
};
class third : public first, public second
{
private:
int year; int
page;

public:
void input()
{
getdata(); indata();
cout << "Enter number of pages : "; cin >> page;
cout << "Enter Year of Publication : "; cin >> year;
}
void output()
{
putdata();
showdata();
cout << "Number of pages : " << page << endl; cout << "Year of
Publication : " << year;
}
};
main()
{
int i, n;
cout << "Enter number of Books : "; cin >> n;
third obj[5];
for (i = 0; i < n; i++)
{
obj[i].input();
}
for (i = 0; i < n; i++)
{
cout << "About Book no. " << i + 1 << " : " << endl; obj[i].output();
}
}
OUTPUT
EXPERIMENT – 20

AIM
Design three classes STUDENT, EXAM, and RESULT. The STUDENT class has data members such as
roll no, name. Create a class EXAM by inheriting the STUDENT class. The EXAM class adds data
members representing the marks scored in six subjects. Derive the RESULT from the EXAM class and
has its own data members such as total marks. WAP to model this relationship.
SOURCE CODE
#include <iostream>

using namespace std;

class Student
{
public :

int rollno;
string name;

void getinfo()
{
cout<<"\nEnter Roll No. :";
cin>>rollno;
cin.ignore();
cout<<"\nEnter name :";
getline(cin,name);
}

};

class Exam : public Student

{
public :
float marks[6];
void getmarks()
{
cout<<"\nEnter marks in 6 subjects : ";
for(int i = 0;i<6;i++)
{
cin>>marks[i];
}
}
};

class Result : public Exam

{
public :
float totalmarks;
void getdata()
{
getinfo();
getmarks();
}

void showResult()
{
cout<<"\nThe roll no is :"<<rollno;
cout<<"\nThe name is : "<<name;
float sum = 0;
for(int i = 0;i<6;i++)
{
sum+=marks[i];
}
totalmarks = sum;
cout<<"\nThe total marks are(out of 600) :"<<totalmarks;
}
};
int main()
{
Result R;
R.getdata();
R.showResult();
return 0;
}
OUTPUT
EXPERIMENT – 21

AIM
Create a class called LIST with two pure virtual function store() and retrieve().To store a value call store
and to retrieve call retrieve function. Derive two classes stack and queue from it and override store and
retrieve.
SOURCE CODE
#include <iostream>

using namespace std;

class List
{
public :

int List[30];
int size;
virtual void store() = 0;
virtual void retrieve() = 0;
};

class Stack : public List

{
public :
void store()
{
cout<<"Enter number of elements : ";
cin>>size;
cout<<"\nEnter stack :";
for(int i = 0;i<size;i++)
{
cin>>List[i];
}
}
void retrieve()
{
cout<<"\nThe elements in stack are(from the top): ";
for(int i = 0;i<size;i++)
{
cout<<"\n"<<List[size-i-1];
}
}
};

class Queue : public List

{
public :
void store()
{
cout<<"\nEnter number of elements : ";
cin>>size;
cout<<"\nEnter queue :";
for(int i = 0;i<size;i++)
{
cin>>List[i];
}
}

void retrieve()
{
cout<<"\nThe elements in queue are(from the start): ";
for(int i = 0;i<size;i++)
{
cout<<"\n"<<List[i];
}
}
};
int main()
{
List *L;
Stack S;
L = &S;
L->store();
L->retrieve();
Queue Q;
L = &Q;
L->store();
L->retrieve();
return 0;
}
OUTPUT
EXPERIMENT – 22

AIM
Create a base class called SHAPE. Use this class to store two double type values. Derive two specific
classes called TRIANGLE and RECTANGLE from the base class. Add to the base class, a member
function getdata to initialize base class data members and another member function display to compute
and display the area of figures. Make display a virtual function and redefine this function in the derived
classes to suit their requirements. Using these three classes design a program that will accept driven of a
TRIANGLE or RECTANGLE interactively and display the area.
SOURCE CODE
#include <iostream>

using namespace std;

class Shape
{
public :

double value1;
double value2;

void getdata()
{
cout<<"\nEnter value 1 :";
cin>>value1;
cout<<"\nEnter value 2 :";
cin>>value2;
}

virtual void display() = 0;


};

class Rectangle : public Shape

{
public :
double area;
void display()
{
area = value1 * value2;
cout<<"\nThe area of rectangle is : "<<area<<"\n\n";
}
};

class Triangle : public Shape

{
public :
double area;
void display()
{
area = 0.5 * value1 * value2;
cout<<"\nThe area of triangle is : "<<area<<"\n\n";
}
};

int main()
{
Shape *S;
Rectangle R;
S = &R;
S->getdata();
S->display();
Triangle T;
S = &T;
S->getdata();
S->display();
return 0;
}
OUTPUT
EXPERIMENT – 23

AIM
Write a program to define the function template for calculating the square of given numbers with
different data types.
SOURCE CODE
#include <iostream>

using namespace std;

template<typename T>
T square(T a){
return a*a;
}

int main()
{
int i;
float j;
cout<<"Enter integer value: ";
cin>>i;
cout<<"\nThe square is : "<<square(i);
cout<<"\nEnter float value :";
cin>>j;
cout<<"\nThe square is : "<<square(j);
}
OUTPUT
EXPERIMENT – 24

AIM
Write a program to demonstrate the use of special functions, constructor and destructor in the class
template. The program is used to find the largest of two numbers entered.
SOURCE CODE
#include <iostream>

using namespace std;


template<class T>
class Bigger
{
private :
T a,b;

public :
Bigger(T x,T y)
{
a = x;
b = y;
}

~Bigger()
{
cout<<"\nObject Destroyed";
}

T Compare()
{
if(a>b)
{
return a;
}
return b;
}
};

int main()
{

int x,y;
float e,d;
cout<<"Enter the integer numbers : ";
cin>>x>>y;
Bigger<int> b(x,y);
cout<<"\nThe bigger number is : "<<b.Compare();
cout<<"\nEnter the float numbers : ";
cin>>e>>d;
Bigger<float> c(e,d);
cout<<"\nThe bigger number is : "<<c.Compare();
}
OUTPUT
EXPERIMENT – 25

AIM
Write a program to define a function template for swapping two items of the various data types such as
integer, float and character.
SOURCE CODE
#include <iostream>

using namespace std;

template<class T>
void Swap(T &a,T &b){
T temp;
temp = a;
a = b;
b = temp;
}

int main()
{
int i,j;
cout<<"Enter the two integer values : ";
cin>>i>>j;
Swap(i,j);
cout<<"\nThe numbers after swapping are : "<<i<<" and "<<j;
float a,b;
cout<<"\nEnter the two float values : ";
cin>>a>>b;
Swap(a,b);
cout<<"\nThe numbers after swapping are : "<<a<<" and "<<b;
char c,d;
cout<<"\nEnter the two characters : ";
cin>>c>>d;
Swap(c,d);
cout<<"\nThe characters after swapping are : "<<c<<" and "<<d;
return 0;
}

OUTPUT
EXPERIMENT – 26

AIM
Write a program to illustrate how template functions can be overloaded.
SOURCE CODE
#include <iostream>

using namespace std;

template<typename T>
void square(T a){
cout<<"\nTemplate function called.";
cout<<"\nThe square is :"<<a*a;
}

void square(int a){


cout<<"\nNormal function called.";
cout<<"\nThe square is :"<<a*a;
}

int main()
{
int i;
float j;
cout<<"Enter integer value: ";
cin>>i;
square(i);
cout<<"\nEnter float value :";
cin>>j;
square(j);
}
OUTPUT
EXPERIMENT – 27

AIM
Write a program to illustrate how to define and declare a class template for reading two data items from
the keyboard and find their sum.
SOURCE CODE
#include <iostream>

using namespace std;


template<class T>
class Sum
{
private :
T a,b;

public :
Sum(T x,T y)
{
a = x;
b = y;
}

T Addition()
{
return a+b;
}

};

int main()
{

int x,y;
float e,d;
cout<<"Enter the integer numbers : ";
cin>>x>>y;
Sum<int> b(x,y);
cout<<"\nThe sum is : "<<b.Addition();
cout<<"\nEnter the float numbers : ";
cin>>e>>d;
Sum<float> c(e,d);
cout<<"\nThe sum is : "<<c.Addition();
return 0;
}
OUTPUT
EXPERIMENT – 28

AIM
Write a program to read a set of lines from keyboard and store it on a specified file.
SOURCE CODE
#include <iostream>
#include<fstream>

using namespace std;

int main()
{
string data;
cout<<"Enter a string : ";
getline(cin,data);
ofstream out_file;
out_file.open("test.txt");
out_file << data << endl;
cout<<"File made successfully.";
out_file.close();
}
OUTPUT
EXPERIMENT – 29

AIM
Write a program to read a text file and display its contents on the screen.
SOURCE CODE
#include <fstream>
#include <iostream>
using namespace std;

int main()
{
int n;
char str1[100], str2[100];
fstream outfile;
outfile.open("exp23.txt", ios::out);
cout << "Writing file...." << endl;
cin.get();
cout << "Type something : ";
gets(str1);
outfile << str1 << endl;
outfile.close();
fstream infile;
infile.open("exp23.txt", ios::in);
cout << "Reading from file : " << endl;
while (infile.getline(str2, 100))
{
puts(str2);
}
infile.close();
return 0;
}
OUTPUT
EXPERIMENT – 30

AIM
Write a program to copy the contents of a file into another.
SOURCE CODE
#include <fstream>
#include <iostream>
using namespace std;
int main()
{

int n;
char str1[100], str2[100], str3[100];

fstream outfile1;
outfile1.open("myfile.txt", ios::out);
cout << "Writing to 1st file....." << endl;
cin.get();
cout << "Enter data : "; gets(str1);
outfile1 << str1 << endl;
outfile1.close();
fstream infile1, outfile2;
infile1.open("myfile.txt", ios::in);
outfile2.open("myfile1.txt", ios::out);
cout << "Reading from the first file and copying to second file" << endl;
while (infile1.getline(str2, 100))
{
puts(str2);
outfile2 << str2 << endl;
}
infile1.close();
outfile2.close();
fstream infile2;
infile2.open("myfile1.txt", ios::in);
cout << "Reading from copied file" << endl;
while (infile2.getline(str3, 100))
{
puts(str3);
}
infile2.close();
return 0;
}

OUTPUT
EXPERIMENT – 31

AIM
Write a program to read the class object of student_info such as name, age, sex, height and weight from
the keyboard and to store them in a file using read() and write() functions. Again the same file is opened
for reading and displaying the contents of the file on the screen
SOURCE CODE
#include <iostream>
#include<fstream>
using namespace std;

class Student
{
public:
string name;
int age;
char sex;
float weight,height;

void enterdata()
{
cout<<"\nEnter student name : ";
getline(cin,name);
cout<<"\nEnter student age : ";
cin>>age;
cout<<"\nEnter gender : ";
cin>>sex;
cout<<"Enter height(in m) : ";
cin>>height;
cout<<"Enter Weight(in kg) : ";
cin>>weight;
}

void showdata()
{
cout<<"\nThe student name is : "<<name;
cout<<"\nThe student age is: "<<age;
cout<<"\nThe gender is: "<<sex;
cout<<"\nThe height is(in m) : "<<height;
cout<<"\nThe Weight is(in kg) : "<<weight;
}
};

int main()
{
Student S;

S.enterdata();

fstream outfile;
outfile.open("Input.txt",ios::out);

outfile.write((char*)&S, sizeof(S));
outfile.close();

Student D;
fstream infile;
infile.open("Input.txt",ios::in);
infile.read((char*)&D, sizeof(D));
D.showdata();
outfile.close();

return 0;
}
OUTPUT
EXPERIMENT – 32

AIM
Write a program to perform the deletion of white spaces such as horizontal tab, vertical tab, space,
line feed, new line and carriage return from a text file and store the contents of the file without the white
spaces on another file.
SOURCE CODE

#include <iostream> #include


<fstream> using namespace std;

int main()

{
int n;
char str1[100], str2[100];
cout << "Enter no. of lines : "; cin >> n;
fstream outfile; outfile.open("exp8in.txt", ios::out); cout
<< "Writing file...." << endl; cin.get();
for (int i = 1; i <= n; i++)

{
cout << "Type something : "; gets(str1);
outfile << str1 << endl;
}
outfile.close();

fstream fin("exp8in.txt"); fstream


fout("exp8out.txt"); string s;
while (fin >> s)

{
fout << s;
}
fin.close();
fout.close();
fstream infile; infile.open("exp8out.txt", ios::in); cout <<
"Reading from file : " << endl; while (infile.getline(str2,
100))
{
puts(str2);
}
infile.close();

return 0;
}
OUTPUT
EXPERIMENT – 33

AIM
Write a program to raise an exception if any attempt is made to refer to an element whose index is
beyond the array size.
SOURCE CODE
#include <iostream>

using namespace std;

class Array
{
private :
int a[100];
int size;

public :
Array(int y)
{
size = y;
for (int i = 0;i<size;i++)
{
cin>>a[i];
}
showArray();
}

void showArray()
{
cout<<"\nThe array is :";
for(int i = 0;i< size;i++)
{
cout<<a[i]<<" ";
}
}

float accessElement(int index)


{
if((index<0)||(index>=size))
{
throw "Index is out of bounds.Exception caused.";
}
return a[index];
}
};

int main()
{
int size,i;
cout << "Enter size of array :" << endl;
cin>>size;
cout<<"\nEnter array : ";
Array a(size);
cout<<"\nEnter index of element to be accessed : ";
cin>>i;
try
{
int element = a.accessElement(i);
cout<<"\nThe element is : "<<element;
}catch(const char* msg)
{
cout<<msg<<endl;
}
return 0;
}
OUTPUT
EXPERIMENT – 34

AIM
Write a program to read two numbers and then divide first no by second no and raise an exception if
second number is zero.
SOURCE CODE
#include <iostream>

using namespace std;

class Divide
{
private :
float a;
float b;

public :
Divide(int x,int y)
{
a=x;
b=y;
}

float Division()
{
if(b==0)
{
throw "Denominator is zero.Exception caused.";
}
return (a/b);
}

};

int main()
{
float a,b;
cout<<"\nEnter element 1 : ";
cin>>a;
cout<<"\nEnter element 2 : ";
cin>>b;
Divide d(a,b);

try {
float z = d.Division();
cout<<"\nThe answer is :"<<z;
}
catch (const char* msg){
cout<<msg<<endl;
}
return 0;
}
OUTPUT

You might also like