0% found this document useful (0 votes)
60 views39 pages

Oop Assignment#01

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

Oop Assignment#01

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

ROLL NUMBER: DT-045

OOP ASSIGNMENT NUMBER #01

QUESTION NUMBER 1:
/*Create a SavingsAccount class. Use a static data member annualInterestRate to
store the

annual interest rate for each of the savers. Each member of the class contains a
private data

member savingsBalance indicating the amount the saver currently has on


deposit. Provide

member function calculateMonthlyInterest that calculates the monthly interest by

multiplying the balance by annualInterestRate divided by 12; this interest should


be added to

savingsBalance. Provide a static member function modifyInterestRate that sets


the static

annualInterestRate to a new value. Write a driver program to test class


SavingsAccount.

Instantiate two different objects of class SavingsAccount, saver1 and saver2, with
balances of

Rs.2000.00 and Rs.3000.00, respectively. Set the annualInterestRate to 3 percent.


Then

calculate the monthly interest and print the new balances for each of the savers.
Then set the

annualInterestRate to 4 percent, calculate the next month’s interest and print the
new

balances for each of the savers.*/

#include <iostream>

using namespace std;

class SavingsAccount

{
ROLL NUMBER: DT-045

private:

double savingsBalance;

static double annualInterestRate;

public:

SavingsAccount(double balance) : savingsBalance(balance) {}

void calculateMonthlyInterest()

double monthlyInterestRate = savingsBalance * annualInterestRate / 12;

savingsBalance += monthlyInterestRate;

static void modifyInterestRate(double newRate)

annualInterestRate = newRate;

double displayBalance()

return savingsBalance;

};

double SavingsAccount::annualInterestRate = 0.0;

/*static member ko outside of the class iniatialize krtay

hain kyunkay iski memory seperately allocate ki jaati hai.*/

int main()

{
ROLL NUMBER: DT-045

cout << "-----------------------------------------------------" << endl;

cout << "This is the Assignment of Ebaad Khan Roll No: DT-045" << endl;

cout << "-----------------------------------------------------" << endl;

SavingsAccount person1(2000.0);

SavingsAccount person2(3000.0);

SavingsAccount::modifyInterestRate(0.03);

person1.calculateMonthlyInterest();

person2.calculateMonthlyInterest();

cout << "Balance of person 1 after a month with 3 perecent interest is is: " <<
person1.displayBalance() << endl;

cout << "Balance of person 2 after a month with 3 perecent interest is: " <<
person2.displayBalance() << endl;

SavingsAccount::modifyInterestRate(0.04);

person1.calculateMonthlyInterest();

person2.calculateMonthlyInterest();

cout << "Balance of person 1 after a month with 4 perecent interest: " <<
person1.displayBalance() << endl;

cout << "Balance of person 2 after a month with 4 percent interest: " <<
person2.displayBalance() << endl;

return 0;

OUTPUT:
ROLL NUMBER: DT-045

QUESTION NUMBER 2:

/*Design a Ship class that has the following members:

• A member variable for the name of the ship (a string)

• A member variable for the year that the ship was built (a string)

• A constructor and appropriate accessors/getters and mutators/setters

• A print function that displays the ship's name and the year it was built.

Design a CruiseShip class that is derived from the Ship class. The CruiseShip
class should have

the following members:


ROLL NUMBER: DT-045

• A member variable for the maximum number of passengers (an int)

• A constructor and appropriate accessors and mutators

• A print function that overrides the print function in the base class. The
Cruiseship

class's print function should display only the ship's name and the maximum
number

of passengers.

Design a CargoShip class that is derived from the Ship class. The CargoShip
class should have

the following members:

• A member variable for the cargo capacity in tonnage (an int)

• A constructor and appropriate accessors and mutators

• A print function that overrides the print function in the base class. The
CargoShip

class's print function should display only the ship's name and the ship's cargo
capacity.

Write a test program that declares an array of Ship pointers. The array elements
should be

initialized with the addresses of Ship, Cruiseship, and CargoShip objects. The
program should

then step through the array, calling each object's print function.*/

#include <iostream>

#include <string>

using namespace std;

class Ship

public:
ROLL NUMBER: DT-045

string nameofship;

string year;

string newyear;

char YN;

Ship(const string &nameofship, const string &year)

: nameofship(nameofship), year(year) {}

void set_name()

cout << "Enter the name of the ship" << endl;

cin >> this->nameofship;

void display_name()

cout << "The name of the ship is: " << this->nameofship << endl;

void set_year()

cout << "Enter the year the ship was built" << endl;

cin >> this->year;

void display_year()
ROLL NUMBER: DT-045

cout << "The ship was built in the year: " << this->year << endl;

/*void modify_year()

cout << "Do you want to change the year? ";

cin >> this->YN;

if (this->YN == 'Y')

cout << "Enter New Year: ";

cin >> this->newyear;

displaynewyear();

else

cout << "No new year Entered" << endl;

}*/

void displaynewyear()

cout << "The ship name is: " << this->nameofship << " and the year is: " << this-
>newyear << endl;

virtual void display_info()


ROLL NUMBER: DT-045

cout << "The name of the ship is: " << this->nameofship << " and it was built in the
year: " << this->year << endl;

};

