0% found this document useful (0 votes)
34 views24 pages

Chapter-9 (Inheritance 31.8.2020)

Uploaded by

jenesias2919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views24 pages

Chapter-9 (Inheritance 31.8.2020)

Uploaded by

jenesias2919
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

SECOND YEAR

IT-22015
Programming Language in C++

Chapter 9
Inheritance

1
SECOND YEAR

Topics

Derived Class and Base Class

Derived Class Constructors

Overriding Member Functions

Inheritance in the English Distance Class

Class Hierarchies

2
SECOND YEAR

Objectives

To learn about inheritance

To be familiar with superclass and subclass

To explore how to redefine the member functions of a base class

To examine how the constructors of base and derived classes work

3
SECOND YEAR

Learning Outcomes

Be able to understand Super classes and Sub classes.

Understand how to work the Overriding Member Functions and


Derived Class Constructors.

Understand how to apply the major object-oriented concepts to


implement object oriented programs in C++ and inheritance.

4
SECOND YEAR

What is Inheritance?

Inheritance is one of the most important concepts in object-oriented


programming.

Inheritance is the process of creating new classes, called Derived or


Child or Sub classes, from existing classes or Parent or Base or Super
classes.

Inheritance makes the code reusable. When we inherit an existing class,


all its methods and fields become available in the new class, hence code
is reused.

5
SECOND YEAR

Basic Syntax of Inheritance


class Subclass_name : access_mode Superclass_name
{
// data members
// methods
}

While defining a subclass like this, the super class must be already
defined or at least declared before the sub class declaration.
Access Mode is used to specify, the mode in which the properties of
super class will be inherited into sub class, public, private or protected.
: is operator which is used for inheriting the features of base class into
derived class it improves the functionality of derived class.

6
SECOND YEAR

Derived Class and Base Class

The derived class is the class that inherit properties from base class
(es).

The derived class inherits all the capabilities of the base class and can
add additional features to the derived class.

A base class is a class from which other classes are derived.

The base class is used for creation of other classes.

7
SECOND YEAR

Example of Inheritance
Animal
Base Class /
Super Class

Derived from

Dog Cat Cow

Derived Class /
Sub Class
Dog class, Cat class and Cow class inherit the
properties from its super class Animal.
8
SECOND YEAR

Inheritance and Accessibility


A derived class can access all the non-private members of its base class.
Thus base-class members that should not be accessible to the member
functions of derived classes should be declared private in the base class.

Access Accessible from Accessible from Accessible from


Specifier Own Class Derived Class Objects Outside Class

public yes yes yes


protected yes yes no
private yes no no

9
SECOND YEAR

Example of inheritance with Counter class


#include <iostream> class CountDn : public Counter
using namespace std; {
class Counter public:
{ Counter operator --()
protected: { return Counter(--count); }
};
unsigned int count;
int main ()
public: {
Counter () : count (0) CountDn c1;
{} cout << “\nc1=” << c1.get_count();
Counter ( int c) : count (c) ++c1; ++c1; ++c1; Output
{} cout << “\nc1=” << c1.get_count();
--c1; --c1; c1 = 0
unsigned int get_count () const c1 = 3
cout << “\nc1=” << c1.get_count();
{ return count; } c1 = 1
cout << endl;
Counter operator ++ () return 0; }
{ return Counter(++count); } 10
};
SECOND YEAR

// constructors in derived class


#include <iostream>
Derived Class using namespace std;
Constructors class Counter
{
protected:
What happens if we want to initialize unsigned int count;
a CountDn object to a value?
public:

Can the one-argument constructor in Counter() : count()


Counter be used? {}
Counter (int c) : count(c)
- The answer is no. {}
unsigned int get_count()
We must write a new set of constructors
{ return count; }
for the derived class.
Counter operator ++ ()
{ return Counter(++count); }
};
11
SECOND YEAR

Derived Class Constructors


class CountDn: public Counter ++c1; ++c1; ++c1;
{ cout << “\nc1=” << c1.get_count ();
public:
CountDn() : Counter() { } --c2; --c2;
cout<< “\nc2=” << c2.get_count();
CountDn (int c) : Counter(c) { }
CountDn operator --() CountDn c3 = --c2;
{ return CountDn(--count); } cout<< “\nc3=” << c3.get_count();
};
cout<< endl;
int main () return 0; Output
{ }
c1 = 0
CountDn c1; c2 = 100
CountDn c2 (100); c1 = 3
c2 = 98
cout << “\nc1=” << c1.get_count (); c3 = 97
cout << “\nc2=” << c2.get_count ();
12
SECOND YEAR

Overriding Member Functions

We can define data member and member function with the same name
in both base and derived class.

When the function with same name exists in both class and derived
class, the function in the derived class will get executed.

This means, the derived class function overrides the base class
function.

13
SECOND YEAR

Overriding Member Functions


