Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
157 views

Inheritance and Inline Function

The document discusses the concept of inheritance in object-oriented programming. It defines inheritance as inheriting properties and capabilities from a base class to a derived class. This allows for code reusability, reliability, and better program design. It provides examples to illustrate inheritance between classes like Circle and Sphere, Employee and Manager. It also discusses private/protected member access, base class access specifications, and overriding member functions in derived classes.

Uploaded by

Naveen
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views

Inheritance and Inline Function

The document discusses the concept of inheritance in object-oriented programming. It defines inheritance as inheriting properties and capabilities from a base class to a derived class. This allows for code reusability, reliability, and better program design. It provides examples to illustrate inheritance between classes like Circle and Sphere, Employee and Manager. It also discusses private/protected member access, base class access specifications, and overriding member functions in derived classes.

Uploaded by

Naveen
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

Inheritance

• Concept of Inheritance (What, Why, and How)


• Simple Example of Inheritance
• Base Classes and Derived Classes
• Private Member Data vs. Protected Member Data
• Base Class Access Specifications
• The Base Class Constructor Calls
• Overriding Member Functions
Inheritance (continue)
• Types of Inheritance
• More Examples of Simple Inheritance
• Examples of Multi-Level Inheritance
• Examples of Multiple Inheritance
Concept of Inheritance
• What is inheritance?
- to inherit properties/capabilities (member data
and member functions) from an existing class
or existing classes.
- the existing class is called as a base class
- the inherited class is called as a derived class
- The two classes in the inheritance must have
relationship in terms of code-reusable
Concept of Inheritance (continue)
• Why Inheritance?
- code reusability (save time and money)
- Increasing program reliability
- Better problem-solving and program design
- Supporting polymorphism
Concept of Inheritance (continue)
• Conceptual examples
- example 1: circle
r
base class: circle
area = 3.1415*r*r

derived class: sphere sphere


area = 4 *circle::area r

volume = 4/3*circle::area*r
Sphere is kind of circular shape
Concept of Inheritance (continue)
- example 2:
base class: employee
members:
ID, name, salary
employee()
show()
derived class: manager
members:
manager is kind of employee office, bonus
manager()
computeBonus()
show()
Base Classes and Derived Classes
• Characteristics of a base class
- has all features of the objects in the category
- is a general description of all objects in the category in
terms of member data and member functions
• Characteristics of a derived class
- has detailed and specific features of the objects in the
class
- usually has more capabilities than the base class in
terms of member data and member functions
• Examples:
- circle sphere employee manager
Private Member Data vs.
Protected Member Data
• Define the member data as protected in a base
class if the derived class can directly access the
data as it own member data
• Define the member data as private in a base class
if the derived class can only access the data via
the member functions defined in the base class.
Inheritance Concept
class Rectangle{
Polygon private:
int width, length;
public:
void set(int w, int l);
Rectangle
Triangle int area();
}

class Polygon class Triangle{


{ private:
private: int width, length;
int width, length; public:
public: void set(int w, int l);
void set(int w, int l); int area();
}
9 }
Inheritance Concept
class Polygon
Polygon {
protected:
int width, length;
public:
Rectangle void set(int w, int l);
Triangle }
class Rectangle{
protected:
class Rectangle : public Polygon int width, length;
{ public:
public: int area(); void set(int w, int l);
} int area();
10
}
Inheritance Concept
class Polygon
Polygon {
protected:
int width, length;
public:
Rectangle void set(int w, int l);
Triangle }
class Triangle{
protected:
class Triangle : public Polygon int width, length;
{ public:
public: int area(); void set(int w, int l);
} int area();
11
}
Inheritance Concept
x
class Point
Point y {
protected:
int x, y;
Circle 3D-Point public:
x x void set(int a, int b);
y y
r z }

class Circle : public Point class 3D-Point: public Point


{ {
private: private:
double r; int z;
} }
12
Inheritance Concept
• Augmenting the original class
Polygon Point

Rectangle
Triangle Circle 3D-Point

• Specializing the original class