class CruiserShip : public Ship

private:

int max_passengers;

public:

CruiserShip(const string &shipname, const string &year, int max_passengers)

: Ship(shipname, year), max_passengers(max_passengers) {}

int get_passengers() const

return max_passengers;

void setmax_passengers(int maxpass)

max_passengers = maxpass;

void display_info() override

{
ROLL NUMBER: DT-045

cout << "The name of the ship is: " << this->nameofship << " and the max number of
passengers is: " << this->max_passengers << endl;

};

class CargoShip : public Ship

public:

int capacity;

CargoShip(const string &shipname, const string &year, int cargoCap)

: Ship(shipname, year), capacity(cargoCap) {}

void set_capacity()

cout << "Enter the Capacity of Cargo in tons: ";

cin >> capacity;

void display_info() override

cout << "The name of the ship is: " << this->nameofship << " and the Capacity in
tons is: " << this->capacity << endl;

};

int main()
ROLL NUMBER: DT-045

cout << "-----------------------------------------------------" << endl;

cout << "This is the Assignment of Ebaad Khan Roll No: DT-045" << endl;

cout << "-----------------------------------------------------" << endl;

Ship *ships[3];

ships[0] = new Ship("Titanic", "1912");

ships[1] = new CruiserShip("Santa Maria", "1987", 1000);

ships[2] = new CargoShip("Sinking Ship", "2023", 1500);

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

ships[i]->display_info();

cout << endl;

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

delete ships[i];

return 0;

OUTPUT:
ROLL NUMBER: DT-045

QUESTION NUMBER 3:
/*Review it to write program for each of the following tasks:

a. Create classes with defined member variables and functions. Each class must
have

default and parameterized constructors that assigns initial values to all members
and

destructors.

b. Write a main function where:

i. Create one country “Pak” which should have five provinces.

ii. One province has four cities.

iii. Consider your ID as the total budget in millions for country “Pak”, for example

ID is “CT-22034”, so 22034 million budget.

iv. Divide that budget and assign equally to each province


ROLL NUMBER: DT-045

v. From assigned budget, equally divide budget to the cities.

vi. Print all details in a tabular form that shows all provinces and their members

values. All cities and their member values.

c. Write a function totalExpenses() which asks people count for each city. If each
person

has a service cost of last two digits of your id (CT-22034 → 34) then calculate
total

expenses for each city.

d. Write a function totalExpenses2() which uses expenses values of cities and


calculates

total expenses of provinces and the country.

e. Write a function highestExpensesP() which prints details of the province with


highest

expenses.

f. Write a function finalFunction() which prints profit or loss using budget and
expenses

members for each province in a tabular form.*/

/*Review it to write program for each of the following tasks:

a. Create classes with defined member variables and functions. Each class must
have

default and parameterized constructors that assigns initial values to all members
and

destructors.

b. Write a main function where:

i. Create one country “Pak” which should have five provinces.

ii. One province has four cities.

iii. Consider your ID as the total budget in millions for country “Pak”, for example
ROLL NUMBER: DT-045

ID is “CT-22034”, so 22034 million budget.

iv. Divide that budget and assign equally to each province

v. From assigned budget, equally divide budget to the cities.

vi. Print all details in a tabular form that shows all provinces and their members

values. All cities and their member values.

c. Write a function totalExpenses() which asks people count for each city. If each
person

has a service cost of last two digits of your id (CT-22034 → 34) then calculate
total

expenses for each city.

d. Write a function totalExpenses2() which uses expenses values of cities and


calculates

total expenses of provinces and the country.

e. Write a function highestExpensesP() which prints details of the province with


highest

expenses.

f. Write a function finalFunction() which prints profit or loss using budget and
expenses

members for each province in a tabular form.*/

#include <iostream>

using namespace std;

class cities

private:

int peopleCount;

float budget;
ROLL NUMBER: DT-045

float expenses;

public:

cities() : peopleCount(0), budget(0), expenses(0) {}

cities(int pc, float b, float e)

peopleCount = pc;

budget = b;

expenses = e;

void setPeopleCount(int pc)

peopleCount = pc;

int getPeopleCount() const

return peopleCount;

void setBudget(float b)

budget = b;

float getBudget() const

return budget;
ROLL NUMBER: DT-045

void setExpenses(float e)

expenses = e;

float getExpenses() const

return expenses;

};

class provinces

private:

int citiesCount;

int peopleCount;

float budget;

float expenses;

cities *citiesInProvinces;

public:

provinces() : citiesCount(0), peopleCount(0), budget(0), expenses(0),


citiesInProvinces(nullptr) {}

provinces(int c, int pc, float b, float e)

{
ROLL NUMBER: DT-045

citiesCount = c;

peopleCount = pc;

budget = b;

expenses = e;

citiesInProvinces = new cities[citiesCount];

for (int i = 0; i < citiesCount; i++)

citiesInProvinces[i] = cities();

~provinces()

delete[] citiesInProvinces;

void setPeopleCount(int pc)

peopleCount = pc;

int getPeopleCount() const

return peopleCount;

void setBudget(float b)

{
ROLL NUMBER: DT-045

budget = b;

float getBudget() const

return budget;

void setExpenses(float e)

expenses = e;

float getExpenses() const

return expenses;

void setCitiesCount(int c)

citiesCount = c;

delete[] citiesInProvinces;

citiesInProvinces = new cities[citiesCount];

for (int i = 0; i < citiesCount; i++)

citiesInProvinces[i] = cities();

}
ROLL NUMBER: DT-045

int getCitiesCount() const

return citiesCount;

cities *getCities(int index)

return &citiesInProvinces[index];

};

class country

private:

int provincesCount;

int peopleCount;

float budget;

float expenses;

provinces *provincesInCountry;

public:

country() : provincesCount(0), peopleCount(0), budget(0), expenses(0),


provincesInCountry(nullptr) {}

country(int p, int pc, float b, float e)

provincesCount = p;
ROLL NUMBER: DT-045

peopleCount = pc;

budget = b;

expenses = e;

provincesInCountry = new provinces[provincesCount];

~country()

delete[] provincesInCountry;

void setPeopleCount(int pc)

peopleCount = pc;

int getPeopleCount() const

return peopleCount;

void setBudget(float b)

budget = b;

float getBudget() const

return budget;

}
ROLL NUMBER: DT-045

void setExpenses(float e)

expenses = e;

float getExpenses() const

return expenses;

void setProvincesCount(int p)

provincesCount = p;

delete[] provincesInCountry;

provincesInCountry = new provinces[provincesCount];

int getProvincesCount() const

return provincesCount;

provinces *getProvince(int index)

return &provincesInCountry[index];

};

int totalExpenses(int peopleCount)

