1. For a class with several stl::string data members (like Card in the project),
ID: 665771 • Letter: 1
Question
1. For a class with several stl::string data members (like Card in the project), why can C++ copy objects of the class without an explicitly written copy constructor in the class? -------------------------------------------------------------------- 2. C++ allows constructor overloading. Why doesn't it allow destructor overloading? -------------------------------------------------------------------- 3. When enabling a class to work with iostreams by adding overloaded operator<< and operator>> functions, should they be friends of the class or member functions, and why? -------------------------------------------------------------------- 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? -------------------------------------------------------------------- 7. When a function has an object parameter that it doesn't modify, what is the best way to declare the parameter (in the function signature), and why? Show an example using a function with a string object parameter. -------------------------------------------------------------------- 8. (a) When should a member function be declared as a 'const' member function? (b) What advantage is there to defining a member function as 'const'? -------------------------------------------------------------------- 9. a) When a class has objects as data members, why should its constructors use initialization lists (member and initializer syntax) vs. assignment to the members in the body of the ctor? b) Name two cases where you *must* use initialization lists in constructors (as opposed to assignment in the ctor body). -------------------------------------------------------------------- 10. a) Suppose class Y is derived from class X. Each class declares a constructor and a destructor. Class Y has a string member object that has a constructor and destructor. In what order do the three constructors and destructors run when instances of class Y are created and destroyed? b) Should X's destructor be declared virtual? Why or why not, and what difference would it make? -------------------------------------------------------------------- 11. What is a pure virtual function? Why would you define a pure virtual function? Give an example of a pure virtual function. -------------------------------------------------------------------- 12. What is an abstract base class? What does it do? Why would you make a class abstract? -------------------------------------------------------------------- 13. Why does the overloaded assignment operator need to check for assignment to self (especially in a class that holds pointers to dynamic resources)? -------------------------------------------------------------------- 14. Explain the three main concepts represented in the Standard Template Library (STL), what their purpose is, and how they work together. -------------------------------------------------------------------- 15. (a) What are the advantages of using an stl::string to hold character data vs. a dynamic char array?
Explanation / Answer
This question has lot of subparts. Please post 3-4 more questions for the same.
1. C++ does not allow copy of the class objects as every class has an allocated memory and it won't allow this.
2. Destructor cannot be overloaded as in C++ there is nothing to call destructor. It is automatically called by the C++ frame work when the object is deleted/goes out of scope. So basically it cannot be overloaded.
3. Yes it can be done with the help of friend function. You can also use this parameter in the left hand side of the <<-operator for doing the same.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.