Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 18

include #include using namespace std; class baseClass { public: void print() const; baseClass(strin… # What is the output of the following C++ program? #include #include using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int main() { baseClass baseObject("This is the base class", 2); derivedClass derivedObject("DDDDDD", 3, 7); baseObject.print(); derivedObject.print(); return 0; } void baseClass::print() const { cout << x << " " << str << endl; } baseClass::baseClass(string s, int a) { str = s; x = a; } void derivedClass::print() const { cout << "Derived class: " << y << endl; baseClass::print(); } derivedClass::derivedClass(string s, int a, int b) :baseClass("Hello Base", a + b) { y = b; }

Problem 19

Consider the following class definitions: class baseClass { public: void print() const; int getX() const; baseClass(int a = 0); protected: int x; }; class derivedClass: public baseClass { public: void print() const; int getResult() const; derivedClass(int a = 0, int b = 0); private: int y; }; Suppose the definitions of the member functions of these classes are as follows: void baseClass::print() const { cout << "In base: x = " << x << endl; } baseClass::baseClass(int a) { x = a; } int baseClass::getX() const { return x; } void derivedClass::print() const { cout << "In derived: x = " << x << ", y = " << y << "; x + y = " << x + y << endl; } int derivedClass::getResult() const { return x + y; } derivedClass::derivedClass(int a, int b) : baseClass(a) { y = b; } What is the output of the following function main? int main() { baseClass baseObject(7); derivedClass derivedObject(3, 8); baseObject.print(); derivedObject.print(); cout << "****" << baseObject.getX() << endl; cout << "" << derivedObject.getResult() << endl; return 0; }

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks