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

Blank (!)

The document contains 22 programs demonstrating various object-oriented programming concepts in C++ like classes, objects, constructors, destructors, inheritance, polymorphism, operator overloading, templates, and exception handling. Each program contains the code, input/output and brief description. The programs cover basic concepts like classes with member functions defined inside and outside the class to advanced topics like multilevel and multiple inheritance, virtual functions, function and class templates, and exception handling.

Uploaded by

503lavikcse1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Blank (!)

The document contains 22 programs demonstrating various object-oriented programming concepts in C++ like classes, objects, constructors, destructors, inheritance, polymorphism, operator overloading, templates, and exception handling. Each program contains the code, input/output and brief description. The programs cover basic concepts like classes with member functions defined inside and outside the class to advanced topics like multilevel and multiple inheritance, virtual functions, function and class templates, and exception handling.

Uploaded by

503lavikcse1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

UIET MDU ROHTAK

PRACTICAL FILE OF
OBJECT ORIENTED PROGRAMMING
(PCC-CSE-208G)

Submitted To: Submitted By:


Dr. Sunita Dhingra Jyoti Yadav
UIET MDU, Rohtak Roll No. 25520
CSE-A (4th Sem.)
INDEX
Sno. Title Pg. Signature

1 Write a program that uses a Class where the member functions are 3
defined inside a class

2 Write a program that uses a Class where the member functions are 4
defined outside a class

3 Write a program to demonstrate the use of static data members 5

4 Write a program to demonstrate the use of const data members. 6

5 Write a program to demonstrate the use of parameterized constructor 7

6 Write a program to demonstrate the use of dynamic constructor. 8

7 Write a program to demonstrate the use of explicit constructor. 9

8 Write a program to demonstrate the use of Initializer List. 10

9 Write a program to demonstrate the overloading of increment and 11


decrement operators.

10 Write a program to demonstrate the overloading of binary arithmetic 12


operators.

11 Write a program to demonstrate the overloading of Memory 13-14


management operators.

12 Write a program to demonstrate the multilevel inheritance 15

13 Write a program to demonstrate the multiple inheritance. 16

14 Write a program to demonstrate virtual derivation of a Class 17

15 Write a program to demonstrate the runtime polymorphism. 18

16 Write a program to demonstrate the exception handling 19

17 Write a program to demonstrate the use of function templates 20

18 Write a program to demonstrate the use of class templates 21

19 adding one object of class DM with another object of class DB: 22-23

20 Class Master derive from classes account and admin which turn derive 24-25
from class person

21 Finding difference of two complex numbers using operator 26-27


overloading

22 Creating zeneric class 28-29


PROGRAM.1
Write a program that uses a Class where the member functions are defined inside a class
[Classes and Objects]

OUTPUT
PROGRAM.2
Write a program that uses a Class where the member functions are defined outside a class
[Classes and Objects]

OUTPUT
PROGRAM.3
Write a program to demonstrate the use of static data members
[Classes & Objects]

OUTPUT

.
PROGRAM .4
Write a program to demonstrate the use of const data members.
[Classes & Objects]

OUTPUT
PROGRAM.5
Write a program to demonstrate the use of parameterized constructor
[Constructors & Destructors]

OUTPUT
PROGRAM.6
Write a program to demonstrate the use of dynamic constructor.
[Constructors & Destructors]

OUTPUT
PROGRAM.7
Write a program to demonstrate the use of explicit constructor.
[Constructors & Destructors]

OUTPUT
PROGRAM.8
Write a program to demonstrate the use of Initializer List.
[Initializer Lists]

OUTPUT
PROGRAM.9
Write a program to demonstrate the overloading of increment and decrement operators.
[Operator Overloading]

OUTPUT
PROGRAM.10
Write a program to demonstrate the overloading of binary arithmetic operators.
[Operator Overloading]

OUTPUT
PROGRAM.11
Write a program to demonstrate the overloading of Memory management operators.
[Operator Overloading]
OUTPUT
PROGRAM.12
Write a program to demonstrate the multilevel inheritance
[Inheritance]

OUTPUT
PROGRAM.13
Write a program to demonstrate the multiple inheritance.
[Inheritance]

OUTPUT
PROGRAM.14
Write a program to demonstrate virtual derivation of a Class
[Inheritance]

OUTPUT
PROGRAM.15
Write a program to demonstrate the runtime polymorphism.
[Polymorphism]

OUTPUT
PROGRAM.16
Write a program to demonstrate the exception handling
[Exception Handling]

