1.  The following class, called road_vehicle, very broadly defines vehicles, that travel on the road. It stores the number of wheels a vehicle has and.

Slides:



Advertisements
Similar presentations
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Learners Support Publications Inheritance: Extending Classes.
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
CS 222 Object Oriented Programming Using C++ Inheritance.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
 2006 Pearson Education, Inc. All rights reserved Midterm review Introduction to Classes and Objects.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Inheritance, Polymorphism, and Virtual Functions
Chapter 6: Functions.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Polymorphism &Virtual Functions
Chapter 8 More Object Concepts
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritance Session 5 Object Oriented Programming with C++/ Session 5/ 1 of 41.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Object-Oriented Programming in C++ More examples of Association.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CONSTRUCTOR AND DESTRUCTORS
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter -6 Polymorphism
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Lecture 08. Since all Java program activity occurs within a class, we have been using classes since the start of this lecture series. A class is a template.
1 Chapter 7 INHERITANCE. 2 Outlines 7.1 Fundamentals of Inheritance 7.2 The protected Access Specifier 7.3 Constructing and Destroying Derived Classes.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
17-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Welcome to Constructors and Destructors Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 5 Classes.
Polymorphism & Virtual Functions
group work #hifiTeam
Polymorphism Lec
Introduction to Classes
CS1201: Programming Language 2
Virtual Functions Department of CSE, BUET Chapter 10.
Packages and Interfaces
Inheritance.
CONSTRUCTORS AND DESRUCTORS
Constructors and Destructors
Multiple Base Classes Inheritance
Inheritance.
Inheriting Multiple Base Classes
Presentation transcript:

1

 The following class, called road_vehicle, very broadly defines vehicles, that travel on the road. It stores the number of wheels a vehicle has and the number of passengers it can carry. class road_vehicle { int wheels; int passengers; public: void set_wheels(int num) { wheels = num; } int get_wheels() { return wheels; } void set_pass(int num) { passengers = num; } int get_pass() { return passengers; } }; 2

 You can use last broad definition of a road vehicle to help define specific types of vehicles. For example, the fragment shown here inherits road_vehicle to create a class called truck. class truck : public road_vehicle { int cargo; public: void set_cargo(int size) { cargo = size; } int get_cargo() { return cargo; } void show(); };  Because truck inherits road_vehicle, truck includes all of road_vehicle. It then adds cargo to it, along with the supporting member functions. 3

Notice how road_vehicle is inherited. The general form for inheritance is shown here: class derived-class : access base-class { body of new class } 4

 Here, access is optional. However, if present, it must be either public, private, or protected. You will learn more about these options later in this chapter. For now, all inherited classes will use public.  Using public means that all the public members of the base class will also be public members of the derived class.  Therefore, in the preceding example, members of truck have access to the public member functions of road_vehicle, just as if they had been declared inside truck.  However, truck does not have access to the private members of road_vehicle. For example, truck does not have access to wheels. 5

// Demonstrate inheritance. #include using namespace std; // Define a base class for vehicles. class road_vehicle { int wheels; int passengers; public: void set_wheels(int num) { wheels = num; } int get_wheels() { return wheels; } void set_pass(int num) { passengers = num; } int get_pass() { return passengers; } }; 6

// Define a truck. class truck : public road_vehicle { int cargo; public: void set_cargo(int size) { cargo = size; } int get_cargo() { return cargo; } void show(); }; enum type {car, van, wagon}; // Define an automoble. class automobile : public road_vehicle { enum type car_type; public: void set_type(type t) { car_type = t; } enum type get_type() { return car_type; } void show(); }; 7

void truck::show() { cout << "wheels: " << get_wheels() << "\n"; cout << "passengers: " << get_pass() << "\n"; cout << "cargo capacity in cubic feet: " << cargo << "\n"; } void automobile::show() { cout << "wheels: " << get_wheels() << "\n"; cout << "passengers: " << get_pass() << "\n"; cout << "type: "; switch(get_type()) { case van: cout << "van\n"; break; case car: cout << "car\n"; break; case wagon: cout << "wagon\n"; } 8

int main() { truck t1, t2; automobile c; t1.set_wheels(18); t1.set_pass(2); t1.set_cargo(3200); t2.set_wheels(6); t2.set_pass(3); t2.set_cargo(1200); t1.show(); cout << "\n“; t2.show(); cout << "\n"; c.set_wheels(4); c.set_pass(6); c.set_type(van); c.show(); return 0; } 9

The output from this program is shown here: wheels: 18 passengers: 2 cargo capacity in cubic feet: 3200 wheels: 6 passengers: 3 cargo capacity in cubic feet: 1200 wheels: 4 passengers: 6 type: van 10

 As this program shows, the major advantage of inheritance is that it lets you create a base class that can be incorporated into more specific classes.  In this way, each derived class can be precisely tailored to its own needs while still being part of a general classification.  One other point: Notice that both truck and automobile include a member function called show( ), which displays information about each object.  This illustrates another aspect of polymorphism. Since each show( ) is linked with its own class, the compiler can easily tell which one to call for any given object. 11

 When one class inherits another, the members of the base class become members of the derived class.  The access status of the base class members inside the derived class is determined by the access specifier used for inheriting the base class.  The base class access specifier must be public, private, or protected. If the access specifier is not used, then it is private by default if the derived class is a class.  If the derived class is a struct, then public is the default in the absence of an explicit access specifier.  Let’s examine the ramifications of using public or private access. 12

 When a base class is inherited as public, all public members of the base class become public members of the derived class.  In all cases, the private elements of the base class remain private to that class, and are not accessible by members of the derived class.  For example, in the following program, the public members of base become public members of derived. Thus, they are accessible by other parts of the program 13

#include using namespace std; class base { int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; class derived : public base { int k; public: derived(int x) { k = x; } void showk() { cout << k << "\n"; } }; int main() { derived ob(3); ob.set(1, 2); // access member of base ob.show(); // access member of base ob.showk(); // uses member of derived class return 0; } 14

 Since set( ) and show( ) are inherited as public, they can be called on an object of type derived from within main( ).  Since i and j are specified as private, they remain private to base.  The opposite of public inheritance is private inheritance.  When the base class is inherited as private, then all public members of the base class become private members of the derived class.  For example, the program shown next will not compile, because both set( )and show( ) are now private members of derived, and thus cannot be called from main( ). 15

// This program won't compile. #include using namespace std; class base { int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; // Public elements of base are private in derived. class derived : private base { int k; public: derived(int x) { k = x; } void showk() { cout << k << "\n"; } }; int main() { derived ob(3); ob.set(1, 2); // Error, can't access set() ob.show(); // Error, can't access show() return 0; } 16

 The key point to remember is that when a base class is inherited as private, public members of the base class become private members of the derived class.  This means that they are still accessible by members of the derived class, but cannot be accessed by other parts of your program. 17

 In addition to public and private, a class member can be declared as protected.  Further, a base class can be inherited as protected.  Both of these actions are accomplished by using the protected access specifier.  The protected keyword is included in C++ to provide greater flexibility for the inheritance mechanism.  When a member of a class is declared as protected, that member is not accessible to other, non-member elements of the program.  With one important exception, access to a protected member is the same as access to a private member; it can be accessed only by other members of the class of which it is a part.  The sole exception to this rule is when a protected member is inherited.  In this case, a protected member differs substantially from a private one. 18

 As you know, a private member of a base class is not accessible by any other part of your program, including any derived class.  However, protected members behave differently.  When a base class is inherited as public, protected members in the base class become protected members of the derived class, and are accessible to the derived class.  Therefore, by using protected, you can create class members that are private to their class, but that can still be inherited and accessed by a derived class. 19

 Consider this sample program: #include using namespace std; class base { protected: int i, j; // private to base, but accessible to derived public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; class derived : public base { int k; public: // derived may access base's i and j void setk() { k = i*j; } void showk() { cout << k << "\n"; } }; int main() { derived ob; ob.set(2, 3); // OK, known to derived ob.show(); // OK, known to derived ob.setk(); ob.showk(); return 0; } 20

 Here, because base is inherited by derived as public, and because i and j are declared as protected, derived’s function setk( ) may access them.  If i and j were declared as private by base, then derived would not have access to them, and the program would not compile.  REMEMBER: The protected specifier allows you to create a class member that is accessible within a class hierarchy, but is otherwise private. 21

 When a derived class is used as a base class for another derived class, then any protected member of the initial base class that is inherited (as public) by the first derived class can be inherited again, as a protected member, by a second derived class.  For example, the following program is correct, and derived2 does, indeed, have access to i and j: 22

#include using namespace std; class base { protected: int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; // i and j inherited as protected. class derived1 : public base { int k; public: void setk() { k = i*j; } // legal void showk() { cout << k << "\n"; } }; // i and j inherited indirectly through derived1. class derived2 : public derived1 { int m; public: void setm() { m = i-j; } // legal void showm() { cout << m << "\n"; } }; 23

int main() { derived1 ob1; derived2 ob2; ob1.set(2, 3); ob1.show(); ob1.setk(); ob1.showk(); ob2.set(3, 4); ob2.show(); ob2.setk(); ob2.setm(); ob2.showk(); ob2.showm(); return 0; } 24

 When a base class is inherited as private, protected members of the base class become private members of the derived class.  Therefore, in the preceding example, if base were inherited as private, then all members of base would become private members of derived1, meaning that they would not be accessible to derived2.  (However, i and j would still be accessible to derived1.)  This situation is illustrated by the following program, which is in error (and won’t compile). The comments describe each error. 25

// This program won't compile. #include using namespace std; class base { protected: int i, j; public: void set(int a, int b) { i = a; j = b; } void show() { cout << i << " " << j << "\n"; } }; // Now, all elements of base are private in derived1. class derived1 : private base { int k; public: // This is legal because i and j are private to derived1. void setk() { k = i*j; } // OK void showk() { cout << k << "\n"; } }; 26

// Access to i, j, set(), and show() not inherited. class derived2 : public derived1 { int m; public: // Illegal because i and j are private to derived1. void setm() { m = i-j; } // error void showm() { cout << m << "\n"; } }; int main() { derived1 ob1; derived2 ob2; ob1.set(1, 2); // Error, can't use set() ob1.show(); // Error, can't use show() ob2.set(3, 4); // Error, can't use set() ob2.show(); // Error, can't use show() return 0; } 27

 Even though base is inherited as private by derived1, derived1 still has access to the public and protected elements of base.  However, it cannot pass this privilege along. This is the reason that protected is part of the C++ language.  It provides a means of protecting certain members from being modified by non-member functions, but allows them to be inherited.  The protected specifier can also be used with structures. It cannot be used with a union, however, because a union cannot inherit another class or be inherited.  (Some compilers will accept its use in a union declaration, but because unions cannot participate in inheritance, protected is the same as private in this context.) 28

 The protected access specifier may occur anywhere in a class declaration, although typically it occurs after the (default) private members are declared, and before the public members.  Thus, the most common full form of a class declaration is class class-name { private members protected: protected members public: public members }; 29

 In addition to specifying protected status for members of a class, the keyword protected can also be used to inherit a base class.  When a base class is inherited as protected, all public and protected members of the base class become protected members of the derived class. Here is an example: 30

// Demonstrate inheriting a protected base class. #include using namespace std; class base { int i; protected: int j; public: int k; void seti(int a) { i = a; } int geti() { return i; } }; // Inherit base as protected. class derived : protected base { public: void setj(int a) { j = a; } // j is protected here void setk(int a) { k = a; } // k is also protected int getj() { return j; } int getk() { return k; } }; 31

int main() { derived ob; /* This next line is illegal because seti() is a protected member of derived, which makes it inaccessible outside of derived. */ // ob.seti(10); // cout << ob.geti(); // illegal -- geti() is protected // ob.k = 10; // also illegal because k is protected // these next statements are OK ob.setk(10); cout << ob.getk() << ' '; ob.setj(12); cout << ob.getj() << ' '; return 0; } 32

 As you can see by reading the comments in last program, k, j, seti( ), and geti( ) in base become protected members of derived.  This means that they cannot be accessed by code outside of derived.  Thus, inside main( ), references to these members through ob are illegal 33

 Because the access rights as defined by public, protected, and private are fundamental to C++ programming, let’s review their meanings: 1. When a class member is declared as public, it can be accessed by any other part of a program. 2. When a member is declared as private, it can be accessed only by members of its class. 3. Further, derived classes do not have access to private base class members. 4. When a member is declared as protected, it can be accessed only by members of its class, or by derived classes. 5. Thus, protected allows a member to be inherited, but to remain private within a class hierarchy. 34

 When a base class is inherited by use of public, its public members become public members of the derived class, and its protected members become protected members of the derived class.  When a base class is inherited by use of protected, its public and protected members become protected members of the derived class.  When a base class is inherited by use of private, its public and protected members become private members of the derived class. 35

 In all cases, private members of a base class remain private to the base class, and are not inherited.  As you become more familiar with C++, the meaning of public, protected, and  private will become second nature.  For now, if you are unsure what precise effect an access specifier has, write a short sample program as an experiment and observe the results. 36

 It is possible for a derived class to inherit two or more base classes.  For example, in this short program, derived inherits both base1 and base2: // An example of multiple base classes. #include using namespace std; class base1 { protected: int x; public: void showx() { cout << x << "\n"; } }; class base2 { protected: int y; public: void showy() { cout << y << "\n"; } }; // Inherit multiple base classes. class derived: public base1, public base2 { public: void set(int i, int j) { x = i; y = j; } }; 37

int main() { derived ob; ob.set(10, 20); // provided by derived ob.showx(); // from base1 ob.showy(); // from base2 return 0; }  As this example illustrates, to cause more than one base class to be inherited, you must use a comma- separated list.  Further, be sure to use an access specifier for each base class inherited. 38

 There are two important questions that arise relative to constructors and destructors when inheritance is involved.  First, when are base class and derived class constructors and destructors called?  Second, how can parameters be passed to a base class constructor? This section answers these questions. 39

 When Constructors and Destructors Are Executed It is possible for a base class, a derived class, or both, to contain a constructor and/or destructor.  It is important to understand the order in which these are executed when an object of a derived class comes into existence and when it goes out of existence.  Examine this short program: 40

#include using namespace std; class base { public: base() { cout << "Constructing base\n"; } ~base() { cout << "Destructing base\n"; } }; class derived: public base { public: derived() { cout << "Constructing derived\n"; } ~derived() { cout << "Destructing derived\n"; } }; int main() { derived ob; // do nothing but construct and destruct ob return 0; } 41

 As the comment in main( ) indicates, this program simply constructs and then destroys an object called ob, which is of class derived.  When executed, this program displays: Constructing base Constructing derived Destructing derived Destructing base  As you can see, the constructor of base is executed, followed by the constructor of derived. Next (since ob is immediately destroyed in this program), the destructor of derived is called, followed by that of base. The results of the foregoing experiment can be generalized as follows: ◦ When an object of a derived class is created, the base class constructor is called first, followed by the constructor for the derived class. ◦ When a derived object is destroyed, its destructor is called first, followed by the destructor for the base class. ◦ Put differently, constructors are executed in the order of their derivation. ◦ Destructors are executed in reverse order of derivation.. 42

 If you think about it, it makes sense that constructor functions are executed in the order of their derivation.  Because a base class has no knowledge of any derived class, any initialization it needs to perform is separate from, and possibly prerequisite to, any initialization performed by the derived class.  Therefore, it must be executed first.  Likewise, it is quite sensible that destructors be executed in reverse order of derivation.  Since the base class underlies a derived class, the destruction of the base class implies the destruction of the derived class.  Therefore, the derived destructor must be called before the object is fully destroyed. 43

 In the case of a large class hierarchy (i.e., where a derived class becomes the base class for another derived class), the general rule applies:  Constructors are called in order of derivation, destructors in reverse order.  For example, this program 44

#include using namespace std; class base { public: base() { cout << "Constructing base\n"; } ~base() { cout << "Destructing base\n"; } }; class derived1 : public base { public: derived1() { cout << "Constructing derived1\n"; } ~derived1() { cout << "Destructing derived1\n"; } }; class derived2: public derived1 { public: derived2() { cout << "Constructing derived2\n"; } ~derived2() { cout << "Destructing derived2\n"; } }; int main() { derived2 ob; // construct and destruct ob return 0; } displays this output: Constructing base Constructing derived1 Constructing derived2 Destructing derived2 Destructing derived1 Destructing base 45

 The same general rule applies in situations involving multiple base classes. For example, this program: #include using namespace std; class base1 { public: base1() { cout << "Constructing base1\n"; } ~base1() { cout << "Destructing base1\n"; } }; class base2 { public: base2() { cout << "Constructing base2\n"; } ~base2() { cout << "Destructing base2\n"; } } ; class derived: public base1, public base2 { public: derived() { cout << "Constructing derived\n"; } ~derived() { cout << "Destructing derived\n"; } }; 46

int main() { derived ob; // construct and destruct ob return 0; } produces this output: Constructing base1 Constructing base2 Constructing derived Destructing derived Destructing base2 Destructing base1  As you can see, constructors are called in order of derivation, left to right, as specified in derived’s inheritance list. Destructors are called in reverse order, right to left. This means that if base2 were specified before base1 in derived’s list, as shown here: class derived: public base2, public base1 { then the output of the preceding program would look like this: Constructing base2 Constructing base1 Constructing derived Destructing derived Destructing base1 Destructing base2 47

 So far, none of the preceding examples have included constructors requiring arguments. In cases where only the constructor of the derived class requires one or more arguments, you simply use the standard parameterized constructor syntax.  But how do you pass arguments to a constructor in a base class? The answer is to use an expanded form of the derived class’ constructor declaration, which passes arguments along to one or more base class constructors. The general form of this expanded declaration is shown here: derived-constructor(arg-list) : base1(arg-list), base2(arg-list),... baseN(arg-list); { body of derived constructor } 48

 Here, base1 through baseN are the names of the base classes inherited by the derived class.  Notice that a colon separates the constructor declaration of the derived class from the base classes, and that the base classes are separated from each other by commas, in the case of multiple base classes. Consider this sample program: #include using namespace std; class base { protected: int i; public: base(int x) { i = x; cout << "Constructing base\n"; } ~base() { cout << "Destructing base\n"; } }; class derived: public base { int j; public: // derived uses x; y is passed along to base. derived(int x, int y): base(y) { j = x; cout << "Constructing derived\n"; } ~derived() { cout << "Destructing derived\n"; } void show() { cout << i << " " << j << "\n"; } }; 49

int main() { derived ob(3, 4); ob.show(); // displays 4 3 return 0; }  Here, derived’s constructor is declared as taking two parameters, x and y. However, derived( ) uses only x; y is passed along to base( ).  In general, the constructor of the derived class must declare the parameter(s) that its class requires, as well as any required by the base class.  As the preceding example illustrates, any parameters required by the base class are passed to it in the base class’ argument list, specified after the colon. 50

51