240 likes | 555 Views
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);
E N D
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); • ABC(const ABC & other); • }; Copy constructor must have one of the following form MyClass( constMyClass& other ); MyClass( MyClass& other );
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; • }
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!
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
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?
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!
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;
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
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;
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 ; }
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 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?
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.
What benefits do we gain from virtual functions and polymorphism? Software reuse, easy maintenance, because ……
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.