Give reasonably brief, but complete answers to the following. 4. Given the follo
ID: 3917961 • Letter: G
Question
Give reasonably brief, but complete answers to the following.
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
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 {...}
---here operator + declared..which is wrong initialization
b)
Clock Clock::operator++(int) {...}
this is valid...if parametres are passed that are matched to clock....like int / double...
c) c. Menu Menu::operator++(const Menu& a, const Menu& b) {...}
---here operator ++ declared..which is wrong initialization. for menu we must give ** instead of ++
d)
Menu Menu::operator**(const Menu& a) const {...}
---explained in above statement
(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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.