PRACTICAL FILE
OF
OBJECT ORIENTED PROGRAMMING LAB
USING C++(LC-CSE-214G)
BACHELOR OF TECHNOLOGY
IN
COMPUTER SCIENCE & ENGINEERING
Submitted To:- Submitted By:-
Ms. Ritu Kadyan Name:-
Assistant Professor College Roll_no.:-
Computer Sci. & Engg. Dept. Branch & Sem:-
GANGA INSTITUTE OF TECHNOLOGY AND
MANAGEMENT
(Accredited by NAAC “A” Grade)
Approved By AICTE New Delhi, Affiliated to Maharshi Dayanand University, Rohtak
V.P.O- Kablana, Jhajjar, Haryana-124104
INDEX
SR.NO. PRACTICAL NAME DONE CHECKED TEACHERS’S REMARKS
ON ON SIGNATURE
1 Write a program that uses a class where
the member functions are
defined inside a class.
2 Write a program that uses a class where
the member functions are defined
outside a class.
3 Write a program to demonstrate the use
of static data members.
4 Write a program to demonstrate the use
of const data members
5 Write a program to demonstrate the
use of zero argument
and parameterized constructors.
6 Write a program to demonstrate the use
of dynamic
constructor
7 Write a program to demonstrate the use
of explicit constructor.
8 Write a program to demonstrate the use
of initializer list.
9 Write a program to demonstrate the
overloading of increment and
decrement operators.
10 Write a program to demonstrate the
overloading of binary arithmetic
operators.
11 Write a program to demonstrate the
overloading of memory management
operators.
12 Write a program to demonstrate the
multilevel inheritance.
13 Write a program to demonstrate the
multiple inheritance.
14 Write a program to demonstrate the
virtual derivation of a class.
15 Write a program to demonstrate the
runtime polymorphism.
16 Write a program to demonstrate the
exception handling.
17 Write a program to demonstrate the
use of function template.
18 Write a program to demonstrate the use
of class template.
PRACTICAL-1
Aim:- Write a program that uses a class where the member functions
are defined inside the class.
#include <iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
};
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
OUTPUT
Enter car number : 9999
Enter car model : Sedan
Car number is 9999
Car model is Sedan
PRACTICAL-2
Aim:- Write a program that uses a class where the member functions defined
outside a class.
#include <iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata(); //function declaration
void showdata();
};
// function definition
void car::getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void car::showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
OUTPUT
Enter car number : 9999
Enter car model : Sedan
Car number is 9999
Car model is Sedan
PRACTICAL-3
Aim:-Write a program to demonstrate the use of static data members.
#include <iostream>
usingnamespace std;
class Demo
{
private:
staticint X;
public:
staticvoid fun()
{
cout <<"Value of X: " << X << endl;
}
};
//defining
int Demo :: X =10;
int main()
{
Demo X;
X.fun();
return 0;
}
OUTPUT
Value of X: 10
VPRACTICAL-4
Value of: 10
Aim:- Write a program to demonstrate the use of const data member.
#include<iostream>
using namespace std;
class Demo {
int val;
public:
Demo(int x = 0) {
val = x;
}
int getValue() const {
return val;
}
};
int main() {
const Demo d(28);
Demo d1(8);
cout << "The value using object d : " << d.getValue();
cout << "\nThe value using object d1 : " << d1.getValue();
return 0;
}
OUTPUT
The value using object d : 28
The value using object d1 : 8
PRACTICAL-5
Aim:- Write a program to demonstrate the use of zero argument and
parameterized constructors.
#include <iostream>
usingnamespace std;
class Demo
{
private:
int A;
int B;
int C;
public:
Demo();
void set(int A, int B, int C);
void print();
};
Demo::Demo()
{
A=1;
B=1;
C=1;
}
void Demo::set(int A, int B, int C)
{
this->A = A;
this->B = B;
this->C = C;
}
void Demo::print()
{
cout<<"Value of A : "<<A<<endl;
cout<<"Value of B : "<<B<<endl;
cout<<"Value of C : "<<C<<endl;
}
int main()
{
Demo obj = Demo(); //Constructor called when object created
obj.print();
obj.set(10,20,30);
obj.print();
return 0;
}
OUTPUT
Value of A : 1
Value of B : 1
Value of C : 1
Value of A : 10
Value of B : 20
Value of C : 30
PRACTICAL-6
Aim:- Write a program that to demonstrate the use of dynamic constructors.
#include <iostream>
usingnamespace std;
classABC {
constchar* p;
public:
// default constructor
ABC()
{
// allocating memory at run time
p = newchar[6];
p = "fair";
}
void display()
{
cout << p << endl;
}
};
int main()
{
ABC obj;
obj.display();
}
OUTPUT
fair
PRACTICAL-7
Aim:- Write a program to demonstrate the use of explicit constructor.
#include<iostream.h>
class A
{
int data;
public:
A(int a):data(a)
{
cout<<"A::Construcor...\n";
cout<<"value of data :="<<data<<endl;
};
};
int main()
{
A a1 = 37;
return (0);
}
./a.out
A::Construcor...
value of data :=37
PRACTICAL-8
Aim:- Write a program to demonstrate the use of initializer list.
#include <iostream>
usingnamespace std;
//Class declaration.
class Demo
{
//Private block to declare data member( X,Y ) of integer type.
private:
constint X;
constint Y;
//Public block of member function to access data members.
public:
//Const member can only be initialized with member initializer list.
//Declaration of defalut constructor.
Demo():X(10),Y(20){};
//Declaration of parameterized constructor to initialize data members by
member intializer list.
Demo (int a, int b) : X(a),Y(b){};
//To display output onn screen.
void Display();
};//End of class
//Definition of Display() member function.
void Demo:: Display()
{
cout << endl << "X: " << X;
cout << endl << "Y: " << Y << endl;
}
int main()
{
//Ctor automatically call when object is created.
Demo d1; //Default constructor
Demo d2(30,40) ; //Parameterized constructor.
//Display value of data member.
cout << "Value of d1: " ;
d1.Display();
cout << "Value of d2: ";
d2.Display();
return 0;
}
OUTPUT
Value of d1:
X: 10
Y: 20
Value of d2:
X: 30
Y: 40
PRACTICAL-9
Aim:- Write a program to demonstrate the use of overloading of increment
and decrement operators.
Increment
#include <bits/stdc++.h>
usingnamespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the prefix operator
Integer operator++()
{
Integer temp;
temp.i = ++i;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before increment: ";
i1.display();
// Using the pre-increment operator
Integer i2 = ++i1;
cout << "After pre increment: ";
i2.display();
}
OUTPUT
Before increment: i = 3
After pre increment: i = 4
DECREMENT
#include <bits/stdc++.h>
usingnamespace std;
class Integer {
private:
int i;
public:
// Parameterised constructor
Integer(int i = 0)
{
this->i = i;
}
// Overloading the postfix operator
Integer operator--(int)
{
Integer temp;
temp.i = i--;
return temp;
}
// Function to display the value of i
void display()
{
cout << "i = " << i << endl;
}
};
// Driver function
int main()
{
Integer i1(3);
cout << "Before decrement: ";
i1.display();
// Using the post-decrement operator
Integer i2 = i1--;
cout << "After post decrement: ";
i2.display();
}
OUTPUT
Before decrement: i = 3
After post decrement: i = 3
PRACTICAL-10
Aim:- Write a program to demonstrate the overloading of binary arithmetic
operator.
#include<iostream.h>
#include<conio.h>
class complex {
int a, b;
public:
void getvalue() {
cout << "Enter the value of Complex Numbers a,b:";
cin >> a>>b;
}
complex operator+(complex ob) {
complex t;
t.a = a + ob.a;
t.b = b + ob.b;
return (t);
}
complex operator-(complex ob) {
complex t;
t.a = a - ob.a;
t.b = b - ob.b;
return (t);
}
void display() {
cout << a << "+" << b << "i" << "\n";
}
};
void main() {
clrscr();
complex obj1, obj2, result, result1;
obj1.getvalue();
obj2.getvalue();
result = obj1 + obj2;
result1 = obj1 - obj2;
cout << "Input Values:\n";
obj1.display();
obj2.display();
cout << "Result:";
result.display();
result1.display();
getch();
}
OUTPUT
Enter the value of Complex Numbers a, b
4 5
Enter the value of Complex Numbers a, b
2 2
Input Values
4 + 5i
2 + 2i
Result
6 + 7i
2 + 3i
PRACTICAL-11
Aim:- Write program to demonstrate the overloading of memory
management operator.
#include<iostream>
#include<stdlib.h>
usingnamespace std;
class student
{
string name;
int age;
public:
student()
{
cout<< "Constructor is called\n" ;
}
student(string name, int age)
{
this->name = name;
this->age = age;
}
void display()
{
cout<< "Name:" << name << endl;
cout<< "Age:" << age << endl;
}
void * operator new(size_t size)
{
cout<< "Overloading new operator with size: " << size << endl;
void * p = ::operator new(size);
//void * p = malloc(size); will also work fine
return p;
}
void operator delete(void * p)
{
cout<< "Overloading delete operator " << endl;
free(p);
}
};
int main()
{
student * p = new student("Yash", 24);
p->display();
delete p;
OUTPUT
Overloading new operator with size: 40
Name:Yash
Age:24
Overloading delete operator
PRACTICAL-12
Aim:- Write a program to demonstrate the multilevel inheritance.
#include <iostream>
using namespace std;
class A {
public:
void display() {
cout<<"Base class content.";
}
};
class B : public A {};
class C : public B {};
int main() {
C obj;
obj.display();
return 0;
}
OUTPUT
Base class content.
PRACTICAL-13
Aim:-Write a program to demonstrate multiple inheritance.
#include <iostream>
using namespace std;
class Mammal {
public:
Mammal() {
cout << "Mammals can give direct birth." << endl;
}
};
class WingedAnimal {
public:
WingedAnimal() {
cout << "Winged animal can flap." << endl;
}
};
class Bat: public Mammal, public WingedAnimal {};
int main() {
Bat b1;
return 0;
}
OUTPUT
Mammals can give direct birth.
Winged animal can flap.
PRACTICAL-14
Aim:- Write a program a program to demonstrate the runtime polymorphism.
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating...";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread...";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
OUTPUT
Eating bread...
PRACTICAL-15
Aim:- Write a program to demonstrate the exception handling.
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
OUTPUT
Division by zero condition!
PRACTICAL-16
Aim:- Write a program to demonstrate the exception handling.
#include <iostream>
using namespace std;
double division(int a, int b) {
if( b == 0 ) {
throw "Division by zero condition!";
}
return (a/b);
}
int main () {
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
OUTPUT:
Division by zero condition!
PRACTICAL-17
Aim:- Write a program to demonstrate the use of function template.
#include <iostream>
using namespace std;
template <typename T>
T add(T num1, T num2) {
return (num1 + num2);
}
int main() {
int result1;
double result2;
// calling with int parameters
result1 = add<int>(2, 3);
cout << "2 + 3 = " << result1 << endl;
// calling with double parameters
result2 = add<double>(2.2, 3.3);
cout << "2.2 + 3.3 = " << result2 << endl;
return 0;
}
OUTPUT:
2+3=5
2.2 + 3.3 = 5.5
PRACTICAL-18
Aim:- Write a program to demonstrate the use of class template.
// Class template
template <class T>
class Number {
private:
// Variable of type T
T num;
public:
Number(T n) : num(n) {} // constructor
T getNum() {
return num;
}
};
int main() {
// create object with int type
Number<int> numberInt(7);
// create object with double type
Number<double> numberDouble(7.7);
cout << "int Number = " << numberInt.getNum() << endl;
cout << "double Number = " << numberDouble.getNum() << endl;
return 0;
}
OUTPUT
int Number = 7
double Number = 7.