B.FathimaMary Dept. of Commerce CA SJC Trichy-02

Slides:



Advertisements
Similar presentations
Chapter 5 Inheritance. Objectives Introduction, effects, and benefits of inheritance Base class and derived class objects Base class and derived class.
Advertisements

CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Math 130 B Smith: Consider using the example in SAMS Teach Yourself C++ in 24 hours B Smith: Consider using the example in SAMS Teach Yourself.
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance Inheritance is the capability of one class of things to inherit the capabilities or properties from another class. Consider the example of.
INHERITANCE By: Er. Gurpreet Singh Assistant Professor Department of Information Technology, Department of Information Technology, MIMIT Malout 1.
Learners Support Publications Inheritance: Extending Classes.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
1 Chapter 14-1 Object- Oriented Software Development Dale/Weems.
Inheritance using Java
Chapter 12: Adding Functionality to Your Classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
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.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
INHERITANCE IN C++ Amit Saxena PGT(CS) KV Rangapahar Cantt. (Nagaland)
Chapter 10 Inheritance and Polymorphism
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.
Inheritance - Multilevel MEENA RAWAT PGT (COMP) KV ONGC Chandkheda 1.
Inheritance: Base and Derived Classes. Introduction In dictionary inheritance is defined as the action of inheriting; the transfer of property; to receive.
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
Object Oriented Programming COP3330 / CGS5409.  Inheritance  Assignment 5.
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science) MTech[IT],MPhil (Comp.Sci), MCA, MSc[IT], PGDCA, ADCA, Dc. Sc. & Engg.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
Chapter -6 Polymorphism
CS1201: Programming Language 2 Classes and objects Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
Types of Inheritance in C++. In C++ we have 5 different types of inheritance: – Single Inheritance – Multiple Inheritance – Hierarchical Inheritance –
1 Chapter 4: Another way to define a class Inheritance..!!
Class Inheritance. The biggest advantage of object oriented design is that these objects can be repeatedly used and redefined as needed. As programmers,
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
BY:- TOPS Technologies
Constructors and Destructors
Modern Programming Tools And Techniques-I
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Lecture 12 Inheritance.
Inheritance Concept of Inheritance . Types of Inheritance .
Inheritance in Java Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea behind.
2.7 Inheritance Types of inheritance
Inheritance Basics Fall 2008
Introduction to Programming with Java
Inheritance: hiding methods
Inheritance in Java.
Week 8 Lecture -3 Inheritance and Polymorphism
ATS Application Programming: Java Programming
Andy Wang Object Oriented Programming in C++ COP 3330
CS1201: Programming Language 2
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Advanced Programming Behnam Hatami Fall 2017.
Introduction Types Syntax Visibility Modes Example Programs
Constructors and Destructors
CS1201: Programming Language 2
By Muhammad Waris Zargar
C++ Inheritance.
Inheritance: Polymorphism and Virtual Functions
Inheritance:Concept of Re-usability
Inheritance and Polymorphism
Institute of Petroloeum Technology, Gandhinagar
COP 3330 Object-oriented Programming in C++
Computer Science II for Majors
Presentation transcript:

B.FathimaMary Dept. of Commerce CA SJC Trichy-02 Inheritance B.FathimaMary Dept. of Commerce CA SJC Trichy-02

What is inheritance? Inheritance is one of the key features of Object-oriented programming in C++. It allows user to create a new class (derived class) from an existing class(base class).

Why inheritance should be used? Suppose, in your game, you want three characters - a maths teacher, a footballer and a businessman. Since, all of the characters are persons, they can walk and talk. However, they also have some special skills. A maths teacher can teach maths, a footballer can play football and a businessman can run a business. You can individually create three classes who can walk, talk and perform their special skill as shown in the figure below.

Without Using Inheritance

Using inheritance

Implementation of Inheritance in C++ Programming class Person { ... .. ... }; class MathsTeacher : public Person { ... .. ... class Footballer : public Person { .... .. ...

Single Inheritance When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance. For example, Animal is derived from living things Car is derived from vehicle Typist is derived from staff

Syntax of Single Inheritance class base_classname { properties; methods; }; class derived_classname : visibility_mode base_classname { properties; methods;

class AddData          //Base Class {         protected:                 int num1, num2;         public:                 void accept()                 {                         cout<<"\n Enter First Number : ";                         cin>>num1;                         cout<<"\n Enter Second Number : ";                         cin>>num2;                 } }; class Addition: public AddData   //Class Addition – Derived Class {                 int sum;         public:                 void add()                 {                         sum = num1 + num2;                 }                 void display()                 {                         cout<<"\n Addition of Two Numbers : "<<sum;                 } }; int main() {         Addition a;         a.accept();         a.add();         a.display();         return 0; }

Multiple Inheritance Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. i.e one sub class is inherited from more than one base classes.

Syntax class subclass_name : access_mode base_class1, access_mode base_class2, .... { // body of subclass };

Multiple Inheritance class Vehicle { public: Vehicle() { // first base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; // second base class class FourWheeler { FourWheeler() cout << "This is a 4 wheeler Vehicle" << endl; // sub class derived from two base classes class Car: public Vehicle, public FourWheeler { // main function int main() // creating object of sub class will // invoke the constructor of base classes Car obj; return 0;

Multilevel Inheritance Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.

Multilevel Inheritance // base class class Vehicle { public: Vehicle() cout << "This is a Vehicle" << endl; } }; class fourWheeler: public Vehicle { public: fourWheeler() cout<<"Objects with 4 wheels are vehicles"<<endl; } }; // sub class derived from two base classes class Car: public fourWheeler{ car() cout<<"Car has 4 Wheels"<<endl; // main function int main() //creating object of sub class will nvoke the constructor of base classes Car obj; return 0; }

Hierarchical Inheritance: Hierarchical Inheritance: In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.

Hierarchical Inheritance: // base class class Vehicle { public: Vehicle() cout << "This is a Vehicle" << endl; } }; // first sub class class Car: public Vehicle { }; // second sub class class Bus: public Vehicle // main function int main() // creating object of sub class will // invoke the constructor of base class Car obj1; Bus obj2; return 0; }

Hybrid Inheritance Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one type of inheritance. For example: Combining Hierarchical inheritance and Multiple Inheritance. Below image shows the combination of hierarchical and multiple inheritance:

Hybrid Inheritance

Hybrid Inheritance // base class class Vehicle { public: Vehicle() { cout << "This is a Vehicle" << endl; } }; //base class class Fare public: Fare() cout<<"Fare of Vehicle\n";

Hybrid Inheritance // first sub class class Car: public Vehicle { }; // second sub class class Bus: public Vehicle, public Fare // main function int main() { // creating object of sub class will // invoke the constructor of base class Bus obj2; return 0; }