{
ROLL NUMBER: DT-045

int serviceCost = 99;

return peopleCount * serviceCost;

int totalExpenses2(country &obj)

int totalExpenses = 0;

for (int i = 0; i < obj.getProvincesCount(); i++)

int provinceExpenses = 0;

for (int j = 0; j < obj.getProvince(i)->getCitiesCount(); j++)

provinceExpenses += obj.getProvince(i)->getCities(j)->getExpenses();

obj.getProvince(i)->setExpenses(provinceExpenses);

totalExpenses += obj.getProvince(i)->getExpenses();

return totalExpenses;

void highestExpensesP(country &obj)

provinces *highestExpensesProvince = nullptr;

int highestExpenses;

for (int i = 0; i < obj.getProvincesCount(); i++)


ROLL NUMBER: DT-045

provinces *province = obj.getProvince(i);

if (obj.getProvince(i)->getExpenses() > highestExpenses)

highestExpenses = obj.getProvince(i)->getExpenses();

highestExpensesProvince = province;

if (highestExpensesProvince != nullptr)

cout << "\t\t======== Province with the Highest Expenses ========" <<
endl;

cout << "\t\t\t\tPeople Count: " << highestExpensesProvince-


>getPeopleCount() << " millions" << endl;

cout << "\t\t\t\tExpenses: " << highestExpensesProvince->getExpenses() << "


millions" << endl;

cout << "\t\t\t\tBudget: " << highestExpensesProvince->getBudget() << "


millions" << endl;

cout << "\t\t\t\tCities Count: " << highestExpensesProvince->getCitiesCount()


<< endl;

void finalFunction(country &obj)

cout << "\n\t\tProfit/Loss for Each Province:" << endl;


ROLL NUMBER: DT-045

cout <<
"\t\t==========================================================" <<
endl;

cout << "\t\tProvince\t| Budget\t| Expenses\t| Profit/Loss" << endl;

cout <<
"\t\t==========================================================" <<
endl;

for (int i = 0; i < obj.getProvincesCount(); i++)

float profitLoss = obj.getProvince(i)->getBudget() - obj.getProvince(i)-


>getExpenses();

string result;

if (profitLoss >= 0)

result = "Profit";

else

result = "Loss";

cout << "\t\tprovince " << i + 1 << "\t ";

cout << obj.getProvince(i)->getBudget() << " M\t ";

cout << obj.getProvince(i)->getExpenses() << " M\t\t ";

cout << profitLoss << " M (" << result << ")" << endl;

}
ROLL NUMBER: DT-045

int main()

cout << endl

<< "======= NAME : Ebaad Khan =========" << endl;

cout << "======= ROLL NO : DT-2245 =======" << endl

<< endl;

country pak;

pak.setProvincesCount(3);

pak.setPeopleCount(239);

pak.setBudget(22099);

float budgetForProvinces = pak.getBudget() / (pak.getProvincesCount());

float budgetForCities = budgetForProvinces / 4;

cout << "\n\nCountry: Pakistan" << endl;

cout << "Provinces: " << pak.getProvincesCount() << endl;

cout << "People Count: " << pak.getPeopleCount() << " millions" << endl;

cout << "Budget: " << pak.getBudget() << " millions" << endl

<< endl;

for (int i = 0; i < pak.getProvincesCount(); i++)

int peopleCountProvinces;

float expenses;

cout << "Enter people count for province " << i + 1 << " in millions "

<< ": ";

cin >> peopleCountProvinces;


ROLL NUMBER: DT-045

pak.getProvince(i)->setPeopleCount(peopleCountProvinces);

pak.getProvince(i)->setBudget(budgetForProvinces);

pak.getProvince(i)->setCitiesCount(2);

for (int j = 0; j < pak.getProvince(i)->getCitiesCount(); j++)

int peopleCountCities;

float expensesCities;

cout << "Enter people count for city " << j + 1 << " in millions "

<< ": ";

cin >> peopleCountCities;

pak.getProvince(i)->getCities(j)->setPeopleCount(peopleCountCities);

pak.getProvince(i)->getCities(j)-
>setExpenses(totalExpenses(pak.getProvince(i)->getCities(j)-
>getPeopleCount()));

pak.getProvince(i)->getCities(j)->setBudget(budgetForCities);

cout << endl;

int totalExpenseCountry = totalExpenses2(pak);

for (int i = 0; i < pak.getProvincesCount(); i++)

