Quiz 8

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

The virtual keyword must be used with _________. 1. the base-class 2. neither the base-class nor the derived class 3. either the base-class or the derived class 4. the derived class

1. the base-class

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; q1->set_text("What is C++?"); ChoiceQuestion* cq1 = q1; cq1->set_answer("A Mine Field among Programming Languages!"); cq1->display(); return 0; } 1. Question: Answer: A Mine Field among Programming Languages! 2. 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. 3. Question: What is C++ Answer: 4. Question: What is C++ Answer: A Mine Field among Programming Languages!

2. 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.

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(10, 20); c1 = ac1; return 0; } Which one of the following statements is false? 1. ac1.height = 20 0% 2. c1.speed = 10 3. c1.height = 20 4. ac1 data is sliced away.

3. c1.height = 20

Study the following two class interfaces: class Question { public: Question(); void set_text(string new_text); void set_answer(string new_answer); void display() const; private: string text; string answer; }; class ChoiceQuestion : public Question { public: ChoiceQuestion(); void set_text(string new_text); }; Which member function from the Question class is overridden in the ChoiceQuestion class? Student Response Value Correct Answer Feedback 1. void display() const 2. Question() 3. void set_text(string new_text) Student Response 4. void set_answer(string new_answer)

3. void set_text(string new_text) Student Response

What is the output of the following code snippet? class Car { public: Car(); virtual void set_speed(double new_speed); void accelerate(double new_speed); double get_speed() const; private: double speed; }; Car::Car() { speed = 0; } void Car::set_speed(double new_speed) { speed = new_speed; } void Car::accelerate(double new_speed) { speed = speed + new_speed; } double Car::get_speed() const { return speed; } class AeroCar : public Car { public: AeroCar(); void set_speed(double new_speed); virtual void accelerate(double new_speed); double get_speed() const; }; AeroCar::AeroCar() : Car() {} void AeroCar::set_speed(double new_speed) { Car::set_speed(Car::get_speed() + new_speed); } void AeroCar::accelerate(double new_speed) { Car::set_speed(new_speed); } double AeroCar::get_speed() const { return Car::get_speed(); } int main() { AeroCar ac1; AeroCar* ac2 = new AeroCar; ac1.set_speed(10); ac1.accelerate(20); ac2->set_speed(10); ac2->accelerate(20); Car* c1 = new Car; c1 = ac2; cout << "First Object Speed: " << ac1.get_speed(); cout << "; Second Object Speed: " << c1->get_speed(); return 0; } 1. First Object Speed: 10; Second Object Speed: 20 2. First Object Speed: 20; Second Object Speed: 10 3. First Object Speed: 10; Second Object Speed: 10 4. First Object Speed: 20; Second Object Speed: 20

4. First Object Speed: 20; Second Object Speed: 20

What is the output of the following code snippet? class Employee { public: Employee() { salary = 0; } Employee(string new_name) { name = new_name; } Employee(double new_salary) { salary = new_salary; } Employee(string new_name, double new_salary) { name = new_name; salary = new_salary; } string get_name()const { return name; } double get_salary() const { return salary; } protected: string name; double salary; }; class Manager : public Employee { public: Manager(){} Manager(string new_department, string new_name, double new_salary) Employee(new_name, new_salary){ department = new_department;} void display_info() const { cout << name << " " << department << " $" << salary << endl; } private: string department; }; int main() { Manager mgr("Production", "Susan Cray", 12000); mgr.display_info(); return 0; } 1. Susan Cray $12000 2. Susan Cray Production $0 3. Susan Salary = $12000 4. Susan Cray Production $12000

4. Susan Cray Production $12000

Consider the following declaration for the Soldier class. class Soldier { public: Soldier(); void set_rank(string new_rank); string get_rank() const; private: string rank; }; The Major class inherits from the Soldier class. For the Major class to override the set_rank function, what must be done? 1. The Major class must define the function void override_set_rank(string new_rank) 2. The Major class must define the function void override(string set_rank, string new_rank) 3. The Major class cannot override the set_rank function. 4. The Major class must define the function void set_rank(string new_rank)

4. The Major class must define the function void set_rank(string new_rank)

Suppose the class Jaguar inherits from the class Animal. Which of the following is the correct way of defining the class Jaguar in the code? 1. class Jaguar::public Animal 2. class Jaguar ; public Animal 3. class Jaguar inherits public Animal 4. class Jaguar : public Animal

4. class Jaguar : public Animal


Kaugnay na mga set ng pag-aaral

Ch. 7 Caring in Nursing Practice

View Set

Chapter 4 Environmental Science (still in progress)

View Set

Module 2 - The Nature of Physical Geography

View Set

Saunders: Prenatal Period and Risk Conditions

View Set

Lewis Ch 31 Hematologic Problems NCLEX

View Set