0% found this document useful (0 votes)
4 views34 pages

Understanding Polymorphism in OOP

The document outlines key concepts in Object-Oriented Programming, focusing on polymorphism, messaging, access protection, and inheritance in C++. It explains how polymorphism allows functions and operators to take multiple forms, and discusses the importance of access specifiers (public, private, protected) in managing data visibility and security within classes. Additionally, it presents practical scenarios for applying these concepts in a Student Management System and a vehicle tracking system.

Uploaded by

Imdadul Islam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views34 pages

Understanding Polymorphism in OOP

The document outlines key concepts in Object-Oriented Programming, focusing on polymorphism, messaging, access protection, and inheritance in C++. It explains how polymorphism allows functions and operators to take multiple forms, and discusses the importance of access specifiers (public, private, protected) in managing data visibility and security within classes. Additionally, it presents practical scenarios for applying these concepts in a Student Management System and a vehicle tracking system.

Uploaded by

Imdadul Islam
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSE 205

Object Oriented Programming Language

WEEK 2

Lec Tasfia Sara


tasfiasara1000@gmail.com1
Lec Tasfia Sara
Polymorphism

Lec Tasfia Sara


Polymorphism

Lec Tasfia Sara


Polymorphism
● Polymorphism means the ability to take more than one form.
● It is the quality that allows one name to be used for two or more related
but technically different purposes. I.e.
○ Function overloading: Using a single function name to perform
different types of tasks (in the previous page’s picture.)
○ Operator overloading: An operation may exhibit different behaviors
depending upon the types of data.
Example (Addition operator):
■ For two numbers, the operation will generate a sum.
■ For string operands, the operation would produce a third string by
concatenation

Lec Tasfia Sara


Polymorphism

Lec Tasfia Sara


Messaging

Lec Tasfia Sara


Messaging
● Objects communicate with one another by sending and
receiving information to each other.

● A message for an object is a request for execution of a


procedure and therefore will invoke a function in the
receiving object that generates the desired results.

● Message passing involves specifying the name of the


object, the name of the function and the information to be
sent.
7

Lec Tasfia Sara


Messaging
Messaging:
Example 1

Lec Tasfia Sara


Namespace

Lec Tasfia Sara


Follow the sessional
slide for this topic!
Reference: [Link]
Lec Tasfia Sara
Class work
Define two namespaces “SimpleProfitSpace” and “CompoundProfitSpace”
both containing a function with same name “CalculateProfit”. Use necessary
variables.

Take input input from the user for the principal, rate of interest, and time
period. Then calls the respective CalculateProfit() functions from both
namespaces and displays the results of both simple and cyclic profits. Provide
the input as parameter.

The formula for simple profit is: Simple Profit= (P x R x T)/100

The formula for Compound Profit=P(1+ R/100)T−P

where P is the principal, R is the rate of interest, and T is the time period. 12

Lec Tasfia Sara


Access
Protection

13

Lec Tasfia Sara


Access Protection
● Access specifiers are used to specify access restriction to the class
members.
● Data hiding is made possible by access specifiers.
● C++ class and struct have three levels of protection.
○ Public: Open for everyone to use, like general information you
share with everyone.
○ Private: Hidden but accessible to anyone in a specific class, like
personal information you share with family or a few trusted
friends.
○ Protected: Shared with only certain parts of the code, like
secrets that you keep to yourself or maybe 1-2 confidants.
Access Specifiers

○ Private
■ Usage - Default / private:
■ Private vars accessible only by other member functions of the
class & friend functions
■ Private functions can’t be called using objects!
■ Private member vars are not accessible from child class
objects!
Access Specifiers
○ Public
■ Usage - public:
■ Accessible by other members and by any other part of the program
that contain the class.
■ Can set and get the value of public variables without any member
function.

16

Lec Tasfia Sara


Access Specifiers
○ Protected
■ Usage - protected:
■ Protected vars can only be accessed using member functions of the
class.
■ Protected functions can’t be called using objects !
■ Inherited protected vars can be accessed using public functions in
child / derived classes.

17

Lec Tasfia Sara


Inheritance: Access Specifiers
Syntax

class derived_class : public base_class

● The keyword public can be replaced with protected or private


● The access specifier limits the most accessible level for the members inherited from
the base class.
● If no access level is specified for the inheritance, the compiler assumes private.

Example

class cseStudent : public mistStudent

● It means the cseStudent class inherits the public members of mistStudent as public,
and the protected members remain protected in the cseStudent class.
18

Lec Tasfia Sara


Inheritance: Access Specifiers
Example

class cseStudent : protected mistStudent

● It means the cseStudent inherits the public members of mistStudent but with
protected access level. The protected member variables are inherited as such with
same level.

class cseStudent : private mistStudent

● It means the cseStudent class inherits the public and protected members of
mistStudent, but both become private in cseStudent.

19

Lec Tasfia Sara


Access Protection

Reference: CLICK HERE 20

Lec Tasfia Sara


Access Protection-
Example 1

Example 2
Example 3 21

Lec Tasfia Sara


Access Specifiers
● Private and Protected vars and functions can’t be accessed from any
Other code written outside the class, not even by objects of the same
class.
● Private vars are not inherited by a child class. Otherwise
encapsulation could be bypassed by inheritance.
● All protected and public vars of base class, are inherited by a child
class.
● Inherited protected and public vars can be accessed using public
functions of the child class.
● Access specifiers are also used in inherited class definition.
● Compiler enforces the access protection.
22

Lec Tasfia Sara


Access Mutator Methods
If all the variables in a class are private, then how do we access the
values?

Using Accessor and Mutator methods -


● Accessor: Also known as getter, returns the value of the private
member variable. This method has a return type and usually no
parameters.
● Mutator: Widely known as setter methods, used to control
changes to a variable. This method usually has an arguments and
void as return type.
23

Lec Tasfia Sara


Access Mutator Methods- Example
#include <iostream> // Setter for name
using namespace std; void setName(string n) {
name = n;
class Account { }
private:
string name; // Getter for name
int acctNo; string getName() {
int balance; return name;
}
public:
// Setter for balance // Setter for account number
void setBalance(int b) { void setAcctNo(int a) {
balance = b; acctNo = a;
} }

// Getter for balance // Getter for account number


int getBalance() { int getAcctNo() {
return balance; return acctNo;
} }
}; 24

Lec Tasfia Sara


Access Specifier: Visualization

25

Lec Tasfia Sara


Try it
class Demo_1 { void f() {
public: cout << “Global function” << endl;
void f(); }
};
void Demo_1::f() {
cout << “Demo_1 function” << endl;
class Demo_2 { }
public:
void f(); void Demo_2::f() {
}; cout << “Demo_2 function” << endl;
}
Let’s see some use case
scenario!
Problem Scenario 1
You are assigned to create a Student Management System for MIST. The system
should allow students to be registered, with their names, address, phone number, student
IDs, and grades stored securely. Each student can perform different actions like updating
their address, changing their password, and requesting a grade change, but only the
Academic Affairs Department should have access to modify the grades directly. These are
some requirements to maintain:

● The student should only be able to view their grades, not directly modify them.
● Only faculty members from the Academic Affairs Department should have the ability
to modify student grades.
● Students can update their personal information (address, phone number, etc.), but they
cannot alter their grades directly.
● The system should prevent unauthorized access to sensitive data, such as student
grades and personal contact details.
● The admin should have the ability to manage the entire system, including adding or
removing faculty members and students. 28

Lec Tasfia Sara


Problem Scenario 1
Given this scenario, what access specifiers (public, private, and protected) would you use for
the following:
1. Student grades and student personal data (such as name, student ID, and address)?
2. Functions for updating student grades and viewing student grades?
3. Admin functions for adding/removing students and faculty?
4. Faculty functions for modifying grades or reviewing grade change requests?
5. Functions for students to update their personal information?

29

Lec Tasfia Sara


Problem Scenario 1- Answers
1. Student grades and student personal data (such as name, student ID, and address):

* Private:
* Grades should be private to prevent direct modification by students or unauthorized
personnel.
* Personal data (name, student ID, address) should also be private, ensuring only authorized
users (like the student or faculty) can access or modify it.

2. Functions for updating student grades and viewing student grades:


View Grade (for student): public
* Viewing grades can be a public function so that students and faculty can view grades, but
with proper access control (students can only see their own grades).

Update Grade (for faculty): protected or public in a Faculty class only or


* Updating grades should be protected or public in Faculty class, as only authorized users
(faculty members) should have the ability to modify grades.
30

Lec Tasfia Sara


Problem Scenario 1- Answers
3. Admin functions for adding/removing students and faculty:

* public (but inside Admin class)


* Admin functions to add/remove students and faculty should be public, but access should be restricted
to admins only, these are high-level functions not accessible to students or faculty.

4. Faculty functions for modifying grades or reviewing grade change requests:

*Protected/Public:
* Faculty functions for modifying grades should be protected (to allow modification only within faculty
and derived classes) or public for those with faculty roles. (Use Protected if you want only derived classes of
Faculty to use updateGrade() internally.)
* Reviewing grade change requests can be a public function but accessible only by faculty.

5. Functions for students to update their personal information:


* Public:
* Students should have public functions to update their personal information (like address or contact
details), but only their own data can be modified. 31

Lec Tasfia Sara


Problem Scenario 2
In a vehicle system,
● Vehicles are tracked using GPS and reported on a central system.
● Drivers can only view the current location of their assigned vehicle and change their
status (active/inactive).
● Managers have the ability to view the location of all vehicles, assign vehicles to drivers,
and adjust the system settings.
● Vehicle information (make, model, and license plate) is confidential and should only
be accessed by authorized personnel.
● The system should ensure that sensitive vehicle data is not exposed to unauthorized
users.
What access specifiers would you use for:
1. Vehicle information (make, model, license plate)?
2. Driver's ability to view and modify their vehicle's status?
3. Manager's access to all vehicle locations and statuses?
4. System's ability to change tracking settings?
32

Lec Tasfia Sara


Problem Scenario 2- Answers
1. Private: Vehicle information such as make, model, and license plate should be private
because it's sensitive and should not be accessed directly from outside the class. Only
authorized personnel (e.g., managers) should have access to this information.

2. Vehicle status variable: private


Functions to view/change status: public (with permission checks)
The status variable itself must be hidden, but drivers are allowed to interact with it
through methods. The driver's ability to view and modify the status of their assigned
vehicle should be public, but with restricted access. Only the driver can change the status
of their assigned vehicle, and private checks should be applied to ensure that only the
driver of that specific vehicle can modify its status.

33

Lec Tasfia Sara


Problem Scenario 2- Answers

1. Public (in Manager class):


Managers should have access to all vehicle locations and statuses. A public
method can be used for this, but access should be verified, ensuring only
authorized managers can execute such functions.
How to prevent unauthorized users from calling public functions? Ans. With role checks and
authentication, not just access specifiers.
2. Private (variable), public/protected (setter method with role check):
Tracking settings are core system configurations, only the system or admin
should modify them.

34

Lec Tasfia Sara

You might also like