Question: PLEASE, EXPLAIN CLEARLY YOUR ANSWERS (CLASSES & INHERITANCE C++) CLASSES & INHERITANCE QUESTIONS (I HAVE THE ANSWERS, I NEED CLEAR EXPLANIANTION) 1)What is the output

PLEASE, EXPLAIN CLEARLY YOUR ANSWERS (CLASSES & INHERITANCE C++)

CLASSES & INHERITANCE QUESTIONS (I HAVE THE ANSWERS, I NEED CLEAR EXPLANIANTION)

1)What is the output of the following code snippet?

class Car

{

public:

Car(double speed);

void start();

void accelerate(double speed);

void stop();

double get_speed() const;

private:

double speed;

};

Car::Car(double speed)

{

this->speed = speed;

}

void Car::start()

{

speed = 1;

}

void Car::accelerate(double speed)

{

this->speed = this->speed + speed;

}

void Car::stop()

{

speed = 0;

}

double Car::get_speed() const

{

return speed;

}

int main()

{

Car* c1 = new Car(90);

c1->start();

c1->accelerate(90);

cout << "Speed: " << c1->get_speed() << endl;

c1->stop();

return 0;

}

A)Speed: 1

B)Speed: 90

C)Speed: 91

D)There is no output because the code snippet does not compile.

Ans: C

Study the following code snippet:

class Car

{

public:

Car();

Car(double new_speed);

double get_speed() const;

private:

double speed;

};

Car::Car()

{

speed = 0;

}

Car::Car(double new_speed)

{

speed = new_speed;

}

double Car::get_speed()

{

return speed;

}

class AeroCar : public Car

{

public:

AeroCar();

AeroCar(double new_height, double new_speed);

void display_data() const;

private:

double height;

};

AeroCar::AeroCar()

{

height = 0;

}

AeroCar::AeroCar(double new_height, double new_speed) : Car(new_speed)

{

height = new_height;

}

void AeroCar::display_data() const

{

cout << Car::get_speed() << height << endl;

}

int main()

{

Car c1;

Car c2(10);

AeroCar ac1(20, 10);

c1 = ac1;

return 0;

}

Which one of the following statements is false?

A)ac1 data is sliced away.

B)ac1.height = 20.

C)c1.speed = 10.

D)c1.height = 20.

Ans: D

What is the output of the following code snippet?

class Question

{

public:

Question();

void set_text(string new_text);

void set_answer(string new_answer);

void display() const;

private:

string text;

string answer;

};

Question::Question()

{

text = "";

answer = "";

}

void Question::set_text(string new_text)

{

text = new_text;

}

void Question::set_answer(string new_answer)

{

answer = new_answer;

}

void Question::display() const

{

cout << "Question: " << text << endl;

cout << "Answer: " << answer << endl;

}

class ChoiceQuestion : public Question

{

public:

ChoiceQuestion();

void set_text(string new_text);

void set_answer(string new_answer);

void display() const;

};

ChoiceQuestion::ChoiceQuestion()

: Question()

{}

void ChoiceQuestion::set_text(string new_text)

{

Question::set_text(new_text);

}

void ChoiceQuestion::set_answer(string new_answer)

{

Question::set_answer(new_answer);

}

void ChoiceQuestion::display() const

{

Question::display();

}

int main()

{

Question* q1 = new Question;

q1->set_text("What is C++?");

ChoiceQuestion* cq1 = q1;

cq1->set_answer("A Mine Field among Programming Languages!");

cq1->display();

return 0;

}

A)Question: What is C++

Answer: A Mine Field among Programming Languages!

B)Question:

Answer: A Mine Field among Programming Languages!

C)Question: What is C++

Answer:

D)There is no output due to a compilation error because a base-class object is assigned to a derived class object variable, which is not allowed.

Ans: D

What is the output of the following code snippet?

class Question

{

public:

Question();

void set_text(string new_text);

void set_answer(string new_answer);

void display() const;

private:

string text;

string answer;

};

Question::Question()

{

text = "";

answer = "";

}

void Question::set_text(string new_text)

{

text = new_text;

}

void Question::set_answer(string new_answer)

{

answer = new_answer;

}

void Question::display() const

{

cout << "Question: " << text << endl;

cout << "Answer: " << answer << endl;

}

class ChoiceQuestion : public Question

{

public:

void add_choice(string choice, bool correct);

private:

vector choices;

};

void ChoiceQuestion::add_choice(string choice, bool correct)

{

...

}

int main()

{

Question* q1 = new Question;

q1->set_text("What is C++?");

ChoiceQuestion* cq1 = new ChoiceQuestion;

cq1->set_answer("A Mine Field among Programming Languages!");

cq1->add_choice("C++ is a simple programming language.", false);

q1 = cq1;

q1->display();

return 0;

}

A)Question: What is C++?

Answer: A Mine Field among Programming Languages!

B)Question:

Answer: A Mine Field among Programming Languages!

C)Question: What is C++?

Answer:

D)There is no output due to compilation errors.

Ans: B

Consider the following code snippet:

class Employee

{

public:

Employee();

Employee(string new_name, double new_salary);

void set_name(string new_name);

virtual void set_salary(double new_salary);

string get_name() const;

double get_salary() const;

private:

string name;

double salary;

};

void Employee::set_salary(double new_salary)

{

salary = new_salary;

}

class Manager : public Employee

{

public:

Manager();

Manager(double new_bonus);

Manager(string new_name, double new_salary, double new_bonus);

void set_salary(double new_salary);

void set_bonus(double new_bonus);

void print_data() const;

private:

double bonus;

};

void Manager::set_salary(double new_salary)

{

Employee::set_salary(new_salary * 2);

}

class Officer : public Employee

{

public:

Officer();

Officer(string new_name, double new_salary, double new_bonus);

void set_bonus(double new_bonus);

void print_data() const;

private:

double bonus;

};

int main()

{

Employee* emp = new Employee;

Officer* officer = new Officer;

emp = officer;

emp->set_salary(100);

return 0;

}

Which of the following functions is called by the "emp->set_salary(100);" statement?

A)void Employee::set_salary(double new_salary);

B)void Manager::set_salary(double new_salary);

C)void Officer::set_salary(double new_salary);

D)None of the listed items.

Ans: A

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 Programming Questions!