0% found this document useful (0 votes)
26 views18 pages

Object Oriented Programming Lab

Uploaded by

aaahsan.ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
26 views18 pages

Object Oriented Programming Lab

Uploaded by

aaahsan.ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 18

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 06
(Spring 2024)

Course: Object Oriented Programming Lab Date: 20/03/2024


Course Code: CSC-210 Max Marks: 30
Faculty’s Name: Rizwan Khalid

Name: MUHAMMAD AHSAN Enroll No: 03-134232-053

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>

using namespace std;


class IceCreamCone {

private:

string flavour;

int numScoops;

string coneType;

public:

IceCreamCone(string _flavour = "Vanilla", int _numScoops = 1, string _coneType =


"Sugar") {

flavour = _flavour;

numScoops = _numScoops;

coneType = _coneType;

double calculatePrice() {

double pricePerScoop = 75.0;

double additionalCost = 0.0;

if (coneType == "Waffle") {

additionalCost += 40.0;

return (pricePerScoop * numScoops) + additionalCost;

}
void display() {

cout << "Flavour: " << flavour << endl;

cout << "Number of Scoops: " << numScoops << endl;

cout << "Cone Type: " << coneType << endl;

cout << "Price: " << calculatePrice() << " rupees" << endl;

};

int main() {

IceCreamCone obj1;

IceCreamCone obj2("Mango", 3, "Chocolate");

IceCreamCone obj3("Mango", 3, "Waffle");

cout << "Object 1:" << endl;

obj1.display();

cout << endl;

cout << "Object 2:" << endl;

obj2.display();

cout << endl;

cout << "Object 3:" << endl;

obj3.display();

cout << endl;

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>

using namespace std;

class Employee {

private:

int employeeNumber;

string name;

int age;

double pay;
public:

Employee(int _employeeNumber, string _name, int _age, double _pay) {

employeeNumber = _employeeNumber;

name = _name;

age = _age;

pay = _pay;

void display() {

cout << "Employee Number: " << employeeNumber << endl;

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

cout << "Pay: " << pay << endl;

int getEmployeeNumber() const {

return employeeNumber;

};

int main() {

Employee employees[3] = {

Employee(101, "Ali", 30, 50000),

Employee(102, "Ahmed", 25, 45000),

Employee(103, "Ahsan", 35, 60000)

};
char choice;

do {

int empNumber;

cout << "Enter the employee number to display information: ";

cin >> empNumber;

bool found = false;

for (int i = 0; i < 3; ++i) {

if (employees[i].getEmployeeNumber() == empNumber) {

employees[i].display();

found = true;

break;

if (!found) {

cout << "Employee with number " << empNumber << " not found." << endl;

cout << "Do you want to continue? (Y/N): ";

cin >> choice;

} while (choice == 'Y' || choice == 'y');

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>

using namespace std;

class Date {

private:

int day;

int month;

int year;

public:
void inputDate() {

cout << "Enter day: ";

cin >> day;

cout << "Enter month: ";

cin >> month;

cout << "Enter year: ";

cin >> year;

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

bcode 4 digits code number

bname string

innings, notout, runs integer type

batavg - it is calculated according to the formula -- batavg =runs/(innings-notout)

Public Members

readdata( ) Function to accept value from bcode, name, innings, notout and

invoke the function calcavg( )

calcavg( ) Function to compute batavg

displaydata( ) Function to display the data members on the screen.

Code:
#include <iostream>

#include <string>

using namespace std;

class batsman {
private:

int bcode;

string bname;

int innings;

int notout;

double batavg;

int runs;

public:

void readdata() {

cout << "Enter batsman code: ";

cin >> bcode;

cout << "Enter batsman name: ";

cin.ignore();

getline(cin, bname);

cout << "Enter number of innings played: ";

cin >> innings;

cout << "Enter number of times not out: ";

cin >> notout;

cout << "Enter total runs scored: ";

cin >> runs;

calcavg();

void calcavg() {

if (innings - notout != 0) {

batavg = static_cast<double>(runs) / (innings - notout);


} else {

batavg = 0;

void displaydata() {

cout << "Batsman code: " << bcode << endl;

cout << "Batsman name: " << bname << endl;

cout << "Innings played: " << innings << endl;

cout << "Not out: " << notout << endl;

cout << "Total runs scored: " << runs << endl;

cout << "Batting average: " << batavg << 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>

using namespace std;

class Person {

private:

string name;

int age;

string city;

public:
void inputDetails() {

cout << "Enter name: ";

getline(cin, name);

cout << "Enter age: ";

cin >> age;

cout << "Enter city: ";

cin.ignore();

getline(cin, city);

int getAge() {

return age;

void display() {

cout << "Name: " << name << endl;

cout << "Age: " << age << endl;

cout << "City: " << city << endl;

};

int main() {

Person person1, person2;

cout << "Enter details for person 1:" << endl;

person1.inputDetails();

cout << "Enter details for person 2:" << endl;

person2.inputDetails();
if (person1.getAge() > person2.getAge()) {

cout << "Details of the elder person:" << endl;

person1.display();

} else if (person2.getAge() > person1.getAge()) {

cout << "Details of the elder person:" << endl;

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

Look like this:


Enter your area code, exchange, and number: 415 555 1212

My number is (212) 767-8900

Your number is (415) 555-1212

Code:
#include <iostream>

#include <string>

using namespace std;

class Phone {

private:

string areaCode;

string exchange;

string number;

public:

Phone(const string& _areaCode, const string& _exchange, const string& _number) {

areaCode = _areaCode;

exchange = _exchange;

number = _number;

Phone() {

areaCode = "";

exchange = "";

number = "";

void inputNumber() {
cout << "Enter area code: ";

cin >> areaCode;

cout << "Enter exchange: ";

cin >> exchange;

cout << "Enter number: ";

cin >> number;

void displayNumber() {

cout << "(" << areaCode << ") " << exchange << "-" << number << endl;

};

int main() {

Phone myNumber("212", "767", "8900");

Phone yourNumber;

cout << "Enter your phone number:" << endl;

yourNumber.inputNumber();

cout << "My number is: ";

myNumber.displayNumber();

cout << "Your number is: ";

yourNumber.displayNumber();

return 0;

}
OUTPUT

Task 6
Write the functionality of the given below code in one paragraph in your writing.

#include<iostream>

using namespace std;

class fClass

private:

int a;

public:

void setter(int x)

a=x;

int getter()

return a;

}
};

int main()

fClass num;

int n,n2;

cout<<"Enter the value: ";

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

You might also like