Presentation is loading. Please wait.

Presentation is loading. Please wait.

LOGO Lecturer: Abdullahi Salad Abdi May 1, 2015 1 Object Oriented Programming in C++

Similar presentations


Presentation on theme: "LOGO Lecturer: Abdullahi Salad Abdi May 1, 2015 1 Object Oriented Programming in C++"— Presentation transcript:

1 LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++

2 LOGO Chapter 9 Inheritance & Polymorphism Class: BIT02

3 LOGO Objectives  Inheritance  categories of Inheritance  Protected Access Specifier  Types of Inheritance Public Private Protected  Derived Class Constructors  Multiple Inheritance 3

4 LOGO Inheritance  Inheritance is the second most important feature of the Object Oriented Programming. In inheritance, the code of existing classes is used for making new classes. This saves time for writing and debugging the entire code for a new class. 4

5 LOGO Cont….  To inherit means to receive. In inheritance a new class is written such that it can access or use the members of an existing class.  The new class that can access the members of an existing class is called the derived class or child class. The existing class is called the base class or parent class. 5

6 LOGO Defining a Class  The derived class can use the data members and member functions of the base class.  It can also have its own data members and member functions. Thus a derived class can be larger than a base class. 6

7 LOGO Figure 7 Member 1 Member 2 Member 1 Member 3

8 LOGO Cont…  The figure shows the relationship between a derived class and the base class. The arrow is drawn from the derived class to the base class.  The direction of the arrow indicates that the derived class can access members of the base class but the base class cannot access member of its derived class. 8

9 LOGO Cont…  The idea of inheritance implements the is a relationship.  For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on. 9

10 LOGO Types of Inheritance 1.Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class. 10

11 LOGO Real life example All Cars are Vehicles. All Motorcycles are Vehicles. All Sedans are Vehicles. Vehicles is the base class. Car is a derived class. Truck derives from Vehicle. Everything about being a Vehicle is inherited by Car s and Truck s and SUV s.

12 LOGO Types of Inheritance 2. Multiple Inheritance: It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es) 12

13 LOGO Types of Inheritance 3. Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherit from one base class. 13

14 LOGO Types of Inheritance 4. Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes. 14

15 LOGO Types of Inheritance 5. Hybrid Inheritance: The inheritance hierarchy that reflects any legal combination of other four types of inheritance. 15

16 LOGO Protected Access Specifier  The public members of a class are accessible by all functions in the program and the private members of a class are accessible only by member functions and friend functions of that class.  Similarly, the protected members of a class are accessible by the member functions and friend functions of that class. 16

17 LOGO Cont….  The protected members of a base class are, however, accessible by members of its derived classes  but the private members of the base class are not accessible directly by members of its derived classes.This is the main difference between the protected and the private access specifiers. 17

18 LOGO Cont…  The protected members of a base class fall between private and public member. These members are public for the derived class but for the rest of the program, these are treated as private. 18

19 LOGO Defining Derived Classes  The syntax for defining a derived class is highly different from the syntax of the base class definition. The declaration of a derived class also includes the name of the base class from which it is derived. 19

20 LOGO Syntax Class sub_class_name : specifier base_class_name { Members of derived class }; 20

21 LOGO Example  A class “student” is defined as Class student { Private: Char name[15], address[15]; Public: Void input(void) Void show(void) }; 21

22 LOGO Example  The class student has two data members and to member functions.  Suppose the marks obtained by a student in three subjects and the total marks of these subjects are to be included as new data members in the above class. This is done by adding new members in the class. There are two ways in which these new members can be added to the class. These are: 22

23 LOGO Example  Add new member in the original class OR  Define a new class that has the new members and that also uses members of the existing “student” class. Using the members of an existing class is the principle of inheritance.  The new class is the derived class. The existing class serves as the base class for the derived class. 23

24 LOGO Example  Driving a new class from the existing class reduces the size of the program. It also eliminates duplication of code within the program.  For example, let the name of the new class be marks. This class uses the members of the existing student class. 24

25 LOGO Cont… Class marks : public student { Private: Int s1,s2,s3,total; Public: Void inputmarks(void) Void show_details(void) }; 25 Example

26 LOGO Types of Inheritance  There are three kinds of inheritance  Public  Private  Protected 26

27 LOGO Public inheritance  In Publilc Inheritance, the public members of the base class become the public members of the derived class. Thus the objects of the derived class can access public members (both data and functions) of the base class. Similarly, the protected data members of the base class also become the protected members of derived class. 27

28 LOGO Public inheritance Syntax  The general syntax for deriving a public class from base class is: Class sub_class_name : public base_class_name { }; 28 Example

29 LOGO Private Inheritance  In private inheritance, the objects of the derived class cannot access the public members of the base class. Its objects can only access the protected data members of the base class. Class sub_class_name : private base_class_name { }; 29 Example

30 LOGO Protected Inheritance  The object of the base class that is derived as protected can access only the protected member of the base class. Class sub_class_name : protected base_class_name { }; 30

31 LOGO Protected Inheritance Where  Protectedspecifies the protected inheritance  Sub_class_namerepresents name of the derived class  Base_class_namerepresents name of the base class 31 Example

32 LOGO 32

33 LOGO Derived Class Constructor  In inheritance, a constructor of the derived class as well the constructor functions of the base class are automatically executed when an object of the derived class is created.  The following example explains the concept of execution of constructor functions in single inheritance. 33 Example

34 LOGO Constructor in Single Inheritance with Arguments  In case of constructor functions with arguments, the syntax to define constructor of the derived class is different. To execute a constructor of the base class that has arguments through the derived class constructor is defined as:  Write the name of the constructor function of the derived class with parameters  Pace a colon immediately after this and then write the name of the constructor function of thee base class with parameters 34 Example

35 LOGO Multiple Inheritance  In multiple inheritance, a class is derived by using more than one classes. In multiple inheritance the derived class receives the members of two or more base classes.  Multiple inheritance is a powerful feature of Object Oriented Programming. This technique reduces the program size and saves programmer’s time. The programmer uses the existing base classes in the program coding to solve problems. 35

36 LOGO Multiple Inheritance  The figure sown below illustrates the relation of derived class with base class. 36 Base Class A Base Class B Derived Class C

37 LOGO Syntax  The syntax of multiple inheritance is similar to that single inheritance class. The names of base classes are written separated by commas (,).  The general Syntax is: 37

38 LOGO Syntax Class sub_class: sp1 b_class_1, sp2 bclass_2, …….. { Members of derived class }; Where:  sp1specifies the access specifier of the first base class  Sp2specifies the access specifier of the second base class 38

39 LOGO Syntax Where:  sp1specifies the access specifier of the first base class  Sp2specifies the access specifier of the second base class 39 Example

40 LOGO Constructors in Multiple Inheritance  When a class is derived from more than one base classes, the constructor of the derived class as well as of all its base classes are executed when an object of the derived class is created.  If constructor function have no parameters than first the constructor functions of the base classes are executed and then the constructor functions of the derived class is executed. 40 Example

41 LOGO Constructors in Multiple Inheritance with Arguments  To execute constructors of base classes that have arguments through the derived constructor functions, the derived class constructor is defined as:  Write a constructor function of the derived class with parameters  Place a colon (:) immediately after this and then write the constructor function names of base classes with parameters separated by commas. 41 Example

42 LOGO Cont…


Download ppt "LOGO Lecturer: Abdullahi Salad Abdi May 1, 2015 1 Object Oriented Programming in C++"

Similar presentations


Ads by Google