Question: Assignment 3 code: #include #include using namespace std; class car { public: car(); car(int s) : speed(s) {}; void accelerate(int new_speed); void stop(); void getSpeed()

Assignment 3 code:

#include

#include

using namespace std;

class car

{

public:

car();

car(int s) : speed(s) {};

void accelerate(int new_speed);

void stop();

void getSpeed() const;

private:

int speed;

};

car::car()

{

speed = 0;

}

class sportscar : public car

{

public:

sportscar();

sportscar(int s);

void turbo(bool flag);

bool isTurbo() const;

private:

bool hasTurbo;

};

sportscar::sportscar()

{

hasTurbo = true;

}

sportscar::sportscar(int s) : car(s)

{

}

void car::accelerate(int new_speed)

{

speed = new_speed;

}

void car::stop()

{

speed = 0;

}

void car::getSpeed() const

{

cout << "Speed: " << speed;

}

void sportscar::turbo(bool flag)

{

hasTurbo = flag;

}

bool sportscar::isTurbo() const

{

return hasTurbo;

}

int main()

{

sportscar scar;

scar.accelerate(15);

scar.getSpeed();

scar.stop();

scar.turbo(false);

cout << endl << "Has Turbo?: " << scar.isTurbo() << endl << endl;

vector car_vector;

car_vector.push_back(new car());

car_vector.push_back(new car());

car_vector.push_back(new sportscar());

car_vector.push_back(new sportscar());

for (int i = 0; i < car_vector.size(); i++)

{

car_vector[i]->accelerate(2);

car_vector[i]->getSpeed();

cout << " ";

}

}

1a. Write C++ code to modify your Car class from Assignment 3 (the code above) to use protected member variables. Then modify your Sportscar class from Assignment 3 to directly access the private member variables of Car.

1b. Write C++ code to modify your programs from Problem #1a above so that Sportscar is named as a friend of the Car class, and all member variables are again private.

1c. Write C++ code to modify your programs from Problem #1b above so that Car and Sportscar are contained within a new namespace called Assignment_7

1d. Write C++ code to modify your programs from Problem #1c to make Sportscar a nested class of Car.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!