Presentation is loading. Please wait.

Presentation is loading. Please wait.

1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived.

Similar presentations


Presentation on theme: "1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived."— Presentation transcript:

1 1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived class? 4.What are the differences of the qualifiers (public, protected, private) when derive from a base class? class Person //base class { protected: string name; }; class Imployee1: public Person { string getName(){return name;} … }; class Imployee2: protected Person { string getName(){return name;} … }; class Imployee3: private Person { string getName(){return name;} … };

2 5. How does a derived class’s constructor call base class’s constructor? 6. How about destructors? 7. In what order are the constructor called? 8. In what order are the destructors called? 9. What is member function redefinition in class inheritance? 10. What is “name hiding” in function redefinition? 11. What are the differences between the redefined functions and virtual functions? class Shape //base class { public: virtual void setPosition(int x, int y) { position = Point(x, y); } }; class Rectangle: public Shape { public: virtual void setPosition(int x, int y) { position = Point(x*2, y*2); } }; int main() { Rectangle r; Shape &s = r; s.setPosition(2, 4); cout << r.getArea() << endl; //output? cout << s.getArea() << endl; //output? return 0; }

3 12. Late binding and early binding 13. Virtual Destructor. 14. What is the output of the following program? class A { ~A() { count << "A destructor\n" } }; class B: public A { virtual ~B() { count << “B destructor\n" } }; class C: public B { ~C() { count << “C destructor\n" } }; class D: public C { virtual ~D() { count << “D destructor\n" } }; int main() { C cClass; A * rBase = &cClass; delete rBase; return 0; }

4 15. How to declare a pure virtual function? 16. How to create an object of a class that include a pure virtual function? 17. What are the potential problems of multiple inheritance? 18. How to declare a template function? 19. How to declare a template class? 20. How to define the member functions of a template class? 21. Write a template function based on the swap function below. void swap(char& var1, char& var2) { char temp; temp = var1; var1 = var2; }

5 22. STL Vector. What are size and capacity of a vector class? 23. How to set capacity of a vector? 24. What happens if the size reaches the capacity of the vector? 25. What does the following code do? … vector v; vector ::iterator it; for (it = v.begin(); it != v.end(); v++) { *it = 0; } …

6 #include using namespace std; // function name: findName // find the target string in the input list // and returns the iterator of the list element list ::iterator& findName(list input, string target); int main() { list nameList; nameList.push_back("John"); nameList.push_back("Jack"); list ::iterator& it = findName(nameList, "John"); } //define findName function here 26. Implement a function described in the following code 27. Open a file-stream. Read a file, “readme.txt” from the directory “textfiles” under current directory. Finally, close the filestream.


Download ppt "1.What are the differences between composition(“has a” relationship) and inheritance(“is a” relationship)? 2.What is a base class? 3.How to declare a derived."

Similar presentations


Ads by Google