Curriculum
Course: Complete C++ Programming Course
Login

Curriculum

Complete C++ Programming Course

Text lesson

Introduction to POP and OOP Concepts

In this lesson, you will learn

  • Procedure Oriented Programming(POP)
  • Object-Oriented Programming System (OOP)
  • Important Features of OOPS
    • Object
    • Class
    • Abstraction
    • Encapsulation
    • Polymorphism
    • Inheritance

 

1. Procedural Oriented Programming (POP) Language

  • Procedural Oriented Programming (POP) is a programming paradigm based on the concept of subroutines or procedure calls also called functions.
  • This approach contrasts with Object-Oriented Programming (OOP), which organizes software design around data, or objects, rather than functions and logic.
  • Examples – Fortran, Pascal, and BASIC.

 

Key Concepts of POP

Functions and Procedures:

  • The core concept of POP is the use of functions or procedures. These are blocks of code that perform a specific task and can be called from other parts of the program.
  • A procedure might perform a task and then return control to the calling function, while a function typically returns a value.

Modularity:

  • The program is divided into smaller modules or procedures. Each module can be developed and tested independently, which simplifies the programming process and debugging.

Sequential Execution:

  • Execution in procedural programming is top-down, starting from the main function or procedure and proceeding through various function calls sequentially.

Variable Scope and Global Variables:

  • Variables in procedural programming can be either local to a procedure (local variables) or accessible to all procedures (global variables).
  • The use of global variables is common, but it can lead to issues with data integrity and security

Stateful Procedures:

  • Procedures in POP can maintain state information through the use of static or global variables. However, unlike OOP, data and methods are not encapsulated together.

 

Advantages of POP

Simplicity

  • Routine tasks and procedural programming can be more straightforward than OOP.

Performance

  • In some cases, procedural programs can be more efficient in terms of execution time and memory usage, as there is less overhead than in OOP.

Ease of Learning:

  • The concepts of procedural programming are generally easier to grasp for beginners.

 

Disadvantages

Scalability

  • As projects grow in size and complexity, procedural code can become harder to maintain and extend compared to OOP.

Code Reusability

  • OOP offers better mechanisms for code reuse through inheritance and polymorphism, which is less inherent in procedural programming.

Data Security

  • The widespread use of global variables can lead to issues with data security and integrity.

Maintenance Challenges

  • Large procedural programs can become unwieldy, making maintenance and updates challenging.

 

2. Object-Oriented Programming (OOP) Language

  • It combines both data and functions into a single unit that operates on that data that unit is called an object.
  • It is a programming model in which we write programs using classes and objects.
  • This approach is used to make neat and reusable code instead of repetitive ones.
  • The program is divided into objects and every object has its data communicating with each other.

OOPSParadigm

 

Features of OOPS

The following figure shows the core features of OOPS.

OOPSFeatures

Let’s discuss all the features of OOPS with real-world examples.

 

What is an Object?

  • An object is a real-time entity having some state and behavior.
  • An object is an instance of the class having variables like the state of the object and the methods as the behavior of the object.
  • Each object has a specific identity, characteristics, and behavior.

 

Real World Examples

Pembroke(dog) -> Object

1. Dogs have state (name, color, breed, hunger) and behavior (barking, fetching, wagging tail). Chair, Bike, Marker, Pen, Table, Car, Book, Apple, Bag, etc the examples of objects.

It can be physical or logical (tangible and intangible).

 

 

2. Orange is an object.

Orange has a state (shape, color, etc.) and behavior (sweet and sour).

3. Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes).

 

What is a Class?

  • A class is a template or blueprint from which objects are created.
  • A class is a group of objects which have common properties.
  • A grouping that represents a set of objects that share common characteristics and behavior.

 

Class vs Object

  • A class is a blueprint and an object is an instance.
  • A class is a logical entity and an object is a real-world or physical entity.

Example

  • Fruit is a class and Apples, Bananas, and Kiwis are objects.
  • A person is a class and a person named James, Daisy, and Allen are objects of a class Person.

 

Note: A class is a template for an object and is represented by a UML Language or a Diagram.

 

https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/

 

The Unified Modeling Language (UML) or UML Diagram

  • The Unified Modeling Language (UML) is a standardized way to visualize a program’s structure and operation using diagrams.
  • The UML is a graphical “language” for modeling computer programs. “Modeling” means to create a simplified representation of something, as a blueprint models a house.
  • It is a graphical notation used to construct and visualize object-oriented systems.

