1 / 17

Which of the following constructor is a copy constructor?

Which of the following constructor is a copy constructor?. class ABC { public: ABC(); ABC(ABC * other); ABC(ABC other); ABC( const ABC & other); };. Which of the following constructor is a copy constructor?. class ABC { public: ABC(); ABC(ABC * other); ABC(ABC other);

jerry-cook
Download Presentation

Which of the following constructor is a copy constructor?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Which of the following constructor is a copy constructor? • class ABC • { • public: • ABC(); • ABC(ABC * other); • ABC(ABC other); • ABC(const ABC & other); • };

  2. Which of the following constructor is a copy constructor? • class ABC • { • public: • ABC(); • ABC(ABC * other); • ABC(ABC other); • ABC(const ABC & other); • }; Copy constructor must have one of the following form MyClass( constMyClass& other ); MyClass( MyClass& other );

  3. What could be the output of the following code? • class ABC • { • public: • ABC(intin_a=0, intin_b=0) • { a = in_a; • b = new int; • *b = in_b; • } • ~ABC() • { delete b; } • intget_a() {return a;} • intget_b() {return *b;} • private: • int a, *b; • }; • void main() • { • ABC val1(1, 2); • ABC val2 = val1; • cout << “val1.a=“ << val1.get_a() • << “, val1.b=“ << val1.get_b() << endl; • cout<< “val2.a=“ << val2.get_a() • << “, val2.b=“ << val2.get_b() << endl; • }

  4. How to fix this? • class ABC • { • public: • ABC(intin_a=0, intin_b=0) • { a = in_a; • b = new int; • *b = in_b; • } • ~ABC() • { delete b; } • intget_a() {return a;} • intget_b() {return *b;} • private: • int a, *b; • }; • void main() • { • ABC val1(1, 2); • ABC val2 = val1; • cout << “val1.a=“ << val1.get_a() • << “, val1.b=“ << val1.get_b() << endl; • cout<< “val2.a=“ << val2.get_a() • << “, val2.b=“ << val2.get_b() << endl; • } val1.a=1, val1.b=2 val2.a=1, val2.b=2 The program crushes!

  5. How to fix this? • class ABC • { • public: • ABC(intin_a=0, intin_b=0) • { a = in_a; • b = new int; • *b = in_b; • } • ABC (ABC & in) // specify your copy constructor • { a = in.a; • b = new int; • *b = in.b; • } • ~ABC() • { delete b; } • intget_a() {return a;} • intget_b() {return *b;} • private: • int a, *b; • }; • … val1.a=1, val1.b=2 val2.a=1, val2.b=2

  6. Polymorphism and virtual functions

  7. Polymorphism is related to inheritance. • class Base • { • public: • Base(intin_x=0) • { x = in_x;} • ~Base(bool flag){} • private: • int x; • }; • class Derived : public Base • { • public: • Derived(intin_x, intin_y) • : x(in_x), y(in_y) • { • } • private: • int y; • }; what is wrong in the left code?

  8. Polymorphism is related to inheritance. • class Base • { • public: • Base(intin_x=0) • { x = in_x;} • ~Base(bool flag){} • private: • int x; • }; • class Derived : public Base • { • public: • Derived(intin_x, intin_y) • : x(in_x), y(in_y) • { • } • private: • int y; • }; destructor does not have argument list! what is wrong in the left code? should call the Base constructor!

  9. Polymorphism is related to inheritance. • class Base • { • public: • Base(intin_x=0) • { x = in_x;} • private: • int x; • }; • class Derived : public Base • { • public: • Derived(intin_x, intin_y) • : Base(in_x), y(in_y) • { • } • private: • int y; • }; Which of the following assignments are legal? Derived d_obj1 (1,2); Base b_obj1; d_obj1 = b_obj1; Base *b_pt1 = &d_obj1;

  10. Polymorphism is related to inheritance. • class Base • { • public: • Base(intin_x=0) • { x = in_x;} • private: • int x; • }; • class Derived : public Base • { • public: • Derived(intin_x, intin_y) • : Base(in_x), y(in_y) • { • } • private: • int y; • }; Which of the following assignments are legal? Derived d_obj1 (1,2); Base b_obj1; d_obj1 = b_obj1; //illegal! Base *b_pt1 = &d_obj1; //legal

  11. A take home question • class Base • { • public: • Base(intin_x=0, char *in=NULL) • { x = in_x; • str = new char[strlen(in)+1]; • strcpy (str, in); • } • ~Base(){delete [] str; } • private: • int x; • char *str; • }; • class Derived : public Base • { • public: • Derived(intin_x, char *in, intin_y) • : Base(in_x, in), y(in_y) • { • } • private: • int y; • }; what is the issue of the following assignment? how to fix it? Derived d_obj1 (1, ”test”, 2); Base b_obj1 = d_obj1;

  12. What will be the output of the following code? class Thing { public: void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing { public: void what_Am_I( ) {cout << "I am an Animal." << endl;} }; void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (inti=0; i<2; i++) pts[i]->what_Am_I( ); return ; }

  13. What about the following code? class Thing { public: virtual void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing { public: void what_Am_I( ) {cout << "I am an Animal." << endl;} }; void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (inti=0; i<2; i++) pts[i]->what_Am_I( ) ; return ; }

  14. What about the following code? class Thing { public: virtual void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing { public: void what_Am_I( ) {cout << "I am an Animal." << endl;} }; void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (inti=0; i<2; i++) pts[i]->what_Am_I( ) ; return ; } What is the difference? Why it generates different output?

  15. What about the following code? class Thing { public: virtual void what_Am_I( ) {cout << "I am a Thing." << endl;} }; class Animal : public Thing { public: void what_Am_I( ) {cout << "I am an Animal." << endl;} }; void main( ) { Thing t ; Animal x ; Thing* pts[2]; // base pointer pts[0] = &t; pts[1] = &x; for (inti=0; i<2; i++) pts[i]->what_Am_I( ) ; return ; } What is the difference? Why it generates different output? Here we define the overwritten function “void what_Am_I()”as a virtual function, so that the compiler will use “late/dynamic binding” to determine which function to call DURING RUN TIME based on the type of the object.

  16. What benefits do we gain from virtual functions and polymorphism? Software reuse, easy maintenance, because ……

  17. What benefits do we gain from virtual functions and polymorphism? Software reuse, easy maintenance, because …… void wrapper(Base *b_pt, …) { // call the virtual functions } We can access all the derived classes of the Base class if we want to use their newer and more specific functionality without worrying about the change of interface or writing more functions.

More Related