Inheritance

21
April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV 1 Inheritance A class can be derived from an existing class by using the following form: class class-name:(public|protected| private) base-name { member declarations }; The keywords public, protected, and private are used to specify how the base-class members can be accessed by the derived class. Public derivation is far more important than private or protected derivation; it should be considered the normal form of inheritance.

description

Inheritance. A class can be derived from an existing class by using the following form: class class-name :(public|protected|private) base-name { member declarations }; - PowerPoint PPT Presentation

Transcript of Inheritance

Page 1: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

1

InheritanceA class can be derived from an existing class by using the following form:

class class-name:(public|protected|private) base-name{

member declarations};

The keywords public, protected, and private are used to specify how the base-class members can be accessed by the derived class.Public derivation is far more important than private or protected derivation; it should be considered the normal form of inheritance.

Page 2: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

2

InheritanceExample:class Student{public:

enum year {fresh, soph, junior, senior, grad};Student(string nm, int id, double g, year y);void Print() const;

protected:string name;int student_id;double gpa;year yr;

};

Page 3: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

3

Inheritanceclass GradStudent : public Student{public:

enum support {ta, ra, fellowship, other};GradStudent(string nm, int id, double g, year y,

support s, string d, string th);void Print() const;

protected:support supp;string dept;string thesis;

};

Page 4: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

4

InheritanceIn the preceding example, GradStudent is the derived class, and Student is the base class.Using the keyword public following the colon in the derived-class header has several effects:• The protected and public members of Student are inherited as protected and public members, resp., of GradStudent.• Private members are inaccessible.• GradStudent is a subtype of Student.• A GraduateStudent is a Student, but a Student does not have to be a GraduateStudent (is-a relationship,

or interface inheritance).

Page 5: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

5

Inheritance• A derived class is a modification of the base class,

inheriting the public and protected members of the base class.

• Notice: Only constructors, destructors, and the member function operator=() cannot be inherited.

• Frequently, a derived class adds new members to the existing class members.

• It is also possible to override existing class members.

• Notice: Overriding is different from overloading, in which the same function name can have different meanings for each unique signature.

Page 6: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

6

Inheritance

Benefits of using a derived class:

• Code is reused: GradStudent uses existing, tested code from Student.

• The hierarchy reflects a relationship found in the problem domain. In the real world, graduate students make up a subgroup of all students.

• Various polymorphic mechanisms will allow client code to treat GradStudent as a subtype of Student, which simplifies the code but keeps distinctions among subtypes.

Page 7: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

7

Typing Conversions and Visibility

A variable of a publicly derived class can in many ways be treated as if it were the base-class type.For example, a pointer whose type is pointer to base class can point to objects that have the derived-class type.To examine the properties of base classes and derived classes, let us first take a closer look at our examples Student and GradStudent.

Page 8: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

8

Typing Conversions and VisibilityImplementation of constructors:

Student::Student(string nm, int id, double g, year y) :name(nm), student_id(id), gpa(g), yr(y)

{}

GradStudent::GradStudent(string nm, int id, double g, year y, support s, string d, string th) : Student(nm, id, g, y), supp(s), dept(d), thesis(th)

{}

Notice: The constructor of Student is invoked as part of the initializer list in the constructor of GradStudent.

Page 9: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

9

Typing Conversions and Visibility

Because GradStudent is a subtype of Student, a reference to the derived class GradStudent may be implicitly converted to a reference to the public base class Student.

Example:

GradStudent gst(“John Miller”, 31416, 3.99, grad, ra, “Computer Science”, “Eye Movements in the

Dark”);Student &st = gst;

Page 10: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

10

Inheritanceclass GradStudent : public Student { … };

Using the keyword public following the colon in the derived-class header has several effects:• The protected and public members of Student are inherited as protected and public members, resp., of GradStudent.• Private members are inaccessible.• GradStudent is a subtype of Student.• A GraduateStudent is a Student, but a Student does not have to be a GraduateStudent (is-a relationship,

or interface inheritance).

Page 11: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

11

Inheritanceclass GradStudent : private Student { … };Using the keyword private following the colon in the derived-class header has several effects:• The protected and public members of Student are inherited as private members of GradStudent.• However, GradStudent can re-declare protected members of Student as protected…• …and public members of Student as either public or

