Computers and Technology
Computers and Technology, 01.07.2021 16:00, rilee3344

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
?

answer
Answers: 3

Other questions on the subject: Computers and Technology

image
Computers and Technology, 21.06.2019 16:30, jonnys412
Margins can be modified in the page layout tab or by using
Answers: 2
image
Computers and Technology, 22.06.2019 01:30, yudayang2012pa9u8p
Consider the following statements: #include #include class temporary { private: string description; double first; double second; public: temporary(string = "", double = 0.0, double = 0.0); void set(string, double, double); double manipulate(); void get(string& , double& , double& ); void setdescription(string); void setfirst(double); void setsecond(double); }; write the definition of the member function set() so that the instance variables are set according to the parameters. write the definition of the constructor so that it initializes the instance variables using the function set() write the definition of the member function manipulate() that returns a decimal number (double) as follows: if the value of description is "rectangle", it returns first * second if the value of description is "circle" it returns the area of a circle with radius first if the value of description is "cylinder" it returns the volume of a cylinder with radius first and height second. hint: the volume of a cylinder is simply the area of the circle at the base times the height. if the value of description is "sphere" it returns the volume of the sphere with radius first. otherwise it returns -1.0;
Answers: 1
image
Computers and Technology, 23.06.2019 10:00, lamanihill
Now, open this passage to read about fafsa requirements. describe the information you will need to provide in order to complete a fafsa. list at least three of the required documents you must include.
Answers: 3
image
Computers and Technology, 24.06.2019 00:30, petergriffin6772
Which boolean operator enables you to exclude a search term? a} not b} and c} or d} plus
Answers: 1
Do you know the correct answer?
Save the following C++ program into a file classes. cpp #include using namespace std;
class A...

Questions in other subjects:

Konu
Mathematics, 04.05.2021 08:00