0% found this document useful (0 votes)
104 views

Class, Inheritance, and Polymorphism in C

The document discusses key object-oriented programming concepts in C++ including classes, objects, inheritance, polymorphism, and access specifiers. It defines classes as user-defined data types that contain data members and member functions. Objects are instantiated from classes and allocate memory. Inheritance allows a derived class to acquire properties of a base class. Polymorphism occurs through function overloading, operator overloading, and function overriding in derived classes. The document provides examples to illustrate these concepts.

Uploaded by

Enamul Haque
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Class, Inheritance, and Polymorphism in C

The document discusses key object-oriented programming concepts in C++ including classes, objects, inheritance, polymorphism, and access specifiers. It defines classes as user-defined data types that contain data members and member functions. Objects are instantiated from classes and allocate memory. Inheritance allows a derived class to acquire properties of a base class. Polymorphism occurs through function overloading, operator overloading, and function overriding in derived classes. The document provides examples to illustrate these concepts.

Uploaded by

Enamul Haque
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Class, Inheritance, and

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( )

Class Bus Class Car Class truck


Inheritance
• Base class: The existing class from where the new class inherit data
members and data functions.
• Derived class: The new class which can take the data from base class or
previous class.
Types of Inheritance
• Multiple Inheritance: It’s a feature of C++ where a class can inherit from
more than one classes. i.e one sub class is inherited from more than
one base classes.

• Multilevel Inheritance: In this type of inheritance, a derived class is


created from another derived class.
Access Control and Inheritance
• Public: Members of a class are accessible from outside the class.
• Private: Members can only be accessed within the class.
• Protected: It is similar to private, but it can also be accessed in the
inherited class.
Access Control and Inheritance
Access Public Protected Private
Same class Yes Yes Yes
Derived classes Yes Yes No
Outside classes Yes No No
Example of Inheritance
// Base class
class Employee {
int main() {
protected: // Protected access specifier
int salary;
Programmer myObj;
};
myObj.setSalary(50000);
// Derived class
class Programmer: public Employee { myObj.bonus = 15000;
public:
int bonus; cout << "Salary: " << myObj.getSalary()
void setSalary(int s) { << "\n";
salary = s;
} cout << "Bonus: " << myObj.bonus <<
int getSalary() {
"\n";
return salary;
}
return 0;
};
}
Polymorphism
• The term "Polymorphism" is the combination of "poly" + "morphs" which
means many forms..
• It occurs when we have many classes that are related to each other by
inheritance.
Types of polymorphism

Polymorphism

Compile time Run time

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;

// Function with 3 int parameters


int sum(int num1, int num2, int num3) { return 0;
return num1 + num2 + num3;
} }
Example of polymorphism
// C++ program to demonstrate function overriding
int main() {
#include <iostream>
using namespace std;
Derived derived1;

class Base {
public:
virtual void print() {
// Call print() function of Derived class
cout << "Base Function" << endl;
}
derived1.print();
};

class Derived : public Base {


public:
return 0;
void print() {
cout << "Derived Function" << endl;
}
}
};
References
• http://slideplayer.com/slide/17172119/
• https://www.geeksforgeeks.org/c-classes-and-objects/
• https://www.programiz.com/cpp-programming
• https://www.javatpoint.com/cpp-polymorphism
• https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm
• https://www.w3schools.com/cpp/
Thank You!
Any question?

You might also like