Inheritance.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Chapter 14 Inheritance Pages ( ) 1. Inheritance: ☼ Inheritance and composition are meaningful ways to relate two or more classes. ☼ Inheritance.
Inheritance (2).
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
EC-241 Object-Oriented Programming
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
Win32 Programming Lesson 4: Classes and Structures.
Learners Support Publications Inheritance: Extending Classes.
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.
1 Review: Two Programming Paradigms Structural (Procedural) Object-Oriented PROGRAM PROGRAM FUNCTION OBJECT Operations Data OBJECT Operations Data OBJECT.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
CONSTRUCTORS AND THEIR TYPES. Prepared by. MURLI MANOHAR. PGT (COMP
CONSTRUCTORS AND DESTRUCTORS Chapter 5 By Mrs. Suman Verma PGT (Comp.Sc)
Order of Constructor Call. Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
Classes Sujana Jyothi C++ Workshop Day 2. A class in C++ is an encapsulation of data members and functions that manipulate the data. A class is a mechanism.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
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.
Motivation for Generic Programming in C++
Constructors and Destructors
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
Modern Programming Tools And Techniques-I
Introducing Templates and Generics in C++
Overloaded Constructors and Multiple Classes
Topic Pre-processor cout To output a message.
Class and Objects UNIT II.
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Inheritance Concept of Inheritance . Types of Inheritance .
7. Inheritance and Polymorphism
Polymorphism &Virtual Functions
Review: Two Programming Paradigms
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 5 Classes.
Classes in C++ C++ originally called "C with classes":
Polymorphism Lec
Lecture 4-7 Classes and Objects
Object Oriented Programming Language (OOP)
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
CS212: Object Oriented Analysis and Design
CS1201: Programming Language 2
Contents Introduction to Constructor Characteristics of Constructor
Classes in C++ C++ originally called "C with classes":
Anatomy of a Function Part 1
Virtual Base Classes By:Nouf Aljaffan Edited by : Nouf Almunyif.
Constructors and Destructors
CS1201: Programming Language 2
CS1201: Programming Language 2
CS1201: Programming Language 2
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Multiple Base Classes Inheritance
Inheritance:Concept of Re-usability
Inheritance.
Inheriting Multiple Base Classes
Java Programming Language
Inheritance in c++ Introduction
Presentation transcript:

Inheritance

Introduction Inheritance is one of the cornerstones of OOP because it allows the creation of hierarchical classifications. Using inheritance, you can create a general class that defines traits common to a set of related items. This class may then be inherited by other, more specific classes, each adding only those things that are unique to the inheriting class. A class that is inherited is referred to as a base class. The class that does the inheriting is called the derived class. Further, a derived class can be used as a base class for another derived class. In this way, multiple inheritance is achieved.

Base-Class Access Control When a class inherits another, the members of the base class become members of the derived class. Class inheritance uses this general form: class derived-class-name : access base-class-name { // body of class } The access status of the base-class members inside the derived class is determined by access. The base-class access specifier must be either public, private, or protected. If no access specifier is present, the access specifier 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.

When the access specifier for a base class is public, all public members of the base become public members of the derived class, and all protected members of the base become protected members of the derived class. In all cases, the base's private elements remain private to the base and are not accessible by members of the derived class.

#include <iostream #include <iostream.h> 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; derived(int x) { k=x; } void showk() { cout << k << "\n"; } void 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 }

When the base class is inherited by using the private access specifier, all public and protected members of the base class become private members of the derived class. When a base class' access specifier is private, public and protected members of the base become private members of the derived class.

Inheritance and protected Members When a member of a class is declared as protected, that member is not accessible by other, nonmember elements of the program. Access to a protected member is the same as access to a private member — it can be accessed only by other members of its class. The sole exception to this is when a protected member is inherited. A private member of a base class is not accessible by other parts of your program, including any derived class. If the base class is inherited as public, then the base class' protected members become protected members of the derived class and are, therefore, accessible by the derived class. 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.

#include <iostream #include <iostream.h> class base { protected: int i, j; // private to base, but accessible by derived public: void set(int a, int b) { i=a; j=b; } void show() { cout << i << " " << j << "\n"; }}; class derived : public base { int k; // derived may access base's i and j void setk() { k=i*j; } void showk() { cout << k << "\n"; }}; void main() { derived ob; ob.set(2, 3); // OK, known to derived ob.show(); // OK, known to derived ob.setk(); ob.showk();}

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; } If, however, base were inherited as private, then all members of base would become private members of derived1, which means that they would not be accessible by derived2. When a derived class is used as a base class for another derived class, any protected member of the initial base class that is inherited (as public) by the first derived class may also be inherited as protected again by a second derived class. #include <iostream.h> 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; void setk() { k = i*j; } // legal void showk() { cout << k << "\n"; } // i and j inherited indirectly through derived1. class derived2 : public derived1 { int m; void setm() { m = i-j; } // legal void showm() { cout << m << "\n"; }

Protected Base-Class Inheritance It is possible to inherit a base class as protected. When this is done, all public and protected members of the base class become protected members of the derived class. #include <iostream> using namespace std; class base { protected: int i, j; // private to base, but accessible by derived public: void setij(int a, int b) { i=a; j=b; } void showij() { cout << i << " " << j << "\n"; } }; // Inherit base as protected. class derived : protected base{ int k; // derived may access base's i and j and setij(). void setk() { setij(10, 12); k = i*j; } // may access showij() here void showall() { cout << k << " "; showij(); } int main() { derived ob; // ob.setij(2, 3); // illegal, setij() is // protected member of // derived ob.setk(); // OK, public member of derived ob.showall(); // OK, public member of //derived // ob.showij(); // illegal, showij() is //protected member of //derived return 0; }