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

Suppose that you have the following classes, classA and classB: class classA { public: virtual void print() const; void doubleNum(); classA(int a = 0); private: int x; }; void classA::print() const { cout << "ClassA x: " << x << endl; } void classA::doubleNum() { x = 2 * x; } classA::classA(int a) { x = a; } class classB: public classA { public: void print() const; void doubleNum(); classB(int a = 0, int b = 0); private: int y; };void classB::print() const { classA::print(); cout << "ClassB y: " << y << endl; } void classB::doubleNum() { classA::doubleNum(); y = 2 * y; } classB::classB(int a, int b) : classA(a) { y = b; } What is the output of the following function main? int main() { classA *ptrA; classA objectA(2); classB objectB(3, 5); ptrA = &objectA ptrA->doubleNum(); ptrA->print(); cout << endl; ptrA = &objectB ptrA->doubleNum(); ptrA->print(); cout << endl; return 0; }

Short Answer

Expert verified
ClassA x: 4 ClassA x: 6 ClassB y: 10

Step by step solution

01

Define Class Object Instances

In the `main` function, two objects are instantiated: `objectA` of type `classA` with initial `x` value of 2, and `objectB` of type `classB` with an initial `x` value of 3 and `y` value of 5.
02

Set Pointer to classA Object

A pointer `ptrA` of type `classA*` is set to point to `objectA`. The method `doubleNum()` is called on this pointer, which doubles `x` in `objectA` from 2 to 4. Then, it calls the `print()` method, outputting "ClassA x: 4".
03

Set Pointer to classB Object

The pointer `ptrA` is reassigned to point to `objectB`. The `doubleNum()` method is called again. In `classB`, this method first calls `doubleNum()` of `classA` to double `x` from 3 to 6, and then doubles `y` from 5 to 10. The `print()` method is invoked, outputting both values for `x` and `y`: "ClassA x: 6", "ClassB y: 10".

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Virtual Functions
Virtual functions play an essential role in C++ programming, especially in scenarios where you deal with inheritance. A virtual function in a superclass allows derived classes to override it. This mechanism ensures that the correct method is called for an object, even when using a base class pointer.
  • In the given example, `print()` is declared as a virtual function in `classA`. This means that when a `classA` pointer holds an object of `classB`, and you call the `print()` function, C++ will execute the `print()` method defined in `classB`, if it exists.
  • Virtual functions are especially useful in a polymorphic context, where objects are treated as instances of their base class.
  • They enable dynamic binding – the process of linking a function call to the code to execute at runtime.
Defining a function as virtual helps achieve flexibility and reusability in code. It allows subclasses to present their versions of the function, making software design modular and adhering to the open/closed principle.
Polymorphism
Polymorphism is a cornerstone of object-oriented programming that allows for treating objects of different classes in a uniform way. Generally, polymorphism is categorized as either compile-time (e.g., function overloading) or runtime (achieved through inheritance and virtual functions).
  • Runtime polymorphism is demonstrated in the exercise through the use of the `classA` pointer, `ptrA`. Though `ptrA` is of type `classA*`, it can point to and operate on objects of both `classA` and `classB` due to polymorphism.
  • This feature allows you to interact with differently derived objects through a common interface, as seen with the invocation of the `doubleNum()` and `print()` functions.
  • Polymorphism enhances flexibility in code management, reducing the need for multiple function overloads or switch-case logic based on object types.
The ability to use a single pointer or reference to call methods on different derived classes makes polymorphism a powerful tool for implementing flexible and maintainable code.
Object-Oriented Programming
Object-oriented programming (OOP) offers a robust framework for building and managing complex software. OOP is based on four main principles: encapsulation, abstraction, inheritance, and polymorphism. In the given exercise, inheritance and polymorphism are highlighted, demonstrating the power and versatility of OOP. Inheritance allows a new class, `classB`, to derive properties and behaviors (methods) from an existing class, `classA`. This reduces redundancy and promotes code reuse.
  • `classA` provides a base blueprint with attributes and functions that `classB` extends and customizes.
  • This reuse of logic helps avoid code duplication, enhancing maintainability and scalability.
Encapsulation is seen in how data (like `x` and `y`) are bundled together with methods such as `doubleNum()` to manipulate them, keeping implementation details hidden. Overall, OOP principles lead to well-structured code that mirrors real-world systems, simplifies complex problems, and makes collaboration on projects easier through modular design and clear interfaces.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Mark the following statements as true or false. a. In C++, pointer is a reserved word. b. In C++, pointer variables are declared using the word pointer. c. The statement delete p; deallocates the variable pointer p. d. The statement delete p; deallocates the dynamic variable that is pointed to by p. e. Given the declaration: int list[10]; int *p; the statement: p = list; is valid in C++. f. Given the declaration: int *p; the statement: p = new int[50]; dynamically allocates an array of 50 components of type int, and p contains the base address of the array. g. The address of operator returns the address and value of its operand. h. If p is a pointer variable, then the statement p = p * 2; is valid in C++.

Consider the following statement: int* p, q; This statement could lead to what type of misinterpretation?

What is the output of the following C++ code? int *first = new int; int *second; *first = 85; second = first; *second = *second + *first; first = new int; *first = *second - 100; cout << *first << " " << *second << endl;

Consider the following statement: int *num; a. Write the C++ statement that dynamically creates an array of 10 components of type int and num contains the base address of the array. b. Write a C++ code that inputs data into the array num from the standard input device. c. Write a C++ statement that deallocates the memory space of array to which num points.

What is the output of the following C++ code? int *myList = new int[5]; int *yourList = new int[10]; myList[0] = 3; for (int i = 1; i < 5; i++) myList[i] = myList[i - 1] + i; for (int i = 0; i < 5; i++) { yourList[i] = myList[i] + 4; yourList[i + 5] = myList[4 - i] - 3; } cout << "myList: "; for (int i = 0; i < 5; i++) cout << myList[i] << " "; cout << endl; cout << "yourList: "; for (int i = 0; i < 10; i++) cout << yourList[i] << " "; cout << endl;

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free