Class, Inheritance, and Polymorphism in C
Class, Inheritance, and Polymorphism in C
Polymorphism in C++
Presented by:
Md. Shafiqul Islam
ID: 2021205060001
Dept. of Mechatronic Engineering
Khwaja Yunus Ali University
Contents
• Class in C++ • Access Control and Inheritance
• Member access specifiers • Example of Inheritance
• Creating an object of a Class • Polymorphism
• Example of class and object • Types of polymorphism
• Inheritance • Example of polymorphism
• Types of Inheritance • References
Class in C++
• A class in C++ is the building block, that leads to Object-Oriented
programming.
• It is a user defined data-type which has data members and member
functions.
• A class definition begins with the keyword ‘class’
• The body of the class is contained within a set of braces, { } ; (notice the
semi-colon).
Member access specifiers
• Public:
• Can be accessed outside the class directly.
• The public stuff is the interface.
• Private:
• Accessible only to member functions of class.
• Private members and methods are for internal use only.
Creating an object of a Class
• Declaring a variable of a class type creates an object. You can have many
variables of the same type (class).
• Once an object of a certain class is instantiated, a new memory location is
created for it to store its data members and code
• You can instantiate many objects from a class type.
Example of class and object
#include <iostream> int main() {
using namespace std; Student s1; //creating an object of Student
class Student { s1.id = 201;
public: s1.name = "Sonoo Jaiswal";
int id; //data member (also instance cout<<s1.id<<endl;
variable)
cout<<s1.name<<endl;
string name; //data member(also
instance variable) return 0;
}; }
Inheritance
• Inheritance is a mechanism in which one object acquires all the
properties and behaviors of parent object in any object oriented
programming.
Class Vehicle
Fuel_Amount( )
Capacity( )
Brakes( )
Polymorphism
Virtual function/Function
Function Overloading Operator Overloading
Overriding
Types of polymorphism
• Function Overloading: When there are multiple functions with same
name but different parameters then these functions are said to be
overloaded.
• Operator Overloading: C++ also provide option to overload operators.
For example, we can make the operator (‘+’) for string class to
concatenate two strings.
• Function Overriding: When we can have the same function in the base
class as well as its derived classes.
Example of polymorphism
// C++ program to overload sum() function int main() {
#include <iostream> // Call function with 2 int parameters
using namespace std;
cout << "Sum 1 = " << sum(5, 6) << endl;
// Function with 2 int parameters
int sum(int num1, int num2) {
return num1 + num2;
// Call function with 2 double parameters
} cout << "Sum 2 = " << sum(5.5, 6.6) << endl;
// Function with 2 double parameters
double sum(double num1, double num2) {
// Call function with 3 int parameters
return num1 + num2;
} cout << "Sum 3 = " << sum(5, 6, 7) << endl;
class Base {
public:
virtual void print() {
// Call print() function of Derived class
cout << "Base Function" << endl;
}
derived1.print();
};