Object Oriented Programming Lab Mannual
Object Oriented Programming Lab Mannual
Submitted to:
Mr. Javed Abbas
Submitted By:
Wishaq Akbar
(2023-BS-AI-041)
Lab 01 ------------------------------------------------------------------------------3
Lab 02 ------------------------------------------------------------------------------6
Lab 03 ------------------------------------------------------------------------------8
Lab 04 (Mid Exam) ------------------------------------------------------------- 12
Lab 05 ---------------------------------------------------------------------------- 15
Lab 06 ---------------------------------------------------------------------------- 18
Lab 07 ---------------------------------------------------------------------------- 22
Lab 08 ---------------------------------------------------------------------------- 25
Lab 01
Task 1: Write a program that declare a structure to store the Roll Number, Marks, Average
and Grades of a student. The program should define a structure variable, input the value
and then display this value.
Solution:
OUTPUT
#include <iostream>
using namespace std;
struct student{
int rollNum;
int marks;
int avg;
char grade;
};
int main(){
student s1;
cout<<endl;
cout<<"***STUDENT'S DATA***"<<endl;
3
Task 2: Write a program that declare a structure to store Book ID, Price, and Page of a Book.
It defines two structure variable and input values.it display the record of most costly book.
Solution:
OUTPUT
#include <iostream>
using namespace std;
struct book{
int Id;
int price;
int pages;
};
int main(){
// Book 1 Data
cout<<"Enter the Book Id: ";
cin>>b1.Id;
cout<<"Enter the Book Price: $";
cin>>b1.price;
cout<<"Enter the Book Pages: ";
cin>>b1.pages;
cout<<endl;
// Book 2 Data
cout<<"Enter the Book Id: ";
cin>>b2.Id;
cout<<"Enter the Book Price: $";
cin>>b2.price;
cout<<"Enter the Book Pages: ";
cin>>b2.pages;
if(b1.price>b2.price)
costlyBook = b1;
else
costlyBook = b2;
cout<<endl;
4
Task 3: Write a program that declare a structure to store the Employee ID and Salary of
employee. It defines and initialize the structure variable and display it.
Solution: OUTPUT
#include <iostream>
using namespace std;
struct emp{
int emp_Id;
int emp_Sal;
};
int main(){
emp e1;
cout<<endl;
cout<<"***EMPLOYEE'S DATA***"<<endl;
5
Lab 02
Task 1: Modify Experiment 1 and add function for subtraction, multiplication and
division. Also add another function which take three argument and perform arithmetic
calculation on those argument inside user defined function. You can ask from user to
perform operation on 2 operands or 3 operands.
Solution: OUTPUT
#include <iostream>
using namespace std;
int main() {
int choice;
cout<<"Number of Operands to Perform (2 OR 3): "<<endl;
cin>>choice;
float num1=0, num2=0, num3=0;
if(choice==2){
cout<<"Enter First Number: ";
cin>>num1;
cout<<"Enter Second Number: ";
cin>>num2;
}else{
6
cout<<"Enter First Number: ";
cin>>num1;
cout<<"Enter Second Number: ";
cin>>num2;
cout<<"Enter Third Number: ";
cin>>num3;
}
Add(num1, num2, num3);
sub(num1, num2, num3);
mul(num1, num2, num3);
divd(num1, num2, num3);
return 0;
}
Solution:
OUTPUT
#include <iostream>
#include <iomanip>
using namespace std;
void celcius(){
cout<<"Table for Celcius: "<<endl;
for(int i=0; i<100; i++){
cout<<i<<"\t"<<i * 1.8 + 32<<endl;
}
}
void fahren(){
cout<<"Table for Fahrenheit: "<<endl;
for(int i=32; i<212; i++){
cout<<i<<"\t"<<setw(10)<<(i - 32) * 0.5556 <<endl;
}
}
int main(){
celcius();
cout<<endl<<endl;
fahren();
}
7
Lab 03
Task 1: Write a class Mark with the data members to store the three marks. Write three
member functions in() to input marks, sum() to calculate and return the sum and avg() to
calculate and return the average marks.
Solution: OUTPUT
#include <iostream>
using namespace std;
class Marks{
public:
int mark1, mark2, mark3;
void in(){
cout<<"Enter Marks 1: ";
cin>>mark1;
cout<<"Enter Marks 2: ";
cin>>mark2;
cout<<"Enter Marks 3: ";
cin>>mark3;
}
int sum(){
return mark1+mark2+mark3;
}
double avg(){
return sum()/3;
}
};
int main(){
Marks stdMarks;
stdMarks.in();
8
Task 2: Write a class with one data member radius. Write three member functions get
radius() to set radius value with parameter value, area() to display radius and circum() to
calculate and display circumference of circle.
Solution:
OUTPUT
#include <iostream>
using namespace std;
class circ {
double radius;
public:
void getRadius() {
cout<<"Enter the radius of the circle: ";
cin>>radius;
}
void area() {
double circleArea = 3.14*radius*radius;
cout<<"Area of the circle: "<<circleArea<<endl;
}
void circum() {
double circumference = 2*3.14*radius;
cout << "Circumference of the circle: "<< circumference << endl;
}
};
int main() {
circ myCircle;
myCircle.getRadius();
myCircle.area();
myCircle.circum();
return 0;
}
Task 2: Write a class for bank account that include the following data members
• Name of depositor
• Account number
• Type of account
• Balance in account
The class contain the following member function
9
• Display function display the name and balance
Solution:
OUTPUT
#include <iostream>
using namespace std;
class bankAcc {
private:
char name[50];
int accNum;
char accType[50];
double accBal;
double newBal;
public:
bankAcc() {
cout<<"Enter Name of the Depositor: ";
cin>>name;
cout<<"Enter Account Number: ";
cin>>accNum;
cout<<"Enter Account Type: ";
cin>>accType;
cout<<"Enter Account Balance: ";
cin>>accBal;
newBal = accBal;
}
void displayInfo() {
cout<<"Name of the Account Holder: "<<name<<endl;
cout << "Account Balance: "<<newBal<<endl;
}
};
int main() {
bankAcc acc1;
cout<<endl;
10
double depositAmount;
cout<<"Enter Amount to Deposit: ";
cin>>depositAmount;
acc1.deposit(depositAmount);
cout<<endl;
double withdrawAmount;
cout<<"Enter Amount to Withdraw: ";
cin>>withdrawAmount;
acc1.withdraw(withdrawAmount);
cout<<endl;
acc1.displayInfo();
}
11
Lab 04 (Mid Exam)
Task 1: Create a class called time that has separate int member data for hours, minutes,
and seconds. One constructor should initialize this data to 0, and another should initialize
it to fixed values. Another member function should display it, in 11:59:59 format. The final
member function should add two objects of type time passed as arguments. A main ()
program should create two initialized time objects (should they be const?) and one that
isn’t initialized. Then it should add the two initialized values together, leaving the result in
the third time variable. Finally, it should display the value of this third variable. Make
appropriate member functions const.
Solution: OUTPUT
#include <iostream>
using namespace std;
class Time {
private:
int hours;
int min;
int sec;
public:
Time() : hours(0), min(0), sec(0) {}
int main() {
const Time t1(2, 45, 30);
const Time t2(9, 60, 40);
Time t3;
t3.add(t1, t2);
t3.display();
return 0;
}
12
Task 2: Point on the two-dimensional plane can be represented by two numbers: an x
coordinate and a y coordinate. For example, (4,5) represents a point 4 units to the right of
the vertical axis, and 5 units up from the horizontal axis. The sum of two points can be
defined as a new point whose x coordinate is the sum of the x coordinates of the two
points, and whose y coordinate is the sum of the y coordinates. Write a program that uses
a structure called point to model a point. Define three points and have the user input
values to two of them. Then set the third point equal to the sum of the other two and
display the value of the new point.
Solution:
OUTPUT
#include <iostream>
using namespace std;
struct Point {
double x;
double y;
};
int main() {
Point p1, p2, p3;
cout << "Sum of P1 and P2: (" << p3.x << ", " << p3.y << ")" << endl;
return 0;
}
Task 3: Write a program that encourages the user to enter two fractions, and then displays
their sum in fractional form. The program should store fractions as objects of a class
called "Fraction." This class should have integer members for the numerator and
denominator. Member functions should accept input in the form of fractions (e.g., "3/5")
and output the fraction's value in the same format. Additionally, implement a member
function to add two fraction values. Finally, write a main () program allowing users to input
two fractions, display their sum, and prompt whether to continue.
13
Solution:
OUTPUT
#include <iostream>
using namespace std;
class Fraction {
int num;
int den;
public:
Fraction() : num(0), den(1) {}
Fraction(int n, int d) : num(n), den(d) {}
void inputFraction() {
char slash;
cout << "Enter a fraction (e.g., 3/5): ";
cin >> num >> slash >> den;
}
void display() const {
cout << num << "/" << den;
}
Fraction add(const Fraction& other) const {
int n = num * other.den + other.num * den;
int d = den * other.den;
return Fraction(n, d);
}
};
int main() {
char choice;
do {
Fraction f1, f2;
f1.inputFraction();
f2.inputFraction();
return 0;
}
14
Lab 05
Task 1: Write the definition of a class person. Add at least 5 attributes (private/public) to
the person class.
Add the following behaviour (function) to the class:
• Name: “”
• Age=0
• Gender=m (m for male, f for female)
• Occupation= student
• Cooking= n (n for no, y for yes)
Solution: OUTPUT
#include <iostream>
using namespace std;
class personClass{
public:
string name;
int age;
char gender;
string occupation;
char cook;
void getName(){
cout<<"Enter Name: ";
cin>>name;
}
void getAge(){
cout<<"Enter Age: ";
cin>>age;
}
15
void getGender(){
cout<<"Enter Gender (M for Male & F for Female): ";
cin>>gender;
}
void getOccupation(){
cout<<"Enter Occupation: ";
cin>>occupation;
}
void canCook(){
cout<<"Can you Cook? (Y for Yes & N for No): ";
cin>>cook;
}
personClass(){
getName();
getAge();
getGender();
getOccupation();
canCook();
}
personClass(string n, int ag, char gen, string occup, char cooki){
name = n;
age = ag;
gender = gen;
occupation = occup;
cook = cooki;
}
personClass(personClass &obj){
name = obj.name;
age = obj.age;
gender = obj.gender;
occupation = obj.occupation;
cook = obj.cook;
}
void disp(){
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
cout<<"Occupation: "<<occupation<<endl;
cout<<"Cooking: "<<cook<<endl;
}
};
int main(){
personClass p1;
personClass p2("Wishaq",19,'M',"Student",'N');
personClass p3(p1);
16
cout<<endl;
cout<<"Person 1: "<<endl;
p1.disp();
cout<<endl;
cout<<"Person 2: "<<endl;
p2.disp();
cout<<endl;
cout<<"Person 3: "<<endl;
p3.disp();
}
17
Lab 06
Task 1: Write a class person that has the attributes of id, name and address.it has a
constructor to initialize, a member function to input and a member function to display
the data members. Create 2nd class student that inherit the person class.it has
additional attributes of Roll No and Marks. It also has a member function to input and
display the data members. Create 3rd class scholarship that inherit the student class. It
has additional attribute of scholarship name and attributes of scholarship name and
amount.it also has a member function to input and display the data members.
Solution:
class person {
public:
int perId;
string perName;
string perAddress;
person(){}
void inputData() {
cout << "Enter ID: ";
cin >> perId;
cin.ignore();
cout << "Enter Name: ";
getline(cin,perName);
cout << "Enter Address: ";
getline(cin,perAddress);
}
void displayData() {
cout << "ID: " << perId << endl;
cout << "Name: " << perName << endl;
cout << "Address: " << perAddress << endl;
}
};
void inputData() {
person::inputData();
cout << "Enter Roll No: ";
cin >> rollNo;
18
cout << "Enter Marks: ";
cin >> marks;
}
void displayData() {
person::displayData();
cout << "Roll No: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
};
void inputData() {
student::inputData();
cin.ignore();
cout << "Enter Scholar Name: ";
getline(cin,scholarName);
cout << "Enter Amount: ";
cin >> amount;
}
void displayData() {
student::displayData();
cout << "Scholar Name: " << scholarName << endl;
cout << "Amount: " << amount << endl;
}
};
int main() {
person p1;
cout<<"Enter Person Details: "<<endl;
p1.inputData();
cout<<"\nPerson Details: "<<endl;
p1.displayData();
student s1;
cout<<"\nEnter Student Details: "<<endl;
s1.inputData();
cout<<"\nStudent Details: "<<endl;
s1.displayData();
scholarship sc1;
cout<<"\nEnter Scholar Details: "<<endl;
sc1.inputData();
cout<<"\nScholar Details: "<<endl;
sc1.displayData();
return 0;
19
}
Task 2: Write a class that contain that contain the attribute teacher name, age and
address.it also contain the member function to input and display the attributes .Write
another class Writer that contain the attributes writer name, address and number of
books written by him.it also contain the member function to input and display its
attributes. Write a third class scholar that inherit the both teacher and writer class.
Solution:
class teacher {
public:
string name;
int age;
string address;
void input(){
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter Age: ";
cin>>age;
}
void display(){
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
}
};
class writer{
public:
string name;
string address;
int numBooks;
void input(){
cout<<"Enter Name: ";
cin>>name;
cout<<"Enter Address: ";
cin>>address;
cout<<"Enter Number of Books: ";
cin>>numBooks;
}
void display(){
cout<<"Name: "<<name<<endl;
cout<<"Address: "<<address<<endl;
20
cout<<"Number of Books written: "<<numBooks<<endl;
}
};
int main() {
teacher t1;
cout<<"Enter Teacher Details: "<<endl;
t1.input();
cout<<"\nTeacher Details: "<<endl;
t1.display();
writer w1;
cout<<"\nEnter Writer Details: "<<endl;
w1.input();
cout<<"\nWriter Details: "<<endl;
w1.display();
scholar s1;
cout<<"\nScholar Details: "<<endl;
s1.display();
}
21
Lab 07
Task 1: Create two classes named Mammals and MarineAnimals. Create another class
named BlueWhale which inherits both the above classes. Now, create a function in each
of these classes which prints "I am mammal", "I am a marine animal" and "I belong to
both the categories: Mammals as well as Marine Animals" respectively. Now, create an
object for each of the above class and try calling
1. function of Mammals by the object of Mammal.
2. function of MarineAnimal by the object of MarineAnimal.
3. function of BlueWhale by the object of BlueWhale.
4. function of each of its parent by the object of BlueWhale.
Solution:
class Mammals{
public:
void display(){
}
};
class MarineAnimals{
public:
void display(){
cout<<"I am a marine animal."<<endl;
}
};
int main(){
Mammals ma1;
MarineAnimals an2;
BlueWhale bw3;
ma1.display();
an2.display();
bw3.display();
bw3.Mammals::display();
bw3.MarineAnimals::display();
22
}
Task 2: Create a class named Shape with a function that prints "This is a shape". Create
another class named Polygon inheriting the Shape class with the same function that
prints "Polygon is a shape". Create two other classes named Rectangle and Triangle
having the same function which prints "Rectangle is a polygon" and "Triangle is a
polygon" respectively. Again, make another class named Square having the same
function which prints "Square is a rectangle". Now, try calling the function by the object
of each of these classes.
Solution:
class Shape{
public:
void display(){
cout<<"This is a shape."<<endl;
}
};
23
}
};
int main(){
Shape s1;
Polygon p1;
Rectangle r1;
Triangle t1;
Square sq1;
s1.display();
p1.display();
r1.display();
t1.display();
sq1.display();
}
Task 3: Write a program to print the names of students by creating a Student class. If no
name is passed while creating an object of the Student class, then the name should be
"Unknown", otherwise the name should be equal to the String value passed while
creating the object of the Student class.
Solution:
class Student{
string name;
public:
Student(){
name = "Unknown";
cout<<"Name: "<<name<<endl;
}
Student(string n):name(n){
cout<<"Name: "<<name<<endl;
}
};
int main(){
Student s1;
Student s2("Wishaq");
}
24
Lab 08
Task 1: Write a class that has three data members hours, minutes and seconds. The
class has fallowing member’s functions.
class Time{
private:
int hours, minutes, seconds;
public:
Time(){
hours = 03;
minutes = 22;
seconds = 56;
}
void show(){
cout << hours << ":" << minutes << ":" << seconds << endl;
}
void operator ++(){
minutes++;
}
void operator --(){
minutes--;
}
};
int main(){
Time t1;
t1.show();
++t1;
t1.show();
--t1;
t1.show();
return 0;
}
25
Task 2: Create a class called Time that has separate int member data for hours, minutes,
and seconds. One constructor should initialize this data to 0, and another should
initialize it to fixed values. Another member function should display it, in 11:59:59
format. The final member function should add two objects of type time passed as
arguments.
Solution:
class Time {
private:
int hours, minutes, seconds;
public:
Time(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}
void display()
{
cout << hours << ":" << minutes << ":" << seconds << endl;
}
int main() {
Time t1(2, 30, 45);
Time t2(3, 45, 23);
Time t3 = t1.add(t1, t2);
cout << "Sum of times: ";
t3.display();
return 0;
}
Task 3: Write a class for Distance which have two parts inches and feet as floating data
members. Write an overloaded function for (+) binary operator to add the inches and feet
of two distance objects. Main function is also necessary to show the working of the
Class.
26
Solution:
class Distance
{
private:
float inches;
float feet;
public:
Distance(float f = 0, float i = 0)
{
feet = f;
inches = i;
}
void display()
{
cout << feet << " feet " << inches << " inches" << endl;
}
Distance operator+(Distance d)
{
Distance temp;
temp.feet = feet + d.feet;
temp.inches = inches + d.inches;
while (temp.inches >= 12)
{
temp.feet ++;
temp.inches -= 12;
}
return temp;
}
};
int main(){
Distance d1(12, 10), d2(3, 4), d3;
d1.display();
d2.display();
d3 = d1 + d2;
cout << "Sum of distances: ";
d3.display();
}
27