Compile the program classes.cpp, run it.  Make sure that the program compiles and runs. Question 1.  In the main function, 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 int y to B and a constructor to initialize y 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.  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 class B to this function? Question 5.  Write a function that takes an object of class A and returns it.  Can you pass an object of class B to this function? Paste your modified file: classes.cpp and the answers to all questions as the comments in the file online in one document file. Include the questions, program outputs, and your answers in the comment section in the classes.cpp file for each question separately.   No screenshots. I need to be able to copy and paste your code to run it. NOTE THAT YOU CAN SUBMIT THE ASSIGNMENT ONLY ONCE WHEN USING Turnitin.. See below the layout format (more consistency for grading): /* Question 1.  In the main function, 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?  */ < Submit only the changed  program  code for question 1, in this case, the main()>   /* Quest 2 ........ ....... */ and so on.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Save the following C++ program into a file classes.cpp:

#include <iostream>
using namespace std;

class A {
public:
A() { x = 0;}
void increment() { x++; }
virtual void decrement() { x--; }
void print() {cout << "x = " << x << endl;}
protected:
int x;
};


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 classes.cpp, run it.  Make sure that the program compiles and runs.

Question 1.  In the main function, 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 int y to B and a constructor to initialize y 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.  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 class B to this function?

Question 5.  Write a function that takes an object of class A and returns it.  Can you pass an object of class B to this function?

Paste your modified file: classes.cpp and the answers to all questions as the comments in the file online in one document file. Include the questions, program outputs, and your answers in the comment section in the classes.cpp file for each question separately.   No screenshots. I need to be able to copy and paste your code to run it. NOTE THAT YOU CAN SUBMIT THE ASSIGNMENT ONLY ONCE WHEN USING Turnitin.. See below the layout format (more consistency for grading):

/*
Question 1.  In the main function, 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? 

<program output>

<Your answers to the question>

*/

< Submit only the changed  program  code for question 1, in this case, the main()>

 

/*

Quest 2 ........

.......

*/

and so on.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY