Oop Assignment#01
Oop Assignment#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
Instantiate two different objects of class SavingsAccount, saver1 and saver2, with
balances of
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
#include <iostream>
class SavingsAccount
{
ROLL NUMBER: DT-045
private:
double savingsBalance;
public:
void calculateMonthlyInterest()
savingsBalance += monthlyInterestRate;
annualInterestRate = newRate;
double displayBalance()
return savingsBalance;
};
int main()
{
ROLL NUMBER: DT-045
cout << "This is the Assignment of Ebaad Khan Roll No: DT-045" << 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:
• A member variable for the year that the ship was built (a string)
• 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
• 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
• 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>
class Ship
public:
ROLL NUMBER: DT-045
string nameofship;
string year;
string newyear;
char YN;
: nameofship(nameofship), year(year) {}
void set_name()
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;
void display_year()
ROLL NUMBER: DT-045
cout << "The ship was built in the year: " << this->year << endl;
/*void modify_year()
if (this->YN == 'Y')
displaynewyear();
else
}*/
void displaynewyear()
cout << "The ship name is: " << this->nameofship << " and the year is: " << this-
>newyear << endl;
cout << "The name of the ship is: " << this->nameofship << " and it was built in the
year: " << this->year << endl;
};
private:
int max_passengers;
public:
return max_passengers;
max_passengers = maxpass;
{
ROLL NUMBER: DT-045
cout << "The name of the ship is: " << this->nameofship << " and the max number of
passengers is: " << this->max_passengers << endl;
};
public:
int capacity;
void set_capacity()
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 << "This is the Assignment of Ebaad Khan Roll No: DT-045" << endl;
Ship *ships[3];
ships[i]->display_info();
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.
iii. Consider your ID as the total budget in millions for country “Pak”, for example
vi. Print all details in a tabular form that shows all provinces and their members
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.
f. Write a function finalFunction() which prints profit or loss using budget and
expenses
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.
iii. Consider your ID as the total budget in millions for country “Pak”, for example
ROLL NUMBER: DT-045
vi. Print all details in a tabular form that shows all provinces and their members
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.
f. Write a function finalFunction() which prints profit or loss using budget and
expenses
#include <iostream>
class cities
private:
int peopleCount;
float budget;
ROLL NUMBER: DT-045
float expenses;
public:
peopleCount = pc;
budget = b;
expenses = e;
peopleCount = pc;
return peopleCount;
void setBudget(float b)
budget = b;
return budget;
ROLL NUMBER: DT-045
void setExpenses(float e)
expenses = e;
return expenses;
};
class provinces
private:
int citiesCount;
int peopleCount;
float budget;
float expenses;
cities *citiesInProvinces;
public:
{
ROLL NUMBER: DT-045
citiesCount = c;
peopleCount = pc;
budget = b;
expenses = e;
citiesInProvinces[i] = cities();
~provinces()
delete[] citiesInProvinces;
peopleCount = pc;
return peopleCount;
void setBudget(float b)
{
ROLL NUMBER: DT-045
budget = b;
return budget;
void setExpenses(float e)
expenses = e;
return expenses;
void setCitiesCount(int c)
citiesCount = c;
delete[] citiesInProvinces;
citiesInProvinces[i] = cities();
}
ROLL NUMBER: DT-045
return citiesCount;
return &citiesInProvinces[index];
};
class country
private:
int provincesCount;
int peopleCount;
float budget;
float expenses;
provinces *provincesInCountry;
public:
provincesCount = p;
ROLL NUMBER: DT-045
peopleCount = pc;
budget = b;
expenses = e;
~country()
delete[] provincesInCountry;
peopleCount = pc;
return peopleCount;
void setBudget(float b)
budget = b;
return budget;
}
ROLL NUMBER: DT-045
void setExpenses(float e)
expenses = e;
return expenses;
void setProvincesCount(int p)
provincesCount = p;
delete[] provincesInCountry;
return provincesCount;
return &provincesInCountry[index];
};
{
ROLL NUMBER: DT-045
int totalExpenses = 0;
int provinceExpenses = 0;
provinceExpenses += obj.getProvince(i)->getCities(j)->getExpenses();
obj.getProvince(i)->setExpenses(provinceExpenses);
totalExpenses += obj.getProvince(i)->getExpenses();
return totalExpenses;
int highestExpenses;
highestExpenses = obj.getProvince(i)->getExpenses();
highestExpensesProvince = province;
if (highestExpensesProvince != nullptr)
cout << "\t\t======== Province with the Highest Expenses ========" <<
endl;
cout <<
"\t\t==========================================================" <<
endl;
cout <<
"\t\t==========================================================" <<
endl;
string result;
if (profitLoss >= 0)
result = "Profit";
else
result = "Loss";
cout << profitLoss << " M (" << result << ")" << endl;
}
ROLL NUMBER: DT-045
int main()
<< endl;
country pak;
pak.setProvincesCount(3);
pak.setPeopleCount(239);
pak.setBudget(22099);
cout << "People Count: " << pak.getPeopleCount() << " millions" << endl;
cout << "Budget: " << pak.getBudget() << " millions" << endl
<< endl;
int peopleCountProvinces;
float expenses;
cout << "Enter people count for province " << i + 1 << " in millions "
pak.getProvince(i)->setPeopleCount(peopleCountProvinces);
pak.getProvince(i)->setBudget(budgetForProvinces);
pak.getProvince(i)->setCitiesCount(2);
int peopleCountCities;
float expensesCities;
cout << "Enter people count for city " << j + 1 << " in millions "
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 << "\t\t======== Province" << i + 1 << " ========" << endl;
cout << "\t\tBudget : " << pak.getProvince(i)->getBudget() << " millions" <<
endl;
cout << "\t\t-------- City" << j + 1 << " --------" << 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,
destructors. Each class should have all members variable private. Setters
and getters
ii. Ask Junaid to enter the number of Mobiles in his inventory. Create an
array of
iii. Ask Junaid to enter purchase price of laptops and mobile phones by
showing
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-
v. At the end of the program, the output should be Name and Model
Number of
ROLL NUMBER: DT-045
#include <iostream>
class Laptop
private:
string name;
string modelNumber;
double purchasePrice;
double sellingPrice;
public:
Laptop() {}
~Laptop() {}
string getName()
return name;
string getModelNumber()
return modelNumber;
ROLL NUMBER: DT-045
double getPurchasePrice()
return purchasePrice;
double getSellingPrice()
return sellingPrice;
name = newName;
modelNumber = newModelNumber;
purchasePrice = newPurchasePrice;
}
ROLL NUMBER: DT-045
sellingPrice = newSellingPrice;
};
class Mobile
private:
string name;
string modelNumber;
double purchasePrice;
double sellingPrice;
public:
Mobile() {}
~Mobile() {}
string getName()
return name;
string getModelNumber()
ROLL NUMBER: DT-045
return modelNumber;
double getPurchasePrice()
return purchasePrice;
double getSellingPrice()
return sellingPrice;
name = newName;
modelNumber = newModelNumber;
{
ROLL NUMBER: DT-045
purchasePrice = newPurchasePrice;
sellingPrice = newSellingPrice;
};
int main()
cout << "This is the Assignment of Ebaad Khan Roll No: DT-045" << endl;
int numberOfLaptops;
<< "Enter the name and model number of the laptops" << endl;
string newName;
laptops[i].setName(newName);
string newModelNumber;
laptops[i].setModelNumber(newModelNumber);
int numberOfMobiles;
<< "Enter the name and model number of the mobiles" << endl;
string newName;
mobiles[i].setName(newName);
ROLL NUMBER: DT-045
string newModelNumber;
mobiles[i].setModelNumber(newModelNumber);
<< "Enter the purchase price of the laptops having following model number
and name" << endl;
double newPurchasePrice;
laptops[i].setPurchasePrice(newPurchasePrice);
<< "Enter the purchase price of the mobiles having following model number
and name" << endl;
{
ROLL NUMBER: DT-045
double newPurchasePrice;
mobiles[i].setPurchasePrice(newPurchasePrice);
double profitMargin;
laptops[i].setSellingPrice(newSellingPrice);
mobiles[i].setSellingPrice(newSellingPrice);
ROLL NUMBER: DT-045
double totalProfit = 0;
return 0;
OUTPUT:
ROLL NUMBER: DT-045