protected. • Private members of Student are inaccessible by GradStudent.• GradStudent is not a subtype of Student.

Page 12: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

12

Inheritanceclass GradStudent : protected Student { … };Using the keyword protected following the colon in the derived-class header has several effects:• The public and protected members of Student are inherited as protected members of GradStudent.• However, GradStudent can re-declare public members of Student as public.• Private members of Student are inaccessible by GradStudent.• GradStudent is not a subtype of Student.

Page 13: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

13

Typing Conversions and VisibilityExample for pointer conversions:int main(){

Student s(“Joe Smith”, 111, 2.57, student::fresh);Student *ps = &s;GradStudent gs(“John Miller”, 31416, 3.99, student::grad, ra, “Computer Science”, “Eye Movements in the Dark”);GradStudent *pgs;ps->Print();ps = pgs = &gs;pgs->Print();ps->Print();

}

// Student::Print()

// GradStudent::Print()// Student::Print()

Page 14: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

14

Virtual Functions• Overloaded member functions are invoked by a

type-matching algorithm.• These types are known at compile time and allow

the compiler to select the appropriate member directly.

• As you will see, it would be nice to dynamically select at runtime the appropriate member function from among base- and derived-class functions.

• Such a mechanism is provided by the keyword virtual; it may be used only to modify member function declarations.

• Virtual functions combined with public inheritance are a form of pure polymorphism.

Page 15: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

15

Virtual Functions• When a virtual function is invoked, its semantics are

the same as those of other functions.• In a derived class, a virtual function can be

overridden by a another function with a matching signature.

• The selection of which function definition to invoke for a virtual function is dynamic.

• A pointer to base class can point at either a base-class object or a derived-class object.

• The member function selected will depend on the class of the object being pointed at, not on the pointer type.

Page 16: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

16

Virtual Functions• Note the difference in selection of the appropriate

overridden virtual function from an overloaded member function:

•An overloaded member function is selected at compile time, based on its argument types, and it can have distinct return types.

•A virtual function is selected at runtime, based on the object’s type, which is passed to it as its this pointer argument.

• Once a function is declared virtual, this property is automatically carried along to all redefinitions in derived classes.

Page 17: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

17

Virtual FunctionsExample: Virtual function selection

class B{public: int i; virtual void Print() const { cout << i << “ inside B” << endl; }};

class D : public B{ void Print() const { cout << i << “ inside D “ << endl; }};

int main(){ B b; B *pb = &b; D d;

d.i = 1 + (b.i = 1); pb->Print(); pb = &d; pb->Print();}

Output:

1 inside B2 inside D

Page 18: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

18

Virtual Functions• In the Student/GradStudent example, the selection

of Print() is based on the pointer type, known at compile time.

• In the current (B/D) example, selection is based on what is being pointed at.

• Here, the pointer’s base type is not determining the function selection.

• Instead, different class objects are processed by different functions, determined at runtime.

Page 19: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

19

Virtual FunctionsExample:class Shape{public: virtual double Area() const { return 0; }protected: double x, y;};

class Rectangle : public Shape{public: double Area() const { return (height*width); }private: double height, width;};

class Circle : public Shape{public: double Area() const { return (PI*radius*radius); }private: double radius;};

int main(){ … Shape *s[N]; … for (int i = 0; i < N; i++) totArea += s[i]->Area();}

Page 20: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

20

Virtual FunctionsNotice:• The declaration of an identifier in a scope hides all

declarations of that identifier in outer scopes.• A base class is an outer scope of any class derived

from it.• This rule is independent of whether the names are

declared virtual or not.• If the selected function is inaccessible, we get a

compile-time error.

Page 21: Inheritance

April 8, 2014 CS410 – Software Engineering Lecture #15: Advanced C++ IV

21

Virtual FunctionsWe can define abstract base classes that include pure virtual functions (also called deferred methods).These are declared as follows:virtual function prototype = 0;An abstract base class specifies the basic common properties of its derived classes but cannot itself be used to declare objects.It is used to declare pointers that can access subtype pointers derived from the abstract class.