ComplexNumber real
imag

RealNumber ImaginaryNumber imag


13 real
What to inherit?

• In principle, every member of a base class is


inherited by a derived class
– just with different access permission

14
Access Control Over the Members
• Two levels of access control
base class/ superclass/ over class members
parent class – class definition
– inheritance type
members goes to
derive from

class Point{
protected: int x, y;
public: void set(int a, int b);
}
derived class/ subclass/
child class class Circle : public Point{
……
15
}
Access Rights of Derived Classes
Type of Inheritance
private protected public
for Members
Access Control

private private private private


protected private private protected
public private protected public

• The type of inheritance defines the minimum access level for the
members of derived class that are inherited from the base class
• With public inheritance, the derived class follow the same access
permission as in the base class
• With protected inheritance, only the public members inherited from the
base class can be accessed in the derived class as protected members
• With private inheritance, none of the members of base class is accessible
16by the derived class
Class Derivation
class daughter : public mother{
mother private:
double a;
public:
daughter son void foo ( );
}
class mother{
protected: void daughter :: foo ( ){
int x, y; x = y = 20;
public: set(5, 10);
void set(int a, int b); cout<<“value of a ”<<a<<endl;
private: z = 100; // error, a private member
int z; }
} 17
daughter can access 3 of the 4 inherited members
Class Derivation
class son : protected mother{
mother private:
double b;
public:
daughter son void foo ( );
}
class mother{
protected: void son :: foo ( ){
int x, y; x = y = 20; // error, not a public member
public: set(5, 10);
void set(int a, int b); cout<<“value of b ”<<b<<endl;
private: z = 100; // error, not a public member
int z; }
} 18
son can access only 1 of the 4 inherited member
What to inherit?

• In principle, every member of a base class is


inherited by a derived class
– just with different access permission

• However, there are exceptions for


– constructor and destructor
– operator=() member
– friends
Since all these functions are class-specific
19
Constructor Rules for Derived Classes
The default constructor and the destructor of the
base class are always called when a new object
of a derived class is created or destroyed.

class A { class B : public A


public: {
A() public:
{cout<< “A:default”<<endl;} B (int a)
A (int a) {cout<<“B”<<endl;}
{cout<<“A:parameter”<<endl;} }
}

output: A:default
20
B test(1); B
Constructor Rules for Derived Classes
You can also specify an constructor of the
base class other than the default constructor
DerivedClassCon ( derivedClass args ) : BaseClassCon ( baseClass
args )
{ DerivedClass constructor body }

class A { class C : public A


public: {
A() public:
{cout<< “A:default”<<endl;} C (int a) : A(a)
A (int a) {cout<<“C”<<endl;}
{cout<<“A:parameter”<<endl;} }
}
output: A:parameter
21
C test(1); C
Define its Own Members
The derived class can also define
its own members, in addition to
class Point{
the members inherited from the
base class protected:
int x, y;
x
Point y
public:
void set(int a, int b);
x }
y Circle
r protected:
class Circle : public Point{ int x, y;
private: private:
double r; double r;
public: public:
void set_r(double c); void set(int a, int b);
22
} void set_r(double c);
Even more …
• A derived class can override methods defined in its parent
class. With overriding,
– the method in the subclass has the identical signature to the method
in the base class.
– a subclass implements its own version of a base class method.

class A {
class B : public A
protected:
{
int x, y;
public:
public:
void print ()
void print ()
{cout<<“From B”<<endl;}
{cout<<“From A”<<endl;}
}
}
23
Access a Method
class Point
class Circle : public Point{
{
private: double r;
protected:
public:
int x, y;
void set (int a, int b, double c) {
public:
Point :: set(a, b); //same name function call
void set(int a, int b)
r = c;
{x=a; y=b;}
}
void foo ();
void print(); }
void print();
}
Circle C;
Point A; C.set(10,10,100); // from class Circle
A.set(30,50); // from base class Point C.foo (); // from base class Point
A.print(); // from base class Point C.print(); // from class Circle
24
Types of Inheritance
• Basically, there are three types of inheritance
- simple inheritance: a base class derives on
or many derived classes
- multiple-level inheritance: a derived class
can be served as a base class for next level of
inheritance
- multiple inheritance: there are more than
one base class to derive one or many derived
classess.
Output:
This is a vehicle

