1 / 16

Today's Topics

Today's Topics. All C++ today. Constructor initialization Inheritance Virtual functions Overriding vs. hiding. Constructor Initialization. the default constructor will be called for “owner” even if it is not explicitly listed. This assignment stmt happens after all the initialization.

alima
Download Presentation

Today's Topics

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. Today's Topics • All C++ today. • Constructor initialization • Inheritance • Virtual functions • Overriding vs. hiding CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  2. Constructor Initialization the default constructor will be called for “owner” even if it is not explicitly listed This assignment stmt happens after all the initialization • Order of initialization: • name = n • weight = 0 • owner public class Pet { string name; int weight; Person owner; } Pet::Pet(string n) : owner(), name(n) { weight = 50; … } CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  3. Constructor Initialization • Sometimes initialization must be done in the initialization list: Person has no default constructor, so we can’t let owner() be called; breed is a constant string, so it cannot occur on the left hand side of an assignment statement veterinarian is a reference type, so also cannot occur on the left hand side of an assignment statement class Person { string name; Person(string n) { name = n; } } class Pet { Person owner; const string breed; Person& veterinarian; } Pet::Pet(Person ownr, string brd, Person vet) : owner(ownr), breed(brd), veterinarian(&vet) { … } CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  4. C++ Inheritance • Syntax is different from Java: • class class-name: public base-name • { • member declarations • }; • There is also private and protected derivation, rarely used; unfortunately, private is the default. CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  5. C++ Inheritance • Example: • class Student • { • public: • Student(string nm, int id); • void Print(); • protected: • string name; • int student_id; • }; CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  6. C++ Inheritance • class GradStudent : public Student • { • public: • GradStudent(string nm, int id, string th); • void Print(); • protected: • string thesis; • }; CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  7. C++ Inheritance • GradStudent is the derived class. • Student is the base class. • Public derivation has these characteristics: • protected and public members of Student are inherited as protected and public members of GradStudent. • Only constructors, destructors, and the member function operator=() cannot be inherited. • Private members of Student are inaccessible. • GradStudent is a subtype of Student (is-a relationship. CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  8. Inheritance and Constructors Note: The constructor of Student is invoked as part of the initializer list in the constructor of GradStudent. • Implementation of constructors: • Student::Student(string nm, int id) • : name(nm), student_id(id) {} • GradStudent::GradStudent(string nm, int id, string th) • : Student(nm, id), thesis(th) {} CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  9. Pointer Type Conversions • A reference to the derived class can be implicitly converted to a reference to the public base class. • Example: • GradStudent gst(“John Smith”, 31416, • “Verification of Network Protocols”); • Student &st = gst; CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  10. Visibility • Print() member functions for Student and GradStudent: • void Student::Print() const • { • cout << name << “ , “ << student_id << endl; • } • void GradStudent::Print() const • { • Student::Print(); • cout << thesis << endl; • } CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  11. Pointer Types and Visibility • Example for pointer conversions: • Student s(“Joe Smith”, 111); • Student *ps = &s; • GradStudent gs(“John Smith”, 31416, • “Verification of Network Protocols”); • GradStudent *pgs; • ps->Print(); • ps = pgs = &gs; • pgs->Print(); • ps->Print(); • // Student::Print() • // GradStudent::Print() • // Student::Print() CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  12. Visibility, continued illegal • Visibility is determined at compile-time: • class Pet {…}; • class Dog : public Pet { • virtual void dig(); • } • … • int main() { • Pet *ptrPet = new Dog(); • ptrPet->dig(); • } dig is not defined for class Pet, only for subclass Dog, and ptrPet is of type pointer-to-Pet CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  13. Hiding versus Overriding • class Student { • public: • void Print() const; • virtual void Vprint() const; • year yr; //year is enum type • … • }; • class GradStudent : • public Student { • public: • void Print() const; • virtual void Vprint() const; • int yr; • … • } • int main() { • GradStudent gs= • new GradStudent(); • Student *pstudent = &gs; • pstudent->Print(); • pstudent->Vprint(); • pstudent->yr = 2006; • pstudent->yr = senior; • } Student::Print() GradStudent::Vprint() Illegal Legal Print() and yr are hidden in GradStudent Vprint() is overridden in GradStudent CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  14. Overloading versus overriding class Diffident { public: virtual void mumble(int softness); … }; class Shy : public Diffident { public: virtual void mumble(string whatYaSay); … }; … Shy simon; simon.mumble(“pardon me”); simon.mumble(2); Ok Illegal-- int arg function is hidden CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  15. Multiple Inheritance • In C++, a class can inherit from two or more base classes: ZooAnimal class ZooAnimal {..}; class Endangered {…}; class Bear :public ZooAnimal {…}; class Panda :public Bear, public Endangered {…}; Bear Endangered Panda CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

  16. Multiple Inheritance: Iostream virtual inheritance Because istream, ostream inherit virtually, iostream has only one copy of ios • Multiple inheritance occurs with the standard iostream library. ios istream ostream ifstream iostream ofstream fstream CS410 – Software Engineering C++ Initialization, Inheritance and Virtual Functions

More Related