#include <iostream> class Stack2 : public Stack
using namespace std; {
#include <process.h> public:
class Stack void push(int var)
{ {
protected :
if (top >= MAX-1)
enum { MAX = 3 };
int st [MAX] ; { cout<< “\n Error: stack is full”; exit(1); }
int top; Stack::push (var );
}
public:
Stack() int pop()
{ top = -1; } {
void push (int var) if(top < 0)
{ st [++top] = var; } { cout<< “\n Error: stack is empty\n”;
int pop() exit(1); }
{ return st [top--]; } return Stack::pop();
}; }
}; 14
SECOND YEAR

Overriding Member Functions


int main()
{
Stack2 s1;

s1.push(11);
s1.push(22);
s1.push(33);

cout<< endl<< s1.pop();


Output
cout<< endl<< s1.pop();
cout<< endl<< s1.pop(); 33
cout<< endl<< s1.pop(); 22
cout<< endl; 11
return 0; Error: stack is empty
}
15
SECOND YEAR

Inheritance in the English Distance Class


// inheritance using English Distances void getdist ()
#include <iostream> {
using namespace std; cout<< “\nEnter feet: ”;
enum posneg{ pos, neg}; cin>> feet;
class Distance cout<< “Enter inches: ” ;
{ cin>> inches;
protected: }
int feet;
float inches; void showdist()
public: {
Distance() : feet(0), inches(0.0) cout<< feet << “\’-” << inches<< ‘\”’;
{ } }
};
Distance(int ft, float in) : feet( ft),inches(in)
{ }

16
SECOND YEAR

Inheritance in the English Distance Class


class DistSign : public Distance void showdist ()
{ {
private: cout<< ( (sign==pos) ? “(+)” : “(-)” );
posneg sign; Distance::showdist ();
public: } Here’s some sample output:
};
DistSign () : Distance() Enter feet: 6
int main()
{ sign = pos; } Enter inches: 2.5
{ Enter sign (+ or -): -
DistSign (int ft, float in, posneg sg =pos) :
DistSign alpha;
Distance(ft, in) alpha = (-) 6’-2.5”
alpha.getdist(); beta = (+) 11’-6.25”
{ sign = sg; }
DistSign beta(11, 6.25); gamma = (-) 100’-5.5”
void getdist() DistSign gamma(100, 5.5, neg);
{ cout<< “\n alpha= ”;
Distance::getdist (); alpha.showdist();
char ch; cout<< “\n beta= ”;
cout<< “Enter sign (+ or -): ” beta.showdist();
cin>> ch; cout<< “\n
sign = (ch==‘+’) ? pos: neg; gamma= ”; gamma.showdist(); 17
} cout<< endl;
SECOND YEAR

Class Hierarchies

Inheritance has been used to add


functionality to an existing class.

Now let’s look at an example where


inheritance is used for a different purpose :
as part of the original design of a program.

Class hierarchies are one-to-one, one-to-


many , many-to-one and many-to-many.

UML class diagram for EMPLOY


18
SECOND YEAR

Class Hierarchies
// models employee database using inheritance void putdata () const
#include <iostream> {
using namespace std; cout<< “\n Name: ” << name;
const int LEN = 80; cout<< “\n Number: ” << number; }
class employee };
{
private: class manager : public employee
char name[LEN]; {
unsigned long number; private:
char title[LEN]; double dues;
public:
public:
void getdata () void getdata()
{ {
cout<< “\n Enter last name: ”; employee::getdata();
cin >> name; cout<< “ Enter title: ”; cin >> title;
cout<< “ Enter number: ”; cout<< “ Enter golf club dues: ”;
cin >> number; cin>> dues;
} }
19
SECOND YEAR

Class Hierarchies
void putdata() const void putdata () const
{ {
employee::putdata(); employee :: putdata ();
cout<< “\n Title: ” << title; cout<< “\n Number of publications: ” <<
cout<< “\n Golf club dues: ” << pubs;
dues; }
} };
}; class laborer : public employee
class scientist : public employee {
{ };
private: int pubs; int main ()
public: {
void getdata() manager m1, m2;
{ scientist s1;
employee :: getdata ();
cout<< “ Enter number of pubs: ”; laborer l1;
cin>> pubs; cout<< endl;
} 20
SECOND YEAR

Class Hierarchies
cout<< “\nEnter data for manager
1”;
m1.getdata();
cout<< “\nEnter data for manager
2”;
m2.getdata();
cout<< “\nEnter data for scientist
1”; Output
s1.getdata();
cout<< “\nEnter data for laborer 1”;
l1.getdata();
cout<< “\n Data on manager 1”;
m1.putdata();
cout<< “\n Data on manager 2”;
m2.putdata();
cout << “\n Data on scientist1”;
s1.putdata();
21
cout<< “\n Data on laborer 1”;
SECOND YEAR

Summary
A derived class can inherit the features of a base class.

The derived class can add other features of its own, so it becomes a
specialized version of the base class.

Inheritance provides a powerful way to extend the capabilities of


existing classes, and to design programs using hierarchical
relationships.

Classes may be publicly or privately derived from base classes.

Inheritance permits the reusability of software.

22
SECOND YEAR

References

‐ Object-Oriented Programming in C++ (4th Edition) by Robert Lafore

‐ https://www.studytonight.com/cpp/overview-of-inheritance.php

‐ https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/

23
SECOND YEAR

THANK YOU!

24

You might also like