inheritance modern object-oriented (oo) programming ...rlowe/cs1070/notes/inheritance.pdf ·...

27
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design, structure and reusability of code.

Upload: others

Post on 21-Jun-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Inheritance

Modern object-oriented (OO) programming languages provide 3 capabilities:

• encapsulation

• inheritance

• polymorphism

which can improve the design, structure and reusability of code.

Page 2: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Encapsulation

• the bundling of an object’s data and procedures into a single unit.

• all of the object's data is contained and hidden in the object and access to it is restricted to members of the class.

• Examples

– A function encapsulates the details of an algorithm

– A class encapsulates both variables and functions together in a single unit.

Page 3: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Encapsulation

• C++ supports encapsulation and data hiding with user-defined types called classes.

• A class combines data and functions/methods into a single unit.

• The method of hiding details of a class is called abstraction. Classes can contain private, protected and public members.

Page 4: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Encapsulation

• Although all the items in a class are private by default, programmers can change the access levels when needed. Three levels of access are available in both C++ and C# and an additional two in C# only. They are:

– Public: All objects can access the data.

– Protected: Access is limited to members of the same class or descendants.

– Private: Access is limited to members of the same class.

Page 5: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Encapsulation

Advantage of Encapsulation

– security of the data.

• Benefits of encapsulation:

– protection of objects from unwanted access by clients.

– allows access to a level without revealing the complex details below that level.

– reduces human errors.

– simplifies the maintenance of the application

– makes the application easier to understand.

Page 6: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Encapsulation

• For the best encapsulation, object data should almost always be restricted to private or protected.

• If you choose to set the access level to public, make sure you understand the ramifications of the choice.

Page 7: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Inheritance

• the capability to derive a new class from an existing class.

• The initial class used as the basis for the derived class is referred to as either the base, parent or superclass.

• The derived class is referred to as either the derived, child, or subclass.

• The derived class inherits all the member variables and functions (except constructors and destructors) of its base class.

Page 8: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Inheritance

C++ class hierarchy is based upon the principle of increased specialization.

• The base class carries attributes that are common to all classes and virtual functions that may or may not be overridden.

• The derived class adds its own additional new data and member functions/methods.

• A function in the derived class may override a base class function.

Page 9: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Inheritance

• The derived class inherits the attributes of classes above it in the class hierarchy

• The specialization can continue over multiple levels

Page 10: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Inheritance

Protection attributes: Base class member access specifiers

• public - Any holder of a pointer to an instance of the class may invoke any public method or modify any public data item.

• private – methods/data members may be accessed only by methods belonging to the class

• protected - methods/data members may be access by derived classes but not others

Page 11: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Base Class Access

C++ supports three inheritance modes, also called base class access modes:

- public inheritanceclass Child : public Parent { };

- protected inheritanceclass Child : protected Parent{ };

- private inheritanceclass Child : private Parent{ };

Page 12: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Base Class Access vs. Member Access Specification

Base class access is not the same as member access specification:

– Base class access: determines access for inherited members

– Member access specification: determines access for members defined in the class

11-12

Page 13: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Member Access Specification

Specified using the keywords

private, protected, public

class MyClass

{

private: int a;

protected: int b; void fun();

public: void fun2();

};

11-13

Page 14: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Base Class Access Specification

class Child : public Parent{

protected:

int a;

public:

Child();

};member access

base access

Page 15: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Base Class Access Specifiers

1) public – object of derived class can be

treated as object of base class (not vice-versa)

2) protected – more restrictive than public,

but allows derived classes to know some of the details of parents

3) private – prevents objects of derived class

from being treated as objects of base class.

Page 16: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Inheritance

Base class access specifier

Type of inheritance

public protected private

public public in derived class.

Can be accessed directly by member functions, friend functions, and nonmember functions.

protected in derived class.

Can be accessed directly by member functions and friend functions.

private in derived class.Can be accessed directly by member functions and friendfunctions.

protected protected in derived class.

Can be accessed directly by member functions and friend functions.

protected in derived class.

Can be accessed directly by member functions and friend functions.

private in derived class.

Can be accessed directly by member functions and friend functions.

private Hidden in derived class.

Can be accessed by member functions and friend functions through public or protected member functions of the base class.

Hidden in derived class.

Can be accessed bymember functions and friend functions through public or protected member functions of the base class.

Hidden in derived class.

Can be accessed bymember functions and friend functions through public or protected member functions of the base class.

Page 17: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Effect of Base Access

private: x

protected: y

public: z

private: x

protected: y

public: z

private: x

protected: y

public: z

Base class members

x inaccessible

private: y

private: z

x inaccessible

protected: y

protected: z

x inaccessible

protected: y

public: z

How base class membersappear in derived class

privatebase class

protectedbase class

publicbase class

Page 18: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Order of Execution

• When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor

• When an object of a derived class is destroyed, its destructor is called first, then that of the base class

Page 19: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Order of Execution

// Student – base class

// UnderGrad – derived class

// Both have constructors, destructors

int main()

{

UnderGrad u1;

...

return 0;

}// end main

Execute Studentconstructor, then

execute UnderGradconstructor

Execute UnderGraddestructor, then

execute Studentdestructor

Page 20: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Passing Arguments to Base Class Constructor

• Allows selection between multiple base class constructors

• Specify arguments to base constructor on derived constructor heading

• Can also be done with inline constructors

• Must be done if base class has no default constructor

Page 21: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Passing Arguments to Base Class Constructor

class Parent {public:

Parent(int,int);

private:

int x, y;

};class Child : public Parent {

public: Child(int a): Parent(a,a*a){

z = a;}

private:int z;

};

Page 22: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Overriding Base Class Functions

• Overriding function: function in a derived class that has the same name and parameter list as a function in the base class

• Typically used to replace a function in base class with different actions in derived class

• Not the same as overloading – with overloading, the parameter lists must be different

Page 23: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Access to Overridden Function

• When a function is overridden, all objects of derived class use the overriding function.

• If necessary to access the overridden version of the function, it can be done using the scope resolution operator with the name of the base class and the name of the function:

Student::getName();

Page 24: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Type Compatibility in Inheritance Hierarchies

• Classes in a program

may be part of an

inheritance hierarchy

• Classes lower in the

hierarchy are special

cases of those above

Vehicle

Car Truck

18-Wheeler

Page 25: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Type Compatibility in Inheritance

• A pointer to a derived class can be assigned to a pointer to a base class. Another way to say this is:

• A base class pointer can point to derived class objects

Vehicle *vehPtr = new Car;

Page 26: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Type Compatibility in Inheritance

• Assigning a base class pointer to a derived class pointer requires a cast

Vehicle *carPtr = new Car;

Car *carPtr;

carPtr = static_cast<Car *>(vehPtr);

• The base class pointer must already point to a derived class object for this to work

Page 27: Inheritance Modern object-oriented (OO) programming ...rlowe/cs1070/notes/inheritance.pdf · Encapsulation •C++ supports encapsulation and data hiding with user-defined types called

Using Type Casts with Base Class Pointers

• C++ uses the declared type of a pointer to determine access to the members of the pointed-to object

• If an object of a derived class is pointed to by a base class pointer, all members of the derived class may not be accessible

• Type cast the base class pointer to the derived class (via static_cast) in order to access

members that are specific to the derived class

15-27