Single Inheritance
// C++ program to explain
// Single inheritance
#include <iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// sub class derived from two base classes


class Car: public Vehicle{

};

// main function Output:


int main()
{
// creating object of sub class will
// invoke the constructor of base classes This is a vehicle
Car obj;
return 0;
}
26
Multiple Inheritance
// C++ program to explain
// multiple inheritance
#include <iostream>
using namespace std;

// first base class


class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// second base class


class FourWheeler {
public:
FourWheeler()
{
cout << "This is a 4 wheeler Vehicle" << endl;
}
};

// sub class derived from two base classes


class Car: public Vehicle, public FourWheeler { Output:
};

// main function
int main()
This is a Vehicle
{
// creating object of sub class will This is a 4 wheeler Vehicle
// invoke the constructor of base classes
Car obj;
return 0; 27
}
Multilevel Inheritance
// C++ program to implement
// Multilevel Inheritance
#include <iostream>
using namespace std;

// 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{
public:
car()
{ output:
cout<<"Car has 4 Wheels"<<endl;
}
};

// main function
This is a Vehicle
int main()
{ Objects with 4 wheels are vehicles
//creating object of sub class will
//invoke the constructor of base classes
Car obj;
Car has 4 Wheels
return 0;
} 28
Hierarchical Inheritance(1)

29
Hierarchical Inheritance(2)
// C++ program to implement
// Hierarchical Inheritance
#include <iostream>
using namespace std;

// base classclass Vehicle


{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

// first sub class


class Car: public Vehicle
{
Output:
};

// second sub class


class Bus: public Vehicle This is a Vehicle
{

};
This is a Vehicle
// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0; 30
}
Hybrid Inheritance(1)

31
Hybrid Inheritance(2)
// first sub class
// C++ program for Hybrid Inheritance class Car: public Vehicle
{
#include <iostream>
using namespace std; };

// base class // second sub class


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

}
cout<<"Fare of Vehicle\n";
Output:
};

This is a Vehicle
Fare of Vehicle 32
Advantages of inheritance
• When a class inherits from another class,
there are three benefits:
• (1) You can reuse the methods and data of
the existing class
(2) You can extend the existing class by
adding new data and new methods
(3) You can modify the existing class by
overloading its methods with your own
implementations
Inheritance Summary
• Inheritance is a mechanism for defining new
class types to be a specialization or an
augmentation of existing types.

• In principle, every member of a base class is


inherited by a derived class with different
access permissions, except for the constructors

34
Inline functions
• An inline function is one in which the function
code replaces the function call directly.
• Inline class member functions
– if they are defined as part of the class definition,
implicit
– if they are defined outside of the class definition,
explicit, I.e.using the keyword, inline.
• Inline functions should be short (preferable one-
liners).
– Why? Because the use of inline function results in
duplication of the code of the function for each
35
invocation of the inline function
Example of Inline functions
class CStr
{
char *pData;
int nLength;

public: Inline functions within class declarations

char *get_Data(void) {return pData; }//implicit inline function
int getlength(void);

};
inline void CStr::getlength(void) //explicit inline function
{
return nLength;
} Inline functions outside of class declarations

int main(void)
{
char *s;
int n;
CStr a(“Joe”); In both cases, the compiler will insert the code
s = a.get_Data();
n = b.getlength(); of the functions get_Data() and getlength()
}
instead of generating calls to these functions
36
Inline functions
• An inline function can never be located in a
run-time library since the actual code is
inserted by the compiler and must therefore be
known at compile-time.
• It is only useful to implement an inline
function when the time which is spent during a
function call is long compared to the code in
the function.

37
References
• https://www.geeksforgeeks.org/inheritance-
in-c/

38

You might also like