Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.

Similar presentations


Presentation on theme: "Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors."— Presentation transcript:

1 Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors

2 Definition Inheritance –A form of software reusability in which new classes are created from existing classes by absorbing their attributes and behaviors, and overriding or embellishing these with capabilities the new classes require Software reusability saves time in program development by reusing proven and debugged high-quality software

3 Inheritance What do we get from our parents? –Attributes & behaviors What is a class composed of? –Data members (attributes) –Member functions (behaviors) A class can “inherit” the data members & member functions from its parent class –Parent class (base class) – general –Child class (derived class) – specific

4 Inheritance –Form of software reuse –Built upon the work of others (other classes) Base class (parent) –The original class –Other classes will be built using this as a starting point

5 Inheritance Derived class (child) –A new class constructed from information present in the base class –A derived class is more specific than its base class & represents a smaller group of objects A class can be both a base class and a derived class –B (derived class) inherits from A (base class) –C (derived class) inherits from B (base class)

6 Relationships View inheritance as an “is-a” relationship The derived class “is-a” specialized/extended object of the base class Inheritance is not a “has-a” relationship –“has-a” relationships deal with composition A person “has-a” a brain A room “has-a” door A Fraction “has-a” numerator and denominator A Complex Number “has-a” real and imaginary part

7 Relationships View inheritance as an “is-a” relationship –A mammal “is-an” animal –A person “is-a” mammal –A square “is-a” shape –A lab “is-a” room –A Mixed Number “is-a” Fraction

8 Member Access Control Public Access –Any public data member of the base class can be “seen” by a derived class Private Access –Any private data member of the base class cannot be “seen” by a derived class Problem –Need public so derived class can see data members of base class –Need private for data encapsulation data encapsulation (information hiding) – hiding the details of how a class works by restricting access to data members, which can only be manipulated by member functions

9 Protected Access Can access protected data members & member functions of a base class –This section can be seen by a derived class with the “is-a” property –This section cannot be seen by a class with a “has-a” property

10 Data Encapsulation Also called “data hiding”, or “information hiding” The implementation details of a class are hidden within the classes themselves Classes must interact with one another through well-defined interfaces –Hiding the details, while supplying an interface is an important underlying theme behind classes, or user-defined data types An interface of a class is a list of function prototypes, so the user does not have access to the body (code) of the member functions

11 Problems with Inheritance The derived class can inherit public member function implementations that it does not need to have –To correct this potential problem, a programmer can override the function with another function which has the appropriate implementation Also note that friend functions are not inherited

12 Basic Format class BaseClass{ protected: //data members public: //member functions }; class DerivedClass: public BaseClass{ //extra data & functions };

13 Example Program class Fraction{ protected: //data members public: //member functions }; class MixedNumber: public Fraction{ //extra data & functions }; //see inheritance.cpp

14 Overriding vs. Overloading Overriding – functions in the base class and the derived class (different classes) with the same name and same parameters –When this function is mentioned by name in the derived class, the derived-class version is automatically selected Overloading – functions in the same class with the same name and different parameters

15 Constructors & Destructors Derived class constructor always calls the constructor of its base class first –If no constructor exists for derived class, the default constructor is called Destructors are reversed - derived class destructor is called before the base class destructor –If no destructor exists for derived class, the default destructor is called

16 Software Engineering Inheritance can be used to customize existing software –A derived class inherits the attributes & behaviors of a base class, and then additional attributes & behaviors are added –In C++, you only need the base class object code and the base class header file A software vender can develop classes for sale –If the user has the object code & header files, then can create new software to suit own needs –The vender’s source code is never revealed


Download ppt "Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors."

Similar presentations


Ads by Google