Inheritance and Inline Function
Inheritance and Inline Function
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();
}
Rectangle
Triangle Circle 3D-Point
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
• 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?
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 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;
}
};
};
// 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;
};
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; };
}
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.
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