Page 1 of 1

Question 1 A) What is the output of this program when it is executed? [4 marks] B) Now suppose we edit this program and

Posted: Tue May 24, 2022 8:33 am
by answerhappygod
Question 1
Question 1 A What Is The Output Of This Program When It Is Executed 4 Marks B Now Suppose We Edit This Program And 1
Question 1 A What Is The Output Of This Program When It Is Executed 4 Marks B Now Suppose We Edit This Program And 1 (86.27 KiB) Viewed 16 times
A) What is the output of this program when it is executed?
[4 marks]
B) Now suppose we edit this program and remove all occurrences of
the word virtual
from the code. When we recompile and execute it, what output does
it produce?
[4 marks]
C) When we execute this modified program with all the virtuals
removed, are there
any runtime errors? Justify your answer.
[3 marks
#include <iostream> #include <string> using namespace std; class Fruit { public: private: }; class Local Fruit : public Fruit { public: get_name (); } private: }; int main() { Fruit (string name) { name = new string (name); } virtual Fruit() { delete name; } virtual string get_name () const { return *name_;} string get_description () const { return "fruit: " + get_name(); } virtual string get_info() const { 11 return get_description () + on the table"; } string *name_; //fruit name. LocalFruit (string name, string type) Fruit (name) { type = new string (type); } virtual Local Fruit() { delete type_; } string get_description () const { return "local fruit "+ *type + ": virtual string get_info() const { return get_description () + " in the basement"; } string *type_; //fruit type Fruit *b = new Fruit ("Fruit King"); Fruit t = new Local Fruit ("Durian", "Musang King"); cout << b->get_description () << endl; cout << b->get_info() << endl; cout << t->get_description () << endl; cout << t->get_info () << endl; delete b; delete t; return 0; } continued on next page