Docsity

Object-Oriented Programming (OOP) in C++: Concepts and Advantages, Study notes of Computer Science

Toggle sidebar
bg1
What is opps
Before Object-Oriented Programming (OOPs), most programs used a
procedural approach, where the focus was on writing step-by-step functions.
This made it harder to manage and reuse code in large applications.
OOP in C++ was introduced to solve this problem by organizing code into
classes and objects, making programs easier to understand, reuse, and
maintain.
Key Features of OOP in C++:
Structures code into logical units (classes and objects)
Keeps related data and methods together (encapsulation)
Makes code modular, reusable and scalable
Prevents unauthorized access to data
Follows the DRY (Don't Repeat Yourself) principle
Characteristics of an OOP (Object Oriented
Programming)
The diagram below demonstrates the C++ OOP Concepts
1. Class
Aclass
is a user-defined blueprint or prototype from which objects are
created. It represents the set of properties or methods that are common to all
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Object-Oriented Programming (OOP) in C++: Concepts and Advantages and more Study notes Computer Science in PDF only on Docsity!

What is opps

Before Object-Oriented Programming (OOPs), most programs used a

procedural approach, where the focus was on writing step-by-step functions.

This made it harder to manage and reuse code in large applications.

OOP in C++ was introduced to solve this problem by organizing code into

classes and objects, making programs easier to understand, reuse, and

maintain.

Key Features of OOP in C++:

 Structures code into logical units (classes and objects)

 Keeps related data and methods together (encapsulation)

 Makes code modular, reusable and scalable

 Prevents unauthorized access to data

 Follows the DRY (Don't Repeat Yourself) principle

Characteristics of an OOP (Object Oriented

Programming)

The diagram below demonstrates the C++ OOP Concepts

1. Class

A class is a user-defined blueprint or prototype from which objects are

created. It represents the set of properties or methods that are common to all

objects of one type. Using classes, you can create multiple objects with the

same behavior instead of writing their code multiple times. In general, class

declarations in C++ can include these components.

 Access Specifiers: A class can have members defined as public, private,

or protected to control accessibility.

 Class Name: The class name should follow naming conventions, usually

starting with a capital letter.

 Body: The class body is enclosed with braces {} and defines data

members and member functions.

2. Object

An Object is a basic unit of Object-Oriented Programming that represents

real-life entities. A typical C++ program creates many objects, which interact

with each other by invoking methods. The objects are what perform your

code, they are the part of your code visible to the user. An object mainly

consists of:

 State: It is represented by the data members (attributes) of an object. It

also reflects the properties of an object.

 Member Function: A member function is a collection of statements that

perform some specific task and may return the result to the caller.

Employee emp("Geek", 10000.0f); emp.displayDetails(); return 0 ; }

Output

Employee: Geek Salary: 10000

3. Abstraction

Abstraction in C++ is the process of hiding the implementation details and

only showing the essential details or features to the user. It allows to focus

on what an object does rather than how it does it.

In C++ abstraction is achieved using abstract classes (classes that have at

least one pure virtual function).

#include using namespace std; // Abstract class Vehicle class Vehicle { public: // Abstract methods virtual void accelerate() = 0 ; // pure virtual function virtual void brake() = 0 ; void startEngine() { cout << "Engine started!" << endl; } }; class Car : public Vehicle { public: // Implement abstract methods void accelerate() override { cout << "Car: Pressing gas pedal..." << endl; }

void brake() override { cout << "Car: Applying brakes..." << endl; } }; int main() { // Create object using pointer to abstract class Vehicle* myCar = new Car(); myCar->startEngine(); myCar->accelerate(); myCar->brake(); delete myCar; return 0 ; }

Output

Engine started! Car: Pressing gas pedal... Car: Applying brakes...

4. Encapsulation

Encapsulation is defined as the process of wrapping data and the methods

into a single unit, typically a class. It is like a protective shield that prevents

the data from being accessed by the code outside the shield.

 Technically, in encapsulation the variables or the data in a class is hidden

from any other class and can be accessed only through any member

function of the class in which they are declared.

 In encapsulation, the data in a class is hidden from other classes, which is

similar to what data-hiding does.

 Encapsulation can be achieved by declaring all the variables in a class as

private and writing public methods in the class to set and get the values of

the variables.

// Getter methods int getId() { return id; } string getName() { return name; } }; int main() { Employee emp; // Using setters emp.setId( 101 ); emp.setName("Geek"); // Using getters cout << "Employee ID: " << emp.getId() << endl; cout << "Employee Name: " << emp.getName() << endl; return 0 ; }

Output

Employee ID: 101 Employee Name: Geek

5. Inheritance

Inheritance is an important pillar of OOP (Object Oriented Programming). It

is the mechanism in C++ by which one class is allowed to inherit the features

(data members and member functions) of another class. We achieve

inheritance by using the : symbol followed by an access specifier (public,

private, or protected). Inheritance is also known as an "is-a" relationship.

Example: Dog, Cat, Cow and be Derived Class of Animal Base Class.

Inheritance

Let us discuss some frequently used important terminologies:

 Superclass: The class whose features are inherited is known as

superclass (also known as base or parent class).

 Subclass: The class that inherits the other class is known as subclass

(also known as derived or extended or child class). The subclass can add

its own fields and methods in addition to the superclass fields and

methods.

 Reusability: Inheritance supports the concept of "reusability", i.e. when

we want to create a new class and there is already a class that includes

some of the code that we want, we can derive our new class from the

existing class.

#include using namespace std; // Superclass (Parent)

Types of Polymorphism

Polymorphism in C++ is mainly of 2 types as mentioned below:

 Function Overloading

 Function Overriding

Function Overloading and Function Overriding

1. Function Overloading : Also known as compile-time polymorphism, this

occurs when two or ore functions in the same class share the same name

but have different parameters lists (different number or types of parameters).

The return type of these functions may or may not be same.

2. Function Overriding : Also, known as run-time polymorphism, this occurs

when a function in the derived class has the same name, return type, and

parameters as a function in the base class. In C++, this is achieve using

virtual functions, so that the derived class provides its own implementation of

the method.

Below is the implementation of both the concepts:

#include using namespace std;

// Parent Class class Parent { public: // Overloaded method void func() { cout << "Parent.func()" << endl; } // Overloaded method virtual void func(int a) { cout << "Parent.func(int): " << a << endl; } }; // Child Class class Child : public Parent { public: // Overrides Parent.func(int) void func(int a) override { cout << "Child.func(int): " << a << endl; } }; int main() { Parent parent; Child child; Parent* polymorphicObj = new Child(); // Method Overloading (compile-time) parent.func(); parent.func( 10 ); // Method Overriding (runtime) child.func( 20 ); // Polymorphism in action polymorphicObj->func( 30 ); delete polymorphicObj; return 0 ; }

Output