cout << "\t\t======== Province" << i + 1 << " ========" << endl;

cout << "\t\tPeople Count : " << pak.getProvince(i)->getPeopleCount() << "


millions" << endl;

cout << "\t\tExpenses : " << pak.getProvince(i)->getExpenses() << " millions"


<< endl;
ROLL NUMBER: DT-045

cout << "\t\tBudget : " << pak.getProvince(i)->getBudget() << " millions" <<
endl;

cout << "\t\tCities Count : " << pak.getProvince(i)->getCitiesCount() << endl;

for (int j = 0; j < pak.getProvince(i)->getCitiesCount(); j++)

cout << "\t\t-------- City" << j + 1 << " --------" << endl;

cout << "\t\tPeople Count :" << pak.getProvince(i)->getCities(j)-


>getPeopleCount() << " millions" << endl;

cout << "\t\tExpenses : " << pak.getProvince(i)->getCities(j)-


>getExpenses() << " millions" << endl;

cout << "\t\tBudget : " << pak.getProvince(i)->getCities(j)->getBudget() << "


millions" << endl;

cout << endl;

cout << "\n\t\t======== TOTAL EXPENSE OF THE COUNTRY ========" <<


endl;

cout << "\t\t\t\t" << totalExpenseCountry << " millions" << endl

<< endl;

highestExpensesP(pak);

finalFunction(pak);

return 0;

OUTPUT:
ROLL NUMBER: DT-045
ROLL NUMBER: DT-045

QUESTION NUMBER 4:
/*Junaid own an electronics shop and he wants to have an inventory
system for Laptops and

Mobile phones. Develop an inventory system for him, and also draw its
UML class diagram,

by using the following guidelines.

a. Write appropriate classes for Laptop and Mobile so that different


instances may be

created. There should be multiple constructors for initializing data


members and

destructors. Each class should have all members variable private. Setters
and getters

should be defined for each member variable.

b. The main program should have following logics:

i. Ask Junaid to enter the number of Laptops in his inventory. Create an


array of

that many instances (objects) of Laptop class.

ii. Ask Junaid to enter the number of Mobiles in his inventory. Create an
array of

that many instances of Mobile Phone class.

iii. Ask Junaid to enter purchase price of laptops and mobile phones by
showing

model number and name of the product.

iv. Ask Junaid to enter profit margin in percentage. Change the selling
price using

purchase price and profit margin by using formula: SellingPrice = Cost/ (1-

Margin%). Set updated value for all products.

v. At the end of the program, the output should be Name and Model
Number of
ROLL NUMBER: DT-045

each product, and total profit if all products are sold.*/

#include <iostream>

using namespace std;

class Laptop

private:

string name;

string modelNumber;

double purchasePrice;

double sellingPrice;

public:

Laptop() {}

Laptop(string name, string modelNumber) : name(name),


modelNumber(modelNumber){};

~Laptop() {}

string getName()

return name;

string getModelNumber()

return modelNumber;
ROLL NUMBER: DT-045

double getPurchasePrice()

return purchasePrice;

double getSellingPrice()

return sellingPrice;

void setName(string newName)

name = newName;

void setModelNumber(string newModelNumber)

modelNumber = newModelNumber;

void setPurchasePrice(double newPurchasePrice)

purchasePrice = newPurchasePrice;

}
ROLL NUMBER: DT-045

void setSellingPrice(double newSellingPrice)

sellingPrice = newSellingPrice;

};

class Mobile

private:

string name;

string modelNumber;

double purchasePrice;

double sellingPrice;

public:

Mobile() {}

Mobile(string name, string modelNumber) : name(name),


modelNumber(modelNumber){};

~Mobile() {}

string getName()

return name;

string getModelNumber()
ROLL NUMBER: DT-045

return modelNumber;

double getPurchasePrice()

return purchasePrice;

double getSellingPrice()

return sellingPrice;

void setName(string newName)

name = newName;

void setModelNumber(string newModelNumber)

modelNumber = newModelNumber;

void setPurchasePrice(double newPurchasePrice)

