0% found this document useful (0 votes)
7 views25 pages

C++ Object Oriented Programming Lab Guide

This document is a practical file for the Object Oriented Programming Lab using C++, detailing various programming exercises and their aims. It includes a list of 16 practicals covering topics such as class definitions, constructors, operator overloading, inheritance, polymorphism, exception handling, and templates. Each practical contains code examples and expected outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views25 pages

C++ Object Oriented Programming Lab Guide

This document is a practical file for the Object Oriented Programming Lab using C++, detailing various programming exercises and their aims. It includes a list of 16 practicals covering topics such as class definitions, constructors, operator overloading, inheritance, polymorphism, exception handling, and templates. Each practical contains code examples and expected outputs.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PRACTICAL FILE

OF
OBJECT ORIENTED PROGRAMMING LAB
USING C++
(LC-CSE-214G)

[Link] CSE 4TH SEM

Submitted To: Submitted By:


Ms. Poonam
Assistant Professor
CSE Deptt.

DEPARTMENT OF COMPUTER SCIENCE &ENGINEERING

GANGA INSTITUTE OF TECHNOLOGY AND


MANAGEMENT, KABLANA
COURSE NAME: - OBJECT ORIENTED PROGRAMMING LAB USING C++
COURSE CODE: LC-CSE-214G

List of Practicals
[Link]. PROGRAM DATE OF DATE OF SIGNATURE
EXECUTION CHECKING

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 overloading
of increment and decrement operators.
8 Write a program to demonstrate the overloading
of binary arithmetic operators.
9 Write a program to demonstrate the overloading
of memory management operators.
10 Write a program to demonstrate the multilevel
inheritance.
11 Write a program to demonstrate the multiple
inheritances.
12 Write a program to demonstrate the virtual
derivation of a class.
13 Write a program to demonstrate the runtime
polymorphism.
14 Write a program to demonstrate the exception
handling.
15 Write a program to demonstrate the use of
function template.
16 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;
[Link]();
[Link]();
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;
}
int main()
{
car c1;
[Link]();
[Link]();
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>
Using namespace std;

class Demo
{
private:
staticint X;

public:
staticvoid fun()
{
cout <<"Value of X: " << X << endl;
}
};

//defining
int Demo :: X =10;

int main()
{
Demo X;

[Link]();

return 0;
}

OUTPUT

Value of X: 10
Value of

PRACTICAL-4
Value of X: 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 : " << [Link]();
cout << "\nThe value using object d1 : " << [Link]();
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
[Link]();
[Link](10,20,30);
[Link]();

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;
[Link]();
}

OUTPUT

fair

PRACTICAL-7

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: ";


[Link]();

// Using the pre-increment operator


Integer i2 = ++i1;

cout << "After pre increment: ";


[Link]();
}

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: ";
[Link]();

// Using the post-decrement operator


Integer i2 = i1--;

cout << "After post decrement: ";


[Link]();
}

OUTPUT

Before decrement: i = 3
After post decrement: i = 3

PRACTICAL-8

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;

[Link]();
[Link]();

result = obj1 + obj2;


result1 = obj1 - obj2;

cout << "Input Values:\n";


[Link]();
[Link]();

cout << "Result:";


[Link]();
[Link]();

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-9

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-10

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;
[Link]();
return 0;
}

OUTPUT

Base class content.

PRACTICAL-11

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-12

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();
[Link]();
return 0;
}

OUTPUT

Eating bread...

PRACTICAL-13

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-14

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-15

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-16

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 = " << [Link]() << endl;


cout << "double Number = " << [Link]() << endl;

return 0;
}

OUTPUT

int Number = 7
double Number = 7

You might also like