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

Practical Guide OPP - Object Oriented Programming

The document provides practical exercises on object oriented programming concepts across 7 sessions. It includes sample C++ programs and instructions for students to type the programs, compile, run and observe the output. The exercises cover topics like functions, classes, objects, static members.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Practical Guide OPP - Object Oriented Programming

The document provides practical exercises on object oriented programming concepts across 7 sessions. It includes sample C++ programs and instructions for students to type the programs, compile, run and observe the output. The exercises cover topics like functions, classes, objects, static members.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Practical

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

Session 1 – Practical Exercises

1. Type following program and get the output.

#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;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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;

cout << endl << "Value of a: " << a;


cout << endl << "Value of b: " << b << endl;

return 0;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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

4. Type a C++ program to input 10 numbers in to array and display them.

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

Session 2 – Practical Exercises

Experiment 1

#include <iostream>
using namespace std;

void sum(int num1, int num2);

int main(){
//Calling the function
sum(1,99);
return 0;
}

void sum(int num1, int num2)


{
cout << "Total : " << (num1+num2) << endl;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

DCSD/DSE/DNE 4 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

Experiment 2

#include <iostream>
using namespace std;

int sum(int x, int y=10, int z=20)


{
return (x+y+z);
}

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;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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

void printChar( char c, int num )


{
int i=0;

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;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

DCSD/DSE/DNE 7 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

1. Write C++ program with a function.

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

Session 3 – Practical Exercises

Experiment 1

#include <iostream>
using namespace std;

class Hello
{
public:
void sayHello()
{
cout << "Hello World" << endl;
}
};

int main()
{
Hello h;

h.sayHello();

return 0;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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;

// Accessing attributes and setting the values


std.rollNo = 101;
std.stdName = "Shivang Yadav";
std.perc = 98.20f;

// Printing the values


cout << "Student's Roll No.: " << std.rollNo << "\n";
cout << "Student's Name: " << std.stdName << "\n";
cout << "Student's Percentage: " << std.perc << "\n";

return 0;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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

Grade Basic salary Hour charge


1 8000 100
2 5000 75

DCSD/DSE/DNE 12 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

Session 4 – Practical Exercises

Experiment 1

#include <iostream>

using namespace std;

class Wall {

private:

double length;

public:

Wall() {

length = 5.5;

cout << "Creating a wall." << endl;

cout << "Length = " << length << endl;

};

int main() {

Wall wall1;

return 0;

DCSD/DSE/DNE 13 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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

Session 5 – Practical Exercises

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);

// Print the area of the object.


cout << "Total area: " << Rect.getArea() << endl;

return 0;
}

DCSD/DSE/DNE 15 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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;
}
};

class P : public M, public N


{
public:
void display(void)
{
cout << "m = " << m << "\n";
cout << "n = " << n << "\n";

DCSD/DSE/DNE 16 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

cout << "m*n = " << m*n << "\n";


}
};

int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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

Session 6 – Practical Exercises

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
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

DCSD/DSE/DNE 18 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

Experiment 2

#include <iostream>

using namespace std;

class A

public:

virtual void example()=0;

};

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();

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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

2. Rewrite previous program assuming function findSalary() in class Employee as pure


virtual.
Steps:

DCSD/DSE/DNE 20 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

Session 7 – Practical Exercises

Experiment 1
# include <iostream.h>

# include <fstream.h>

void main()

char name[10];

float price;

ofstream outf("ITEM.txt");

cout << "\nEnter item name : "; cin >> name;

cout << "\nEnter item price : "; cin >> price;

outf << name << "\n";

outf << price << "\n";

outf.close();

ifstream inf("ITEM.txt");

inf >> name;

inf >> price;

cout << "\nItem name : " << name;

cout << "\nItem price : " << price;

inf.close();

DCSD/DSE/DNE 21 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

Experiment 2

#include <iostream.h>
#include <fstream.h>
int main()
{
ofstream outf("txtFile.txt");
cout<<outf.tellp()<<endl;

outf << "NIBM" ;


cout<<outf.tellp()<<endl;
outf.seekp(10);
outf << "Sri Lanka" ;
outf.close();

return 0;
}

Step 1: Open an Empty File.


Step 2: type the following C++ program.
Step 3: Save the above program as Program.cpp in your working directory.
Step 4: Compile and run the above program
Step 5: Write your observations and conclusions

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;

ofstream oute, outc;

oute.open("EXPENSIVE.txt");

outc.open("CHEAP.txt");

do

{ cout << "\nEnter item name : "; cin >> name;

cout << "\nEnter item price : "; cin >> price;

if (price > 100)

{ oute << name << "\n";

oute << price << "\n";

else

{ outc << name << "\n";

DCSD/DSE/DNE 23 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

outc << price << "\n";

cout << "\nDo u want to enter another record (y/n) : "; cin >> response;

} while (response =='y');

oute.close();

outc.close();

ifstream inf;

inf.open("EXPENSIVE.txt");

cout << "\nExpensive Items ";

while(inf)

{ inf >> name;

inf >> price;

cout << "\nItem name : " << name;

cout << "\nItem price : " << price;

inf.close();

inf.open("CHEAP.txt");

cout << "\nCheap Items ";

while(inf)

{ inf >> name;

inf >> price;

cout << "\nItem name : " << name;

DCSD/DSE/DNE 24 | PAGE
OBJECT ORIENTED PROGRAMMING - PRACTICAL GUIDE

cout << "\nItem price : " << price;

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

You might also like