Vous êtes sur la page 1sur 19

Inheritance

QF002/10/1

UNIT 10

INHERITANCE

OBJECTIVES

General Objective

: To understand and apply the principles of

Inheritance in programming.

Specific Objective : At the end of the unit you should be able to:

 Describe the inheritance in a program.  Apply the inheritance in a program.  Use the protected in a program  Write and design a simple program using the inheritance data.

Inheritance

QF002/10/2

INPUT

10.0

Introduction To Inheritance

 Inheritance is the ability to define a new, specialized class from an existing class. The original class is called the parent or base class. A specialized class which inherits the data structure and methods of its parent, is a derived class or subclass.  Inheritance in C++ means deriving or forming one or more specific classes from a general class. The general class is called the base, parent, ancestor or superclass while the specific classes are called derived, child, descendent or subclass.
Inheritance is the ability to define an object as a descendant of an existing object.

Inheritance

QF002/10/3

For example, we may declare the base class mammal and then use to declare the subclass cat and donkey. This is illustrated in figure 10.1. This is an example of one-level inheritance.

Mammal

Donkey

Cat

Figure 10.1 : An Example One- Level Inheritance  Inheritance can take several level, for example, we can declare the base class person and then derive the subclass lecture and student. Then, use the lecture class to derive the subclass local lecturer and foreign lecturer and the student class to derive the subclass local student and foreign student as shown in figure 10.2. This is an example of a two-level inheritance.

Inheritance

QF002/10/4

person

lecturer

student

foreign lecturer

local lecturer

foreign student

local student

Figure 10.2 : An example of Two- Level Inheritances

Inheritance

QF002/10/5

10.1

Declaring a derived class

Lets consider the class shown in figure 10. 3. The class has two data members ( no_pend and name) and two member functions (open_data and show_data ).

The data members are declared private while the function members are declared public.
class data { private: int on_pend; char name(20); public: void open_data; void show_data; }

If you want to create the subclass data from the base class data, you will not be able to inherit the no_pend and name from data class because they are declared private.

Figure 10.3 : An Example Of Declaring A Derived Class


Private members of a class cannot be accessed by any function that is external to the class.

Public members can be accessed by all functions irrespective of whether they are inside or outside the class.

Inheritance

QF002/10/6

When a class serves as a base class to others, all of its members are inherited except for any private member. While this is a desirable feature for encapsulation and data hiding, it can nevertheless be a problem when it comes to class inheritance.

10.2

Protected

 

This access specifier allows a derived class to inherit the member of the parent class. Figure 10.4 shows the declaration of protected.
class data { protected: int on_pend; char name(20); public: void open_data; void show_data; }

Figure 10.4 :An Example Of Declaring A Protected

If you now create the subclass data from the base class data, it will inherit the variable no_pend and name as well as the function open_data and show_data

Inheritance

QF002/10/7

To create a subclass, you include the following elements in the table 10.1 : a. b. c. d. e. f. g. h. i. The keyword class The derived class name A colon A class specifier The base class name An opening breaket The class definition statements A closing bracket A semicolon.

Table 10.1 : An Example Of Declaring A Protected

Inheritance is the ability to define a new, specialized class from an existing class. The original class is called the parent or base class. A specialized class which inherits the data structure and methods of its parent, is a derived class or subclass.

Inheritance

QF002/10/8

Figure 10.5 illustrates the protected. It shows the base class


data1 and the subclass data2 derived from the base class.
//base class class data1 { protected: int on_pend1; char name1(20); public: void open_data1; void show_data1; }; // a derived class class data2 :: public account { private: int on_pend2; char name2(20); public: void open_data2; void show_data2; }

Figure 10.5 An Illustration Of The Protected

Each class has its own variables and function. Because data2 is derived from data1, therefore data2 will inherit all the variable and the functions (except the constructor) of its parent class (data1)

The data1 class in this program is the base class and the data2 class is the subclass (derived from the data1 class). It has two variables ( no_pend1 and name1).

C++

Inheritance

QF002/10/9

10.3 Simple Example Of A Subclass.


#include <iostream.h> #include <string.h> // base class class data1 { protected: int no_pend; char name[15]; public: data1(); void open_data1(const int acct_no, const char*); void show_data1(); }; // derived from data1 class class data2:public data1 { private: double total; public: data2(); void test(const double bal); void show_total(); }; //constructor data1 :: data1() { no_pend = 0; strcpy (name, " "); } void data1 ::open_data1 (const int acct_no, const char *nameptr) { no_pend = acct_no; strcpy (name, nameptr); }

continue

Inheritance

QF002/10/10

void { cout cout cout cout }

