Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views7 pages

OOP Theory Assignment

This document is an assignment for a course in Object Oriented Programming at the University of Management & Technology. It includes two programming tasks: the first involves creating a class for a swimming pool with methods to manage its water volume, and the second requires creating a sound player class with functionalities for managing sound data. The assignment outlines specific requirements for class design, constructors, member functions, and a main function to demonstrate the classes' capabilities.

Uploaded by

f2024408034
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

OOP Theory Assignment

This document is an assignment for a course in Object Oriented Programming at the University of Management & Technology. It includes two programming tasks: the first involves creating a class for a swimming pool with methods to manage its water volume, and the second requires creating a sound player class with functionalities for managing sound data. The assignment outlines specific requirements for class design, constructors, member functions, and a main function to demonstrate the classes' capabilities.

Uploaded by

f2024408034
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

University of Management & Technology

Department of Informatics and Systems (SST/INFS)


OOP Theory Assignment# 2 (Section – Y7)
Instructor: Mr. Raybal Akhtar
Course Title: Object Oriented Programming
Course Code: CC1022
Total Marks: 10
Student Name: University ID:

Question# 1 [CLO 1 - CLO2]


Understand the concepts of object oriented programming and Identify and
write the solution of the following programming exercises

Task: 1

Answer:

#include <iostream>
using namespace std;

class swimmingPool {
private:
double length;
double width;
double depth;
double fillRate;
double drainRate;
double currentVolume;

double totalVolume() const {


return length * width * depth * 7.48; // 1 cubic foot = 7.48 gallons
}

public:
swimmingPool(double l, double w, double d, double fRate, double dRate,
double cVolume = 0) {
length = l;
width = w;
depth = d;
fillRate = fRate;
drainRate = dRate;
currentVolume = cVolume;
}

double waterToFill() const {


return totalVolume() - currentVolume;
}

double timeToFill() const {


return (fillRate > 0) ? waterToFill() / fillRate : -1;
}
double timeToDrain() const {
return (drainRate > 0) ? currentVolume / drainRate : -1;
}

void fillPool(double minutes) {


currentVolume += fillRate * minutes;
if (currentVolume > totalVolume()) {
currentVolume = totalVolume();
}
}

void drainPool(double minutes) {


currentVolume -= drainRate * minutes;
if (currentVolume < 0) {
currentVolume = 0;
}
}

void displayStatus() const {


cout << "Current Volume: " << currentVolume << " gallons" << endl;
cout << "Total Capacity: " << totalVolume() << " gallons" << endl;
}
};

Task: 2

1. Write a C++ program and follow the instructions:


1) Create a Class with name soundPlayer
2) Private data members, string soundName, int soundDuration in seconds,
suoundRating (value range between 1 to 5), and an array string
*singersName[].
3) Make default and parameterized constructors
4) Make setters and getters for all data members
5) Make copy constructor (shallow or deep) for the above data members
i. You guess what kind of copy constructor you need to design and
implement according to requirements
6) Make a member function displayData that access the getters and print all
the values of data members.
7) Make a member function updateData that accepts two arguments
soundRating and singerName array and update these data member’s
values in this member function.
2. Write main body
1) Create a single object SP1 and set data members using setters or
constructors
2) Create another object SP2 and initialize the data members of SP2 with
values of data members SP1.
3) Call displayData member function with both objects separately.
4) call updateData member function and pass argument soundRating and
singersName array and update the value
5) Again Call displayData member function with both objects separately.
3. Use above program code and now write the main body again as per
instructions:
1) Create an array of objects dynamically
2) Initialize the data members using constructors or setters
3) Display the data of all the objects by calling displayData member
function.

Answer 2: #include <iostream>


#include <string>
using namespace std;
class soundPlayer {
private:
string soundName;
int soundDuration;
int soundRating;
string* singersName;
int singerCount;
public:
soundPlayer() {
soundName = "";
soundDuration = 0;
soundRating = 0;
singersName = nullptr;
singerCount = 0;
}
soundPlayer(string sName, int sDuration, int sRating, string sNames[], int sCount) {
soundName = sName;
soundDuration = sDuration;
soundRating = sRating;
singerCount = sCount;
singersName = new string[singerCount];
for (int i = 0; i < singerCount; i++) {
singersName[i] = sNames[i];
}
}
soundPlayer(const soundPlayer& other) {
soundName = other.soundName;
soundDuration = other.soundDuration;
soundRating = other.soundRating;
singerCount = other.singerCount;
singersName = new string[singerCount];
for (int i = 0; i < singerCount; i++) {
singersName[i] = other.singersName[i];
}
}
void setSoundName(string name) { soundName = name; }
void setSoundDuration(int duration) { soundDuration = duration; }
void setSoundRating(int rating) { soundRating = rating; }
void setSingersName(string names[], int count) {
if (singersName != nullptr) delete[] singersName;
singerCount = count;
singersName = new string[singerCount];
for (int i = 0; i < singerCount; i++) {
singersName[i] = names[i];
}
}
string getSoundName() const { return soundName; }
int getSoundDuration() const { return soundDuration; }
int getSoundRating() const { return soundRating; }
string* getSingersName() const { return singersName; }
int getSingerCount() const { return singerCount; }
void displayData() const {
cout << "Sound Name: " << soundName << endl;
cout << "Duration: " << soundDuration << " seconds" << endl;
cout << "Rating: " << soundRating << " / 5" << endl;
cout << "Singers: ";
for (int i = 0; i < singerCount; i++) {
cout << singersName[i];
if (i < singerCount - 1) cout << ", ";
}
cout << endl << "------------------------" << endl;
}
void updateData(int rating, string sNames[], int count) {
soundRating = rating;
setSingersName(sNames, count);
}
~soundPlayer() {
delete[] singersName;
}
};

Main Function: int main() {


string singers1[] = {"Ali", "Sara"};
soundPlayer SP1("BeatDrop", 180, 4, singers1, 2);
soundPlayer SP2 = SP1;
cout << "Data for SP1:" << endl;
SP1.displayData();
cout << "Data for SP2:" << endl;
SP2.displayData();
string updatedSingers[] = {"Ahmed", "Zoya", "Hamza"};
SP1.updateData(5, updatedSingers, 3);
cout << "After updating SP1:" << endl;
SP1.displayData();
cout << "Data for SP2 (should remain unchanged):" << endl;
SP2.displayData();
return 0;
}

 Good Luck 

You might also like