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

Introduction to OOP(Unit 2)

Object-Oriented Programming (OOP) in C++ organizes software design around objects and is based on four fundamental pillars: Encapsulation, Abstraction, Inheritance, and Polymorphism. Encapsulation bundles data and methods while restricting access, Abstraction hides implementation details, Inheritance allows classes to inherit properties from others, and Polymorphism enables objects of different classes to be treated as instances of a common base class. Together, these principles enhance software design, making it more maintainable and flexible.

Uploaded by

azipohfaithful80
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Introduction to OOP(Unit 2)

Object-Oriented Programming (OOP) in C++ organizes software design around objects and is based on four fundamental pillars: Encapsulation, Abstraction, Inheritance, and Polymorphism. Encapsulation bundles data and methods while restricting access, Abstraction hides implementation details, Inheritance allows classes to inherit properties from others, and Polymorphism enables objects of different classes to be treated as instances of a common base class. Together, these principles enhance software design, making it more maintainable and flexible.

Uploaded by

azipohfaithful80
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Introduction to Object-Oriented Programming

(OOP) in C++

Object-Oriented Programming (OOP) is a programming paradigm that organizes


software design around objects, rather than functions and logic. An object is an
instance of a class, which is a blueprint for the object. OOP allows us to model
real-world entities and relationships between them more intuitively.

C++ is a powerful programming language that supports OOP principles. OOP in


C++ revolves around the four fundamental pillars: Encapsulation, Abstraction,
Inheritance, and Polymorphism.

Let’s dive deeper into these pillars and demonstrate them with code examples in
C++.

The Four Pillars of OOP

1. Encapsulation

 Definition: Encapsulation is the practice of bundling the data (attributes)


and methods (functions) that operate on the data into a single unit (class). It
also refers to restricting access to certain details of an object, which helps in
data hiding and protects the integrity of the object’s state.
 Private and Public Access Specifiers: In C++, private members cannot
be accessed outside the class, whereas public members can be accessed
from anywhere.
 Getter and Setter Methods: Encapsulation often uses getter and setter
functions to access and modify private variables.

1|Page
2. Abstraction

 Definition: Abstraction is the concept of hiding the implementation details


and exposing only the necessary functionality to the user. It focuses on what
an object does, rather than how it achieves it.
 Abstract Classes and Pure Virtual Functions: In C++, abstraction can be
achieved using abstract classes and pure virtual functions. An abstract class
cannot be instantiated directly, and it contains at least one pure virtual
function (= 0).

3. Inheritance

 Definition: Inheritance allows a class (child or subclass) to inherit attributes


and methods from another class (parent or superclass). This enables
reusability and extension of functionality without modifying existing code.
 Types of Inheritance: In C++, inheritance can be public, protected, or
private.
o Public inheritance means that public and protected members of the
base class become public and protected members of the derived class.
o Protected inheritance makes base class members protected in the
derived class.
o Private inheritance means that base class members are private in the
derived class.

4. Polymorphism

 Definition: Polymorphism means "many shapes." It allows objects of


different classes to be treated as objects of a common base class. The most

2|Page
common use of polymorphism is through virtual functions that allow
dynamic method dispatch at runtime.
 Method Overloading: Method overloading allows a class to have multiple
methods with the same name but different parameters.
 Method Overriding: Method overriding allows a subclass to provide its
specific implementation of a method that is already defined in the base class.

Summary

 Encapsulation is the bundling of data and methods together and restricting


direct access to some of the object’s components. It is implemented through
private variables and public methods (getters/setters).
 Abstraction involves hiding the internal workings of an object and exposing
only the essential parts. It is achieved through abstract classes and pure
virtual functions in C++.
 Inheritance allows a class to inherit properties and behaviors from another
class, enabling code reuse and extension. In C++, this is done via base and
derived classes.
 Polymorphism allows objects of different types to be treated as objects of a
common base type, with method overriding being the primary mechanism to
achieve polymorphism. It is a key feature of runtime flexibility in C++.

With these four pillars, OOP in C++ provides a powerful and flexible way to
design software that is easier to maintain, extend, and modify

3|Page

You might also like