data1::show_data1() <<"\n From data1 class"; <<"\n Data1 number: " <<no_pend; <<"\n Nama : " <<name; << endl;

// constructor data2::data2() { total = 0.00; } void data2::test(const double bal) { total = total + bal; } void { cout cout cout cout } void main() { float y,z; int x; char name1[30]; data2 cur; cout<< "what is your data1 no? No_Pend:"; cin>>x; cout<< " \n what is your name? "; cin>>name1; cur.open_data1(x, name1); //from base class cur.show_data1(); //from base class cout<< " \n\n\n Your test1 is % = "; cin>>y; data2::show_total() <<"\n <<"\n <<"\n <<"\n From data2 class"; Data2 no: " <<no_pend; Name: "<<name; Your total is : " <<total<<"%";

continue

Inheritance

QF002/10/11

cur.test(y); //from data2 class cur.show_total(); //from data2 class cout<< " \n\n\n Your test2 is % = "; cin>>z; cur.test(z); cur.show_total(); cout<<" \n --------"; cout<<" \n 200"; }

Figure 10.5 : An Example Of A Subclass Program

Figure 10.6 : An Example Of Output Program Of A Subclass

Inheritance

QF002/10/12

Activity 10

Test your comprehension before continuing the next input. Check your answers on the next page.

10.1. Can inherited members and functions passed along to subsequent generations? If Donkey derives from Mammal and Mammal derives from Animal, does Donkey inherit Animal's functions and data?

10.2. If, in the example above, Mammal overrides a function in Animal, what does Donkey get, the original or the overridden function?

10.3. Can a derived class make a public base function pri vate?

10.4. Why not make all class functions virtual?

Inheritance

QF002/10/13

Feedback 10

Make sure you have tried to answer all the questions given. You can check your answers with the answers below.

10.1.

Yes. As derivation continues, derived classes inherit the sum of all the functions and data in all their base classes.

10.2. If Donkey inherits from Mammal, it gets the same function as mammal. That is call as a overridden function.

10.3. Yes, and it remains private for all subsequent derivation.

10.4. There is overhead with the first virtual function in the creation of a v table. After that, the overhead is trivial. Many C++ programmers feel that if one function is virtual, all others should also be. Other programmers disagreed, feeling that there should always be a reason for what you do.

Inheritance

QF002/10/14

Key Facts

Inheritance in C++ means deriving or forming one or more

specific classes from a general class.

Public members can be accessed by all functions irrespective

of whether they are inside or outside the class .

Private members of a class cannot be accessed by any function

that is external to the class .

Inheritance

QF002/10/15

Self-Assessment

You are approaching success, please answer the questions below. If you have any problems, please discuss it with your lecturer. Wish you good luck and all the best.

Question 10 1

a. Show the declaration of a derived class as the data members are declared private while the function members are declared public.

b. Show the declaration of a class Square, which derives from Rectangle which in turn derives from Shape.

c. Show the declaration of a derived class as the data members are declared protected while the function members are declared public.

Question 10 2

Write a declaration of protected to create the subclass data from the


base class data(student) which will inherit the variable no_pend

and name as well as the function open_data and show_data

Inheritance

QF002/10/16

b.

BUG BUSTERS: Identify the error in this code snippet?


class data1 { public: int no_pend; char name[15]; protected: data1(); void open_data1(const int acct_no,const char*); void show_data1(); };

c.

Identify the error this code snippet?

class data2:public data1 { private: double total; public: data2; void test; void show_total; };

Inheritance

QF002/10/17

Feedback On Self-Assessment

Make sure you have tried to answer all the quest ions given. You can check your answers with the answers below. Answer 10 1 a.
class data { private: int on_pend; char name(20); public: void open_data; void show_data; }

b.

// base class class shape { protected: int data1; public: data1();

Shape
}; // derived from shape class class rectangle:public shape { private: double data2; public: data2(); }; // derived from rectangle class class square:public rectangle { private: double data3; public: data3(); };

Rectangle

Square

Inheritance

QF002/10/18

c.
class data { protected: int on_pend; char name(20); public: void open_data; void show_data; }

Answer 10 - 2

a.
class student { protected: int on_pend; char name(20); public: void open_data; void show_data; }

b.

Data members (no_pend and name) must class data1 declare in protected and two member { functions (open_data and show_data) public: declared in public int no_pend; char name[15]; protected: data1(); void open_data1(const int acct_no,const char*) ; void show_data1(); };

Inheritance

QF002/10/19

c.
class data2:public data1 { private: double total; public: data2; void test; void show_total; };

Member functions (data2, test and show_total) must declare in function type data2() void test(); void show_total();

CONGRATULATION May success be with you always..

Vous aimerez peut-être aussi