csci 104
play

CSCI 104 Inheritance Mark Redekopp David Kempe Sandra Batista 2 - PowerPoint PPT Presentation

1 CSCI 104 Inheritance Mark Redekopp David Kempe Sandra Batista 2 Recall: Constructor Initialization Student::Student() : Student::Student() name(), id(), scores() { // calls to default constructors name = "Tommy Trojan"; {


  1. 1 CSCI 104 Inheritance Mark Redekopp David Kempe Sandra Batista

  2. 2 Recall: Constructor Initialization Student::Student() : Student::Student() name(), id(), scores() { // calls to default constructors name = "Tommy Trojan"; { id = 12313 name = "Tommy Trojan"; scores.resize(10); id = 12313 } scores.resize(10); } But any member not in the initialization list will You can still assign data members in the {…} have its default constructor invoked before the {…} • You can still assign values in the constructor but realize that the default constructors will have been called already • So generally if you know what value you want to assign a data member it's good practice to do it in the initialization list Student::Student() : name("Tommy"), id(12313), scores(10) { } This would be the preferred approach especially for any non-scalar members (i.e. an object)

  3. 3 INHERITANCE

  4. 4 Object Oriented Design Components • Encapsulation – Combine data and operations on that data into a single unit and only expose a desired public interface and prevent modification/alteration of the implementation • Inheritance – Creating new objects (classes) from existing ones to specify functional relationships and extend behavior • Polymorphism – Using the same expression to support different types with different behavior for each type

  5. 5 Inheritance • A way of defining interfaces, re-using classes and extending original functionality • Allows a new class to inherit all the data members and member functions from a previously defined class • Works from more general objects to more specific objects – Defines an "is-a" relationship – Square is-a rectangle is-a shape Parent/ Base – Square inherits from Rectangle which Child / inherits from Shape Derived – Similar to classification of organisms: Grandchild • Animal -> Vertebrate -> Mammals -> Primates

  6. 6 Base and Derived Classes • Derived classes inherit class Person { public: Person(string n, int ident); all data members and string get_name(); int get_id(); private: functions of base class string name_; int id_; }; class Student : public Person { • Student class inherits: public: Student(string n, int ident, int mjr); int get_major(); – get_name() and double get_gpa(); void set_gpa(double new_gpa); get_id() private: int major_; double gpa_; – name_ and id_ member }; variables class Person class Student string name_ string name_ int id_ int id_ int major_ double gpa_

  7. 7 Base and Derived Classes • Derived classes inherit class Person { public: Person(string n, int ident); all data members and string get_name(); int get_id(); private: functions of base class string name_; int id_; }; • Student class inherits: class Student : public Person { public: Student(string n, int ident, int mjr); int get_major(); – get_name() and get_id() double get_gpa(); void set_gpa(double new_gpa); – name_ and id_ member private: int major_; double gpa_; variables }; int main() { Student s1("Tommy", 1, 9); class Person class Student // Student has Person functionality // as if it was written as part of string name_ string name_ // Student cout << s1.get_name() << endl; int id_ int id_ int major_ } double gpa_

  8. 8 Inheritance Example Inheritance Diagrams (arrows show derived Component to base class • Component relationships) – Draw() Window ListBox – onClick() • Window – Minimize() ScrollBox DropDown – Maximize() Box • ListBox – Get_Selection() • ScrollBox – onScroll() • DropDownBox – onDropDown()

  9. 9 CONSTRUCTORS AND INHERITANCE

  10. 10 Constructors and Inheritance • How do we initialize base class Person { public: Person(string n, int ident); class data members? ... private: • Can't assign base class string name_; int id_; members if they are private }; class Student : public Person { public: Student(string n, int ident, int mjr); ... private: int major_; double gpa_; }; Student::Student(string n, int ident, int mjr) { name_ = n; // can we access name_ and id_? id_ = ident; major_ = mjr; }

  11. 11 Constructors and Inheritance class Person { • Constructors are only called when public: Person(string n, int ident); a variable is created and cannot ... be called directly from another private: string name_; constructor int id_; }; – How to deal with base class Student : public Person { constructors? public: Student(string n, int ident, int mjr); • Also want/need base class or ... private: other members to be initialized int major_; double gpa_; before we perform this object's }; constructor code Student::Student(string n, int ident, int mjr) { • Use initializer format instead // How to initialize Base class members? Person(n, ident); // No! can’t call Construc. – See example below // as a function } Student::Student(string n, int ident, int mjr) : Person(n, ident) { cout << "Constructing student: " << name_ << endl; major_ = mjr; gpa_ = 0.0; }

  12. 12 Constructors & Destructors • Constructors – A Derived class will automatically call its Base class base constructor BEFORE it's own constructor executes, (1) either: child • Explicitly calling a specified base class constructor in the (2) initialization list grandchild (3) • Implicitly calling the default base class constructor if no base class constructor is called in the initialization list Constructor call ordering • Destructors – The derived class will call the Base class destructor automatically AFTER it's own destructor executes • General idea base – Constructors get called from base->derived (smaller to (3) larger) child – Destructors get called from derived->base (larger to (2) grandchild smaller) (1) Destructor call ordering

  13. 13 Constructor & Destructor Ordering class A { int main() int a; { public: cout << "Allocating a B object" << endl; A() { a=0; cout << "A:" << a << endl; } B b1; ~A() { cout << "~A" << endl; } cout << "Allocating 1st C object" << endl; A(int mya) { a = mya; C* c1 = new C; cout << "A:" << a << endl; } cout << "Allocating 2nd C object" << endl; }; C c2(4,5); cout << "Deleting c1 object" << endl; class B : public A { delete c1; int b; cout << "Quitting" << endl; public: return 0; Test Program B() { b = 0; cout << "B:" << b << endl; } } ~B() { cout << "~B "; } B(int myb) { b = myb; Allocating a B object A:0 cout << "B:" << b << endl; } B:0 }; Allocating 1st C object A:0 class C : public B { int c; B:0 C:0 public: Allocating 2nd C object C() { c = 0; cout << "C:" << c << endl; } ~C() { cout << "~C "; } A:0 B:4 C(int myb, int myc) : B(myb) { c = myc; C:5 Deleting c1 object cout << "C:" << c << endl; } ~C ~B ~A }; Quitting ~C ~B ~A Output ~B ~A Sample Classes

  14. 14 PUBLIC, PRIVATE, PROTECTED

  15. 15 Protected Members • Private members of a base class Person { public: class can not be accessed ... private : directly by a derived class string name_; int id_; }; member function class Student : public Person { public: – Code for print_grade_report() void print_grade_report(); would not compile since ‘name_’ is private: int major_; double gpa_; private to class Person }; • Base class can declare void Student::print_grade_report() { variables with protected X cout << “Student “ << name_ << ... } storage class which means: – Private to any object or code not class Person { inheriting from the base (i.e. public: private to any 3 rd party) ... protected: – Public to any derived (child) class string name_; int id_; can access directly };

  16. 16 Public, Protected, & Private Access 1. Private Base Members • Derived class sees base class 3 rd party class members using the base class' Base Class or function private : specification // members X – If Base class said it was public or protected , the derived class can access it directly Derived Class – If Base class said it was private , the derived Regardless of public, protected, private inheritance class cannot access it directly 2. Protected Base Members 3 . Public Base Members 3 rd party class 3 rd party class Base Class Base Class or function or function public : protected : ✓ X // members // members ✓ Derived Class Derived Class Regardless of public, protected, Regardless of public, protected, private inheritance private inheritance

Recommend


More recommend