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

OOP_in_Cpp

Object-Oriented Programming (OOP) is a programming paradigm that utilizes 'objects' to encapsulate data and behavior, aiming for intuitive and scalable software design. Key principles of OOP include encapsulation, inheritance, polymorphism, and abstraction, which are implemented in C++ through classes, access modifiers, and method overriding. C++ supports OOP features such as defining classes and objects, encapsulating data, inheriting properties, and implementing polymorphism and abstraction.

Uploaded by

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

OOP_in_Cpp

Object-Oriented Programming (OOP) is a programming paradigm that utilizes 'objects' to encapsulate data and behavior, aiming for intuitive and scalable software design. Key principles of OOP include encapsulation, inheritance, polymorphism, and abstraction, which are implemented in C++ through classes, access modifiers, and method overriding. C++ supports OOP features such as defining classes and objects, encapsulating data, inheriting properties, and implementing polymorphism and abstraction.

Uploaded by

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

What is OOP and OOP in C++

What is OOP?

Object-Oriented Programming (OOP) is a programming paradigm based on the

concept of "objects." Objects represent

real-world entities, and they encapsulate data (attributes) and behavior

(methods) into a single unit. The goal of OOP

is to make software design more intuitive, reusable, and scalable.

Key Principles of OOP:

1. Encapsulation: Bundling data and methods within a class to restrict

unauthorized access. This is achieved using

access modifiers like public, private, and protected.

2. Inheritance: Enables a class (child) to inherit properties and behavior from

another class (parent). It promotes

code reuse.

3. Polymorphism: Allows objects to take multiple forms. For example, a method

can have different implementations

in derived classes (method overriding) or accept different parameters

(method overloading).

4. Abstraction: Hides complex implementation details and shows only essential

features to the user. This is achieved

using abstract classes or interfaces.

OOP in C++
C++ is a popular language that supports Object-Oriented Programming. Below

are the main features of OOP in C++:

1. Classes and Objects:

- A class is a blueprint for creating objects. It defines attributes (variables)

and methods (functions) that

the objects will have.

- Example:

class Car {

public:

string brand;

int speed;

void drive() {

cout << "The car is driving." << endl;

};

- Creating an object:

Car myCar;

myCar.brand = "Toyota";

myCar.speed = 120;

myCar.drive();

2. Encapsulation:

- Example:

class Account {

private:
double balance;

public:

void setBalance(double amount) {

balance = amount;

double getBalance() {

return balance;

};

3. Inheritance:

- Example:

class Vehicle {

public:

void start() {

cout << "Vehicle started." << endl;

};

class Car : public Vehicle {

public:

void drive() {

cout << "Car is driving." << endl;

};

4. Polymorphism:
- Method Overriding:

class Animal {

public:

virtual void sound() {

cout << "Animal makes a sound." << endl;

};

class Dog : public Animal {

public:

void sound() override {

cout << "Dog barks." << endl;

};

5. Abstraction:

- Example:

class Shape {

public:

virtual void draw() = 0; // Pure virtual function

};

class Circle : public Shape {

public:

void draw() override {

cout << "Drawing a circle." << endl;

}
};

You might also like