Practical Guide OPP - Object Oriented Programming
Practical Guide OPP - Object Oriented Programming
Guide
OBJECT ORIENTED PROGRAMMING
DCSD/DSE/DNE
National Institute of Business Management
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Table of Contents
Session 1 – Practical Exercises ....................................................................................................... 2
Session 2 – Practical Exercises ....................................................................................................... 4
Session 3 – Practical Exercises ....................................................................................................... 7
Session 4 – Practical Exercises ..................................................................................................... 13
Session 5 – Practical Exercises ..................................................................................................... 15
Session 6 – Practical Exercises ..................................................................................................... 18
Session 7 – Practical Exercises ..................................................................................................... 21
DCSD/DSE/DNE 1 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
#include<iostream>
int main()
{
cout<<”I am starting my c++ programming”;
return 0;
}
Experiment 1
#include <iostream>
using namespace std;
int m = 10;
int main()
{
int m = 20;
cout << "m = " << m << "\n";
cout << "::m = " << ::m << "\n";
return 0;
}
DCSD/DSE/DNE 2 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 2
#include <iostream>
using namespace std;
int main()
{
int a=10;
int &b = a;
return 0;
}
2. Type a C++ program to input two integer numbers, add and display the total.
3. Type a C++ program to input three integer values into K, L, G and display output of A.
A=K+L/2*7-2+G
5. Type a C++ program to input 10 numbers into an array and display the average value.
DCSD/DSE/DNE 3 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 1
#include <iostream>
using namespace std;
int main(){
//Calling the function
sum(1,99);
return 0;
}
DCSD/DSE/DNE 4 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 2
#include <iostream>
using namespace std;
int main()
{
cout << "Sum is : " << sum(5) << endl;
cout << "Sum is : " << sum(5,15) << endl;
cout << "Sum is : " << sum(5,15,25) << endl;
return 0;
}
DCSD/DSE/DNE 5 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 3
#include <iostream>
using namespace std;
void printChar();
void printChar( char c );
void printChar( char c, int num );
void printChar(int num, char c);
int main()
{
printChar();
printChar('#');
printChar(10,'$');
printChar('@',10);
cout<< endl;
return 0;
}
void printChar()
{
cout<< endl<<"%";
}
void printChar( char c )
{
cout<< endl<< c;
}
DCSD/DSE/DNE 6 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
cout<< endl;
for(i=0; i< num; i++)
cout<< c;
}
void printChar(int num, char c)
{
int i=0;
cout<< endl;
for(i=0; i< num; i++)
cout<< c;
}
DCSD/DSE/DNE 7 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Function1 : Set three arguments as unit charge, number of units used and tax rate
Set all argument as constants
Calculate and display bill amount, tax rate
Info: bill amount= unit charge*number of units used
Tax amount=bill amount * tax rate
Pass values for unit charge, number of units used and tax rate calling Function1 in main
function
DCSD/DSE/DNE 8 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 1
#include <iostream>
using namespace std;
class Hello
{
public:
void sayHello()
{
cout << "Hello World" << endl;
}
};
int main()
{
Hello h;
h.sayHello();
return 0;
}
DCSD/DSE/DNE 9 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 2
#include <iostream>
#include <string>
using namespace std;
class Student
{
public: // Access specifier
int rollNo; // Attribute (integer variable)
string stdName; // Attribute (string variable)
float perc; // Attribute (float variable)
};
int main()
{
//object creation
Student std;
return 0;
}
DCSD/DSE/DNE 10 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 3
#include <iostream>
using namespace std;
class test
{
int code;
static int count;
public:
void setcode()
{
code = ++count;
}
void showcode()
{
cout << "object number: " << code << "\n";
}
static void showcount()
{
cout << "count: " << count << "\n";
}
};
int test :: count;
int main()
{
test t1, t2;
t1.setcode();
t2.setcode();
DCSD/DSE/DNE 11 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
test :: showcount();
test t3;
t3.setcode();
test :: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
1. Class Employee has private properties as employee code, grade, income. Class also
maintains public functions as setData() ,findIncome() and displayIncome(). The function
setData() sets code and grade of an employee. The function findIncome() calculates the
income of an employee according to the criteria given below. The function displayIncome()
displays code and income of an employee.
Type a C++ (object-oriented program) to set data, find income and display income for two
employees.
Info: income=basic salary + hour charge * number of hours
DCSD/DSE/DNE 12 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 1
#include <iostream>
class Wall {
private:
double length;
public:
Wall() {
length = 5.5;
};
int main() {
Wall wall1;
return 0;
DCSD/DSE/DNE 13 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
1. Class Item has private properties as item number, item price, available quantity and total
value. The class also has public functions findValue() and displayValue().
Use default constructor to assign item number, item price and available quantity of an item.
The function findValue() which find total value of an item. The function displayValue()
display total value of an item. Type a C++ program to find and display total value of one
item.
2. Do above program for two items. Use parameterized constructor to assign values for item
number, item price and available quantity of two items.
Steps:
DCSD/DSE/DNE 14 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 1
#include <iostream>
using namespace std;
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
}
DCSD/DSE/DNE 15 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 2
#include <iostream>
using namespace std;
class M
{
protected:
int m;
public:
void get_m(int x)
{
m = x;
}
};
class N
{
protected:
int n;
public:
void get_n(int y)
{
n = y;
}
};
DCSD/DSE/DNE 16 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}
1. Class Person has two protected properties as number and name. The class has a public
function setPersondata() which sets number and name of a person.
Class Student has a private property module mark. The class also has a public function
setStudentData() which set module mark of a student and a public function getStatus()
which displays status of a student based on mark with number and name. If mark is greater
than 50, status is positive, otherwise negative.
Class Employee has a private property number of working years. The class also has a
public function setEmployeeData() which set number of working years and a public
function findGrade() which display the grade of an employee based on number of years
with number and name. If number of working years is greater than 5, grade is 1, otherwise
grade is 2.
Type a C++ program to;
• Set number, name , mark of student and display the student status with
number and name
• Set number, name, number of working years and display the employee
DCSD/DSE/DNE 17 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 1
#include <iostream.h>
class A
{
public:
virtual void display()
{
cout << "Base class is invoked"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout << "Derived Class is invoked"<<endl;
}
};
int main()
{
A* a; //pointer of base class
B b; //object of derived class
a = &b;
a->display(); //Late Binding occurs
}
DCSD/DSE/DNE 18 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 2
#include <iostream>
class A
public:
};
class B:public A
public:
void example()
cout<<"Class - B";
};
class C:public A
public:
void example()
cout<<"Class - C";
DCSD/DSE/DNE 19 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
};
void main()
A* arra[2];
B e1;
C e2;
arra[0]=&e1;
arra[1]=&e2;
arra[0]->example();
arra[1]->example();
1. Class Employee has a virtual function findSalary() which receives basic salary, monthly
allowance and find monthly income.
Class JuniorEmployee uses function findSalary() in class Employee and finds monthly
income of a junior employee. Type an object oriented program to find monthly income of
one junior employee.
Hint: employee monthly income= basic salar + monthly allowance
junior employee monthly income=daily payment * number of days worked
DCSD/DSE/DNE 20 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 1
# include <iostream.h>
# include <fstream.h>
void main()
char name[10];
float price;
ofstream outf("ITEM.txt");
outf.close();
ifstream inf("ITEM.txt");
inf.close();
DCSD/DSE/DNE 21 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 2
#include <iostream.h>
#include <fstream.h>
int main()
{
ofstream outf("txtFile.txt");
cout<<outf.tellp()<<endl;
return 0;
}
DCSD/DSE/DNE 22 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
Experiment 3
# include <iostream.h>
# include <fstream.h>
void main()
char name[10],response;
float price;
oute.open("EXPENSIVE.txt");
outc.open("CHEAP.txt");
do
else
DCSD/DSE/DNE 23 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
cout << "\nDo u want to enter another record (y/n) : "; cin >> response;
oute.close();
outc.close();
ifstream inf;
inf.open("EXPENSIVE.txt");
while(inf)
inf.close();
inf.open("CHEAP.txt");
while(inf)
DCSD/DSE/DNE 24 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE
inf.close();
1. Write a C++ program to read number and name of the students in file “Student”.
2. Class product has private properties product number, product name and product price. This
class has public functions recordData() ,updateData() and searchData(). The function
recordData() records product number, product name and product price in file
“productdata”. The function searchData() search and display product name and product
price of a particular product number in file “productdata”. The function updateData()
updates product number, product name of a particular product in file “productdata”.
Write an object-oriented program to record, update and display product details
DCSD/DSE/DNE 25 | PAGE