OUTPUT
PROGRAM.17
Write a program to demonstrate the use of function templates
[ Templates and Generic Programming]

OUTPUT
PROGRAM.18
Write a program to demonstrate the use of class templates
[Templates and Generic Programming]

OUTPUT
Program 19 :
Write a program that read values for class objects and add one
object of class DM with another object of class DB:

#include<iostream>
using namespace std;
class DB;
class DM{
int value1,value2;
public:
DM(int v1,int v2){
value1 = v1;
value2 = v2;
}

void display(){
cout<<value1<<"m "<< value2<<"cm"<<endl;
}
friend DM addition(DM,DB);
};
class DB{
int value3,value4;
public:
DB(int v3,int v4){
value3 = v3;
value4 = v4;
}

void show(){
cout<<value3<<"feet "<<value4<<"inch "<<endl;
}
friend DM addition(DM,DB);
};
DM addition(DM obj1, DB obj2){
int add;
add = (obj1.value1*100+obj1.value2)+(obj2.value3*12+obj2.value4)*2.54;
int value1,value2;

value1 = (add / 100);


value2 = (add % 100);
DM dm(value1,value2);
return dm;
}
int main(){
DM dm1(5,15);
DB db1(3,10);
DM dm2 = addition(dm1,db1);
cout<<"Distance-1 is: "<<endl;
dm1.display();
cout<<"Distance-2 is: "<<endl;
db1.show();
cout<<"Total distance is: "<<endl;
dm2.display();
}
Program 20:
Class Master derives information from both classes Account and
Admin which in turn derive information from the class
Person.Define all four classes and write a program to
create,update and display the information contained in Master
objects.

#include<iostream>
using namespace std;
class person{
protected:

string name;
int code;

public:
void getdata(string n,int c){
name = n;
code = c;
}
};
class Account:public person{
protected:
int pay;
public:
void getvalue(int p){
pay = p;
}
};
class Admin{
protected:
int experience;
public:

void takevalue(int e){


experience = e;
}
};

class Master:public Account, public Admin{


public:
void display(){
cout<<"Name of person is: "<<name<<endl;
cout<<"Code of person is: "<<code<<endl;
cout<<"Pay of person is: "<<pay<<endl;
cout<<"Experience of person is :"<<experience<<"years"<<endl;
}
};
int main(){
Master m1;
m1.getdata("Ram",12000);
m1.getvalue(50000);
m1.takevalue(5);
m1.display();
}
Program 21: Find difference of two complex numbers using
operator overloading concept:
#include<iostream>
using namespace std;
class complex{
float real,img;
public:
complex():real(0), img(0){
}

void input(){
cout<<"\nenter real and imaginary part of complex number: ";
cin>>real;

cin>>img;
}
complex operator -(complex c2){
complex temp;
temp.real = real-c2.real;
temp.img = img - c2.img;
return temp;
}
complex operator +(complex c2){
complex temp;
temp.real = real+c2.real;
temp.img = img + c2.img;
return temp;
}
void output(){
if(img < 0){
cout<<"The resulted complex number is "<<real<<img<<"i"<<endl;
}
else{
cout<<"The resulted complex number is: "<<real<<"+"<<img<<"i"<<endl;
}
}
};
int main(){
complex c1,c2,result, sumofcomp;
cout<<"Enter first complex number ";
c1.input();
cout<<"Enter second complex number ";
c2.input();
sumofcomp =c1+c2;//added
sumofcomp.output();
result = c1-c2;
result.output();
}
Program 22: Program for zeneric class:
#include<iostream>
using namespace std;
#include <iostream>
#include <string>
using namespace std;
template <class T>
class Stack
{
public:
Stack();
void push(T i);
T pop();
private:
int top;
T st[2];
};

template <class T>


Stack<T>::Stack()
{
top = -1;
}

template <class T>


void Stack<T>::push(T i)
{
st[++top] = i;
}

template <class T>


T Stack<T>::pop()
{
return st[top--];
}

void func(int top) {


try {
if(top==-1) throw 10;
if(top==top-1) throw 's';
}
catch(int) {
cout << "Underflow Exception!\n";
}
catch(float) {
cout << "Overflow Exception!\n";
}
}
int main ()
{
Stack<int> int_stack;
Stack<string> str_stack;
int_stack.push(10);
str_stack.push("Hello");
str_stack.push("Hi");
cout << int_stack.pop() << endl;
cout << str_stack.pop() << endl;
cout << str_stack.pop() << endl;
func(0);
func(1);
return 0;
}

You might also like