Академический Документы
Профессиональный Документы
Культура Документы
LEVEL – LEARNER
1
Icons Used
2
Overview
3
Objectives
4
Inheritance
5
Inheritance (Contd.)
• Inheritance means deriving a new class (sub class) from an existing class (base class
or super class).
• Inheritance creates a hierarchy of related classes (types) which share code and
interface.
Father Example:
State: The father is called the
Assets “Super” class and the
Car Father is the
son is called the
Behaviour: parent class.
Exercise Daily “Sub” class.
Do Work
6
Check Your Understanding
7
Lend A Hand: Forms of Inheritance
• Single Inheritance is the method in which a derived class has only one base class.
#include <iostream>
class Shape // Base class
{
public:
void setWidth(int w)
{
width = w; What will be the
}
output of the code
void setHeight(int h)
{ when you execute it?
height = h;
}
protected:
int width;
int height;
};
class Rectangle: public Shape // Derived class
{
public:
int getArea()
8 {
return (width * height);
Lend A Hand: Forms of Inheritance
• A private member of a base class cannot be inherited and therefore it is not available
for the derived class directly.
• This can be done by modifying the visibility limit of the private member by making it
public. This would make it accessible to all the functions of the programs.
class alpha
{
private: // with in the class
};
10
Polymorphism
• What is Polymorphism?
— The ability to take on different
forms.
— Manipulate objects of various
classes and invoke methods on an
object without knowing that
object’s type.
11
Polymorphism (Contd.)
• Polymorphism is the feature that allows one interface to be used for general class of
action. The specific action is determined by the exact nature of the situation.
• By these you got an idea that, if there is a one interface then there can be many
implementation classes for it. This depends upon:
— the Object type instantiation
— the particular behavior that will be invoked.
12
Polymorphism (Contd.)
13
Polymorphism Types
— Method Overloading: Two versions of the same method available in the same class.
— Method Overriding: Methods of a sub class override the methods of a super class.
— Dynamic Method Binding: At run time the interpreter will find out which object’s
method needs to be executed.
14
Overloading
• Overloading enables several functions of the same name to be defined, as long as these
functions have different sets of parameters. This capability is called function
overloading.
• Overloading selects the proper function by examining the number, types, and order of
the arguments in the call. Function overloading is commonly used to create several
functions of the same name that perform similar tasks but on different data types.
•Example:
— void info(char *name); // if we call info(Bob) this function is executed
— void info(char *name, int age); // if we call info(Bob, 18) this
function is executed
15
Lend A Hand: Function Overloading
#include <iostream>
class printData
{ public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
What will be the output
of the code when you
int main(void) execute it?
{
printData pd;
pd.print(5); // Call print to print integer
16 pd.print(500.263); // Call print to print float
Operator Overloading
17
Lend A Hand: Operators
Overloading in C++
•What will be the output of the code?
#include <iostream>
class Shape // Base class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
class PaintCost // Base class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};
& | ~ ! , =
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
:: .* . ?:
19
Overriding
• Father likes to read books. His favorite • Son also likes to read books. His favourite
genre is medical fiction genre is modern fantasy comics
• Here, the son has inherited the father’s behavior of reading books, but with a difference in the
type of book. Here the son is overriding the behavior of his father.
20
Questions
21
Check Your Understanding
22
Summary
23
Summary (Contd.)
24
Source
• http://www.programcall.com/20/csnet/types-of-inheritance-in-csnet.aspx
• http://en.kioskea.net/contents/poo/polymorp.php3
• http://atomicobject.com/pages/Inheritance
• http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
• http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm
Disclaimer: Parts of the content of this course are based on the materials available from the websites and books listed
above. The materials that can be accessed from the linked sites are not maintained by Cognizant Academy and we are
not responsible for the contents thereof. All trademarks, service marks, and trade names in this course are the marks
of the respective owner(s).
25
Change Log
26
Object – Oriented Concepts
27