subject

Save the following C++ program into a file classes. cpp #include using namespace std;
class A {
protected:
int x;
public:
A() { x = 0;}
void increment() { x++; }
virtual void decrement() { x--; }
void print() {cout << "x = " << x << endl;}
};
class B : public A {
public:
void increment() { x = x + 2; }
virtual void decrement() { x = x - 2; }
};

int main() {
A a; // a is an object of class A
A * ptr; // ptr is a pointer to an object of class A
ptr = &a;
ptr -> increment();
ptr -> print();
ptr -> decrement();
ptr -> print();
}
Compile the program by typing g++ classes. cpp, run it by typing ./a. out. Make sure that the program compiles and runs.
Question 1. In main add an object b of class B. Set the pointer ptr to point to b. Call increment() and decrement() on the object pointed to by ptr, print the object after each call. Which method gets called in each case? Why?
Question 2. Add a protected variable y to B and a constructor to initialize it to 0. Add a print() method in B so that both x and y are printed. Which print method is called on the object B referenced by ptr? If needed, change method declarations so that the print method of B is called for the B object. What did you need to change and why?
Question 3. Assuming that a is a variable of type A, b is an object of type B, and ptr is the pointer of type A, what would you expect to be printed by the following statement:
a = b;
a. print();
ptr = &a;
ptr -> print();
What gets printed? Why? Please explain the difference in behavior between this example and Question 2.
Question 4. What happens if you have an object b of class B and you call the print method like this: b. A::print()?
Question 5. Write a function that takes an object of class A and prints it (don't forget to add the function prototype at the top of the file). Can you pass an object of a class B to this function?
Question 6. Write a function that takes an object of class A and returns it. Can you pass an object of a class B to this function?
Question 7. What happens if you change the declaration
class B : public A
to
class B : A
?

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 20:30
In this lab, you complete a prewritten c program that calculates an employee’s productivity bonus and prints the employee’s name and bonus. bonuses are calculated based on an employee’s productivity score as shown below. a productivity score is calculated by first dividing an employee’s transactions dollar value by the number of transactions and then dividing the result by the number of shifts worked.
Answers: 3
question
Computers and Technology, 23.06.2019 14:00
Need ! will choose brainliest! discuss the role of abstraction in the history of computer software.
Answers: 1
question
Computers and Technology, 24.06.2019 11:00
Under the home tab, where can a user find options to change the bullet style of an outline? in the slides group in the font group in the paragraph group in the drawing group
Answers: 1
question
Computers and Technology, 24.06.2019 13:30
To move an excel worksheet tab, simply right-click on it drag and drop it double-click on it delete it
Answers: 1
You know the right answer?
Save the following C++ program into a file classes. cpp #include using namespace std;
class A...
Questions
question
Mathematics, 04.11.2020 03:10
question
Social Studies, 04.11.2020 03:10
question
Mathematics, 04.11.2020 03:10
question
English, 04.11.2020 03:10
question
Mathematics, 04.11.2020 03:10
question
Mathematics, 04.11.2020 03:10
question
Mathematics, 04.11.2020 03:10
question
Social Studies, 04.11.2020 03:10
question
Mathematics, 04.11.2020 03:10
Questions on the website: 13722361