Understanding Object-Oriented Programming in C++

C++, Object-Oriented Programming (OOP)

types of object oriented programming, except this time the prevalent method of organizing code will revolve around objects and not mere actions. It allows us to model real-world entities using Classes, and Objects where the code becomes more modularized, reusable & manageable reduce in size. One of the best languages to support OOP, C++, Object-Oriented Programming (OOP) that enables developers to write complex and efficient programs. Introduction This write-up touches on Object-Oriented Programming in C++It will cover its main type basic principles and features as well

Understanding Object-Oriented Programming in C++
Understanding Object-Oriented Programming in C++

1. Class and Object

At the core of OOP in C++ lies two important concepts: classes and objects.

Class:It is a template for the creating objects. Root: It separately goes to a datatype by finds the data and what operations can we perform on those datas which put into one for a unit. A class will usually contain attributes (data members) and functions to perform operations on the data, such as member functions or methods.

Object:** An object is an instance of a class.When a class is defined, no memory is allocated until an object of that class is created. Objects can represent real-world entities such as a car, a person, or a bank account.

`types of object oriented programming
class Car {
public:
string model;
int year;

void display() {
cout << “Model: ” << model << “, Year: ” << year << endl;
}
};

int main() {
Car myCar;
myCar.model = “Toyota”;
myCar.year = 2021;
myCar.display();

return 0;
}
“`

Inheritance

**Inheritance** is a key feature of types of object oriented programming that allows a new class (derived class) to inherit properties and behaviors from an existing class (base class). This promotes code reusability and establishes a natural hierarchy between classes C++, Object-Oriented Programming (OOP)

Single Inheritance: In this type, a derived class inherits from only one base class.
Multiple Inheritance: Here, a derived class can inherit from more than one base class.
Multilevel Inheritance: This involves a class being derived from another derived class, creating a multi-tier inheritance structure.
Hierarchical Inheritance: Multiple classes inherit from a single base class.
Hybrid Inheritance: This is a combination of two or more types of inheritance

“`cpp
class Vehicle {
public:
string brand = “Ford”;
};

class Car : public Vehicle {
public:
string model = “Mustang”;
};

int main() {
Car myCar;
cout << myCar.brand + ” ” + myCar.model;
return 0;
}
“`

Polymorphism

Polymorphism: means “many forms” and allows methods to do different things based on the object it is acting upon. It provides the ability to define a function in multiple forms.

Compile-time Polymorphism:** Also known as **static polymorphism**, it is achieved using function overloading and operator overloading.
Run-time Polymorphism: Also known as **dynamic polymorphism**, it is implemented using inheritance and virtual functions.

“`cpp
class Animal {
public:
void sound() {
cout << “This is an animal sound” << endl;
}
};

class Dog : public Animal {
public:
void sound() override { // Runtime Polymorphism
cout << “The dog barks” << endl;
}
};

int main() {
Animal *animal;
Dog myDog;
animal = &myDog;

animal->sound(); // Outputs: The dog barks
return 0;
}
“`

Encapsulation

Encapsulation: is the technique of bundling data and methods that operate on the data within a single unit or class. It restricts direct access to some of an object’s components, which is a means of preventing unintended interference and misuse of the data. Encapsulation is achieved through access specifiers in C++:

Private: The members declared as private are accessible only within the same class.
Protected:** The members declared as protected are accessible within the same class and derived classes.
Public: The members declared as public are accessible from any part of the program.

“`cpp
class Employee {
private:
int salary;

public:
void setSalary(int s) {
salary = s;
}

int getSalary() {
return salary;
}
};

int main() {
Employee emp;
emp.setSalary(50000);
cout << emp.getSalary();
return 0;
}
“`

Abstraction

Abstraction: involves hiding complex implementation details and showing only the essential features of an object. This is achieved in C++ through abstract classes and interfaces.

– **Abstract Class:** A class that cannot be instantiated and usually contains at least one pure virtual function.
– **Interface:** In C++, interfaces are not defined explicitly but can be implemented using abstract classes. They provide a contract that the derived classes must follow.

“`cpp
class Shape {
public:
virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape {
public:
void draw() override {
cout << “Drawing Circle” << endl;
}
};

int main() {
Shape *shape = new Circle();
shape->draw(); // Outputs: Drawing Circle
delete shape;
return 0;
}
“`Co

conclusion

Object-Oriented Programming in C++ offers a robust and organized way to write and manage code, promoting the concepts of modularity, reusability, and scalability. By understanding and effectively utilizing classes, inheritance, polymorphism, encapsulation, and abstraction, developers can create efficient and maintainable software systems. These OOP principles form the backbone of many modern programming languages and are essential knowledge for any C++ programmer.

Leave a Comment