Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

4. Given the following code: struct Circle { int radius; }; struct Cylinder : pu

ID: 3760587 • Letter: 4

Question

4. Given the following code:

struct Circle {

    int radius;

};

struct Cylinder : public Circle {

    int height;

};

Circle *cirptr, cir;

Cylinder *cylptr, cyl;

Which of the following statements are legal?

cirptr = &cyl;

cylptr = ○

cirptr = cylptr;

cylptr = cirptr;

--------------------------------------------------------------------

5. Which of the following operator functions are legal?

   For those not legal, indicate why not.

a. Complex Complex::operator+(const Complex& a) const {...}

b. Clock Clock::operator++(int) {...}

c. Menu Menu::operator++(const Menu& a, const Menu& b) {...}

d. Menu Menu::operator**(const Menu& a) const {...}

--------------------------------------------------------------------

6. a) What behavior can occur when overloaded conversion operators

   and/or single argument constructors are defined for a class?

  

   b) What can you do in the class design to eliminate the problem

   for single argument constructors?

  

   c) Assuming the class needs to provide conversion capabilities,

   what can you do in the class design to eliminate the problem

   for conversion operators?

--------------------------------------------------------------------

Explanation / Answer


4.
cirptr = &cyl; //legal, because base pointer can point to derived class object
cylptr = ○ //legal because same type of pointer can point to same data type
cirptr = cylptr; //legal, because base pointer can point to derived class object
cylptr = cirptr; //not legal, because derived class pointer cannot point to base class
  
5.
a. Complex Complex::operator+(const Complex& a) const {...}
   //legal

b. Clock Clock::operator++(int) {...}
   //legal

c. Menu Menu::operator++(const Menu& a, const Menu& b) {...}
//not legal, because two arguments should not be passed

d. Menu Menu::operator**(const Menu& a) const {...}
//legal
  
6.
a) Overloaded conversion operators basically act as a function but constructors just initialize the value

b) Use default value in arguments

c) implement the function instead of overloaded operator.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote