Object Oriented Programming Lab
Object Oriented Programming Lab
Objective(s):
Topic Pointers
o Classes
Objective
o Constructors
o Data Members
o Member Functions
Task 1
Create a Class named IceCreamCone, Unless arguments are supplied, flavour defaults to
“Vanilla”, number of scoops defaults to 1, and cone type defaults to “Sugar”. The member
functions also calculates the price based on 75 rupees per scoop, with an additional 40 rupees for a
waffle cone. Write a main () function demonstrating that the Class Objects and Class Functions
works correctly. Save the file as IceCreamCone.cpp and also paste the code under every task
Sample input:
IceCreamCone obj;
IceCreamCone obj("Mango", 3, "Chocolate");
IceCreamCone obj("Mango",3,"waffle");
Code:
#include <iostream>
#include <string>
private:
string flavour;
int numScoops;
string coneType;
public:
flavour = _flavour;
numScoops = _numScoops;
coneType = _coneType;
double calculatePrice() {
if (coneType == "Waffle") {
additionalCost += 40.0;
}
void display() {
cout << "Price: " << calculatePrice() << " rupees" << endl;
};
int main() {
IceCreamCone obj1;
obj1.display();
obj2.display();
obj3.display();
return 0;
}
OUTPUT
Task 2
Use a Class to Store the Employee Number, Name, Age and Pay of 3 Employees.
User enter employee number, whose information want to be displayed.
Program will continue when user press 'Y' or 'y'.
Code:
#include <iostream>
#include <string>
class Employee {
private:
int employeeNumber;
string name;
int age;
double pay;
public:
employeeNumber = _employeeNumber;
name = _name;
age = _age;
pay = _pay;
void display() {
return employeeNumber;
};
int main() {
Employee employees[3] = {
};
char choice;
do {
int empNumber;
if (employees[i].getEmployeeNumber() == empNumber) {
employees[i].display();
found = true;
break;
if (!found) {
cout << "Employee with number " << empNumber << " not found." << endl;
return 0;
}
OUTPUT
Task 3
Write a program to input date (day,month,year) by a member function and print the date
(day,month,year) by writing the another member function.
Code:
#include <iostream>
class Date {
private:
int day;
int month;
int year;
public:
void inputDate() {
void printDate() {
cout << "Date: " << day << "/" << month << "/" << year << endl;
};
int main() {
Date d;
d.inputDate();
d.printDate();
return 0;
OUTPUT
Task 4:
Define a class batsman with the following specifications:
Data Members
bname string
Public Members
readdata( ) Function to accept value from bcode, name, innings, notout and
Code:
#include <iostream>
#include <string>
class batsman {
private:
int bcode;
string bname;
int innings;
int notout;
double batavg;
int runs;
public:
void readdata() {
cin.ignore();
getline(cin, bname);
calcavg();
void calcavg() {
if (innings - notout != 0) {
batavg = 0;
void displaydata() {
cout << "Total runs scored: " << runs << endl;
};
int main() {
batsman player;
player.readdata();
player.displaydata();
return 0;
OUTPUT
Task 5
Write a program by using a class that takes name, age and city of a person as class attributes. An input
Details member functions to input the data, getAge function to return age and Display function to display
name, age and city. Input the data for two persons and display the record of the person who is elder in age.
Code:
#include <iostream>
#include <string>
class Person {
private:
string name;
int age;
string city;
public:
void inputDetails() {
getline(cin, name);
cin.ignore();
getline(cin, city);
int getAge() {
return age;
void display() {
};
int main() {
person1.inputDetails();
person2.inputDetails();
if (person1.getAge() > person2.getAge()) {
person1.display();
person2.display();
} else {
cout << "Both persons are of the same age." << endl;
return 0;
OUTPUT
Task 7:
A phone number, such as (212) 767-8900, can be thought of as having three parts: the
Area code (212), the exchange (767), and the number (8900). Write a program that uses a
CLASS to store these three parts of a phone number separately. Call the class
Phone. Create two class variables of type phone. Initialize one, and have the user
Input a number for the other one. Then display both numbers. The interchange might
Code:
#include <iostream>
#include <string>
class Phone {
private:
string areaCode;
string exchange;
string number;
public:
areaCode = _areaCode;
exchange = _exchange;
number = _number;
Phone() {
areaCode = "";
exchange = "";
number = "";
void inputNumber() {
cout << "Enter area code: ";
void displayNumber() {
cout << "(" << areaCode << ") " << exchange << "-" << number << endl;
};
int main() {
Phone yourNumber;
yourNumber.inputNumber();
myNumber.displayNumber();
yourNumber.displayNumber();
return 0;
}
OUTPUT
Task 6
Write the functionality of the given below code in one paragraph in your writing.
#include<iostream>
class fClass
private:
int a;
public:
void setter(int x)
a=x;
int getter()
return a;
}
};
int main()
fClass num;
int n,n2;
cin>>n;
num.setter(n);
n2=num.getter();
cout<<n2;
Answer:
The above given code defines a class fClass with a private member a, and two member functions:
setter() to set the value of a, and getter() to return the value of a. In the main() function, an object
num of type fClass is created. The user is prompted to enter a value, which is then set using the
setter() function. The value stored in num is then returned using the getter() function and stored in
n2, which is finally printed to the console. Overall, this code demonstrates a basic implementation
of encapsulation, where data (member a) is encapsulated within a class (fClass) and accessed
through public member functions (setter() and getter()).