Download presentation
Presentation is loading. Please wait.
Published byMeredith Hines Modified over 6 years ago
1
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design, structure and reusability of code.
2
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.
3
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.
4
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.
5
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.
6
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.
7
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.
8
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.
9
Inheritance The derived class inherits the attributes of classes above it in the class hierarchy The specialization can continue over multiple levels
10
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
11
class Child : public Parent { }; - protected inheritance
Base Class Access C++ supports three inheritance modes, also called base class access modes: - public inheritance class Child : public Parent { }; - protected inheritance class Child : protected Parent{ }; - private inheritance class Child : private Parent{ };
12
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
13
Member Access Specification
Specified using the keywords private, protected, public class MyClass { private: int a; protected: int b; void fun(); public: void fun2(); };
14
Base Class Access Specification
class Child : public Parent { protected: int a; public: Child(); }; base access member access
15
Base Class Access Specifiers
public – object of derived class can be treated as object of base class (not vice-versa) protected – more restrictive than public, but allows derived classes to know some of the details of parents private – prevents objects of derived class from being treated as objects of base class.
16
Inheritance Base class access specifier Type of inheritance public
protected private 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 friend functions. Can be accessed directly by member functions and friend functions. private in derived class. Can be accessed directly by member functions and friend functions. Hidden in derived class. Can be accessed by member functions and friend functions through public or protected member functions of the base class. Can be accessed by member functions and friend functions through public or protected member functions of the base class. Can be accessed by member functions and friend functions through public or protected member functions of the base class.
17
appear in derived class
Effect of Base Access How base class members appear in derived class Base class members public base class private: x protected: y public: z x inaccessible protected: y public: z protected base class private: x protected: y public: z x inaccessible protected: y protected: z private: x protected: y public: z private base class x inaccessible private: y private: z
18
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 See pr11-20.cpp
19
Order of Execution int main() { UnderGrad u1; ... return 0;
// Student – base class // UnderGrad – derived class // Both have constructors, destructors int main() { UnderGrad u1; ... return 0; }// end main Execute Student constructor, then execute UnderGrad constructor Execute UnderGrad destructor, then execute Student destructor
20
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
21
Passing Arguments to Base Class Constructor
class Parent { public: Parent(int,int); private: int x, y; }; class Child : public Parent { Child(int a): Parent(a,a*a){ z = a; } int z; See inheritance2.h, inheritance2.cpp, pr11-21App.cpp
22
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 See inheritance3.h, inheritance3.cpp, pr11-21.cpp
23
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(); See inheritance4.h, inheritance4.cpp, and Pr11-22App.cpp
24
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
25
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;
26
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 See inheritance4.h and pr15-01.cpp
27
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.