{
ROLL NUMBER: DT-045

purchasePrice = newPurchasePrice;

void setSellingPrice(double newSellingPrice)

sellingPrice = newSellingPrice;

};

int main()

cout << "-----------------------------------------------------" << endl;

cout << "This is the Assignment of Ebaad Khan Roll No: DT-045" << endl;

cout << "-----------------------------------------------------" << endl;

int numberOfLaptops;

cout << "Enter Number of Laptops: ";

cin >> numberOfLaptops;

Laptop *laptops = new Laptop[numberOfLaptops];

cout << endl

<< "Enter the name and model number of the laptops" << endl;

for (int i = 0; i < numberOfLaptops; ++i)

string newName;

cout << endl


ROLL NUMBER: DT-045

<< "Enter Model name: ";

cin >> newName;

laptops[i].setName(newName);

string newModelNumber;

cout << "Enter model number: ";

cin >> newModelNumber;

laptops[i].setModelNumber(newModelNumber);

int numberOfMobiles;

cout << endl

<< "Enter Number of Mobiles: ";

cin >> numberOfMobiles;

Mobile *mobiles = new Mobile[numberOfMobiles];

cout << endl

<< "Enter the name and model number of the mobiles" << endl;

for (int i = 0; i < numberOfLaptops; ++i)

string newName;

cout << endl

<< "Enter Model name: ";

cin >> newName;

mobiles[i].setName(newName);
ROLL NUMBER: DT-045

string newModelNumber;

cout << "Enter model number: ";

cin >> newModelNumber;

mobiles[i].setModelNumber(newModelNumber);

cout << endl

<< "Enter the purchase price of the laptops having following model number
and name" << endl;

for (int i = 0; i < numberOfLaptops; ++i)

cout << endl

<< "Model name: " << laptops[i].getName() << endl;

cout << "Model number: " << laptops[i].getModelNumber() << endl;

double newPurchasePrice;

cout << "Purchase price: ";

cin >> newPurchasePrice;

laptops[i].setPurchasePrice(newPurchasePrice);

cout << endl

<< "Enter the purchase price of the mobiles having following model number
and name" << endl;

for (int i = 0; i < numberOfMobiles; ++i)

{
ROLL NUMBER: DT-045

cout << endl

<< "Model name: " << mobiles[i].getName() << endl;

cout << "Model number: " << mobiles[i].getModelNumber() << endl;

double newPurchasePrice;

cout << "Purchase price: ";

cin >> newPurchasePrice;

mobiles[i].setPurchasePrice(newPurchasePrice);

double profitMargin;

cout << endl

<< "Enter the profit margin: ";

cin >> profitMargin;

for (int i = 0; i < numberOfLaptops; ++i)

double newSellingPrice = laptops[i].getPurchasePrice() / (1 - profitMargin /


100);

laptops[i].setSellingPrice(newSellingPrice);

for (int i = 0; i < numberOfMobiles; ++i)

double newSellingPrice = mobiles[i].getPurchasePrice() / (1 - profitMargin /


100);

mobiles[i].setSellingPrice(newSellingPrice);
ROLL NUMBER: DT-045

double totalProfit = 0;

for (int i = 0; i < numberOfLaptops; ++i)

totalProfit += laptops[i].getSellingPrice() - laptops[i].getPurchasePrice();

for (int i = 0; i < numberOfMobiles; ++i)

totalProfit += mobiles[i].getSellingPrice() - mobiles[i].getPurchasePrice();

cout << endl

<< "Laptops: " << endl;

for (int i = 0; i < numberOfLaptops; ++i)

cout << endl

<< "Name: " << laptops[i].getName() << endl;

cout << "Model Number: " << laptops[i].getModelNumber() << endl;

cout << endl

<< "Mobiles: " << endl;

for (int i = 0; i < numberOfMobiles; ++i)

cout << endl


ROLL NUMBER: DT-045

<< "Name: " << mobiles[i].getName() << endl;

cout << "Model Number: " << mobiles[i].getModelNumber() << endl;

cout << endl

<< "Total Profit: " << totalProfit << endl;

return 0;

OUTPUT:
ROLL NUMBER: DT-045

You might also like