UMLclassandobject

 

Abstraction

  • Abstraction is the concept of hiding the complex implementation details and showing only the necessary features of an object.
  • Abstraction means hiding lower-level details and exposing only the essential and relevant details to the users.

Note: Abstraction is often achieved or implemented using abstract classes and methods(i.e. virtual function) in C++

Purpose: The primary purpose of abstraction is to handle complexity by hiding unnecessary details from the user and showing only the relevant features. This allows the user to interact with the object without needing to understand the intricate details of its implementation.

Examples: Abstraction

During driving a car, you only focus on essential features like

  • Gear handling
  • Steering handling
  • Applying brakes
  • Acceleration and applying clutch

Rather than knowing the internal reality or details like the working of the engine, internal wiring, etc.

 

 

The second example is an ATM Machine.

We perform different operations like Cash withdrawal, money transfer, deposit money, printing mini-statements, etc but you don’t know the internal process.

 

Source: https://in.pinterest.com/pin/10203536642764885/

 

Encapsulation

  • Encapsulation is the process of wrapping data (variables) and methods (functions) that operate on the data into a single unit, typically a class.
  • It restricts direct access to some of the object’s components, which is a means of preventing accidental interference and misuse of the data.

Purpose: The primary purpose of encapsulation is to ensure that the internal representation of an object is hidden from the outside. This helps in maintaining data integrity and security by allowing controlled access and modifications.

 

Note: In encapsulation, we declare a field as private in the class to prevent other class access and encapsulated data can be accessed using the public setter/getter method.

 

Key Differences

  • Encapsulation is about bundling the data and the methods that operate on the data into a single unit and controlling access to the data using access modifiers. It is focused on the implementation and internal workings of a class.
  • Abstraction is about hiding the complex reality while exposing only the necessary parts. It is focused on the design and what an object does rather than how it does it.

 

Polymorphism

  • Polymorphism means many forms.
  • Polymorphism is the ability for a message or data to be processed in more than one form.
  • This means that we can define or perform a single action in multiple ways.

 

Examples: Polymorphism

In the below figure, A person takes different roles like a man is a father, a husband, an employee, or a salesperson. This is known as the Polymorphism.

 

Source: edureka

 

Polymorphism can be implemented in C++ in two types:

  1. Compile-time Polymorphism (Static Binding): Achieved through function overloading and operator overloading.
  2. Runtime Polymorphism (Dynamic Binding): Achieved through inheritance and virtual functions.

 

Inheritance

  • Inheritance is the process in which one object or class acquires the properties and behavior of another class or object.
  • It supports the concept of classification.
  • Inheritance – IS-A relationship between a superclass and its subclasses.

 

Example

Inheritance

Explanation

Base Class: Vehicle

Imagine a base class called Vehicle. This class includes common properties and methods that are shared by all types of vehicles.

Properties of Vehicle:

  • make: The manufacturer of the vehicle (e.g., Toyota, Ford).
  • model: The specific model of the vehicle (e.g., Camry, Mustang).
  • year: The year the vehicle was manufactured.
  • color: The color of the vehicle.

Methods of Vehicle:

  • start(): Method to start the vehicle.
  • stop(): Method to stop the vehicle.

 

Derived Classes

Now, let’s create some derived classes that inherit from the Vehicle class.

Car Class

A Car is a type of vehicle, so it inherits from the Vehicle class. In addition to the properties and methods it inherits, it might have additional properties and methods specific to cars.

Additional Properties of Car:

  • numberOfDoors: The number of doors the car has (e.g., 2-door, 4-door).
  • trunkCapacity: The capacity of the car’s trunk.

Additional Methods of Car:

  • openTrunk(): Method to open the car’s trunk.

Motorcycle Class

A Motorcycle is another type of vehicle, so it also inherits from the Vehicle class. It has its own unique properties and methods.

Additional Properties of Motorcycle:

  • type: The type of motorcycle (e.g., cruiser, sportbike).
  • engineCapacity: The engine capacity of the motorcycle.

Additional Methods of Motorcycle:

  • popWheelie(): Method to perform a wheelie.

 

 


 

End of the lesson….enjoy learning

 

 

Student Ratings and Reviews

 

 

 

 

 

Submit a Review