semester project oop
semester project oop
#include <string>
// Class Car
class Car {
public:
double speed;
bool status;
// Parameterized Constructor
status)
// Destructor
~Car() {}
cout << "Car No: " << no << ", Company: " << company
<< ", Speed: " << speed << " km/h, Status: "
// Avg Speed
}
// Compare speed
cout << car1.no << " has the highest speed: " << car1.speed <<
cout << car2.no << " has the highest speed: " << car2.speed <<
} else {
cout << "Both cars have the same speed: " << car1.speed << "km/h" << endl;
};
// Class Road
class Road {
public:
double* lengths;
int numRoads;
// Road Constructor
// Destructor
~Road() {
delete[] lengths;
// Length Function
// No of roads
// Sorting Roads
maxLength = lengths[i];
cout << "The longest road is: " << maxLength << " meters" << endl;
int activeCount = 0;
if (cars[i].status) {
activeCount++;
return activeCount;
}
};
// Class Intersection
public:
string id;
// Constructor
: Road(roadCount), id(intersectionID) {}
// Intersection Id
displayRoadInfo();
};
int main() {
car1.displayCarInfo();
car2.displayCarInfo();
cout << "Average speed of the two cars: " << avgSpeed << " km/h" <<
endl;
Car::compareSpeed(car1, car2);
int roadCount = 3;
double roadLengths[] = {150.5, 200.3, 180.8};
intersection.setRoadLengths(roadLengths);
intersection.displayIntersectionInfo();
intersection.findLongestRoad();
cout << "Number of active cars on the road: " << numActiveCars <<
endl;
return 0;
Output:
Q2: Capital trust Bank needs to implement an account management system that can handle
various types of bank accounts, specifically Current Accounts and Savings Accounts. The
system should be designed using object-oriented principles, leveraging inheritance and
polymorphism to manage account operations effectively. You are tasked with designing and
implementing this system with the following requirements:
1. Create an abstract class called Account having data members:
▸ Private Account Number and Account Balance.
▸ Add suitable setter/getter for data.
▸ Add Debit(float), Credit(float) as member functions (Pure Virtual).
▸ Add Print () function (Virtual) o Override Print Debit and Credit functions according to
derived classes.
2. Create a class called CurrentAccount i-e: CurrentAccount(is-a) Account having data
members:
Service Charges (To be charged during credit if account balance is less than min
balance)
Minimum Balance
Modify the definition of the print() so that it displays a suitable message containing above
info.
Similarly override credit(float), debit(float) functions such that credit(float) simply add
amount to the Account Balance and debit(float) checks if the amount to be debited is within
the range of Account Balance.
4. In main function:
Create an array of Account type Pointers, of size 3.
Assign Account object to index0, CurrentAccount object to index 1 and SavingAccount object
to index 2.
Have you encountered any problem? Report the problem and change the size and
elements of the array accordingly
Now Credit and Debit the CurrentAccount and SavingAccount in main function
Although things seem to be fine on the surface, there is a problem in the program we just
wrote. To observe this problem, we must add destructors for all classes. Paste the following
inline definitions of the destructors in their corresponding classes, execute the program and
paste the output below. Your destructors should delete the child classes first before
destroying parent class. There should not be any memory leaks.
Solution:
#include<iostream>
#include<string>
public:
int pan;
double balance;
Account(){}
~Account(){}
};
public:
int A;
cra(){
this->pan=pan;
this->balance=balance;
this->A=A;
this->minbalance;
this->servicecharges;
void get(){
cin>>pan;
cout<<"Enter your balance of account :"<<endl;
cin>>balance;
void set(){
this->pan;
this->balance;
cout<<"Your ac no:"<<pan<<endl;
void print(){
if(balance>minbalance||balance==minbalance){
else {
double debit(){
cin>>A;
if(A>minbalance||A==minbalance){
else {
cout<<"You have low balance :"<<endl;
};
double cr(){
cin>>A;
~cra(){}
};
public:
int A;
sva(){
this->pan=pan;
this->balance=balance;
this->A=A;
this->minbalance;
this->servicecharges;
void get(){
this->pan;
this->balance;
cin>>pan;
cin>>balance;
void set(){
cout<<"Your ac no:"<<pan<<endl;
void print(){
if(balance>minbalance||balance==minbalance){
else {
double debit(){
cin>>A;
if(A<balance||A==balance){
cout<<"You have withdrawn : "<<A<<endl;
else {
cout<<balance-servicecharges;
};
double cr(){
cin>>A;
~sva(){}
};
int main(){
Account *v;
Account*v1=new cra();
Account*arr[3]={v,v1,v2};
//
//arr[1]->get();
// arr[1]->set();
// arr[1]->print();
// arr[1]->debit();
// delete arr[1];
arr[2]->get();
arr[2]->set();
arr[2]->print();
arr[2]->cr();
arr[2]->debit();
delete arr[2];
delete arr[0];
return 0;
Output: