Ticker

6/recent/ticker-posts

C++ OOPs Concepts


C++ OOPs Concepts: Class and Objects, Abstraction, Encapsulation, Polymorphism, and Inheritance

Introduction to Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that revolves around the concept of "objects," which are instances of classes. OOP allows you to model real-world entities as objects, organizing data and behaviors into logical units. In C++, OOP is an essential part of the language, providing powerful features like Class and Objects, Abstraction, Encapsulation, Polymorphism, and Inheritance.

1. Class and Objects

Class:

A class in C++ is a user-defined data type that acts as a blueprint for creating objects. It defines the properties and behaviors that the objects of that class will possess. A class can contain attributes (data members) and methods (member functions).

Code Example:

cpp
class Rectangle {
public:
int width;
int height;

void calculateArea() {
int area = width * height;
cout << "Area of the rectangle: " << area << endl;
}
};

Objects:

Objects are instances of a class, representing individual entities based on the class blueprint. They have access to the attributes and methods defined in the class.

Code Example:

cpp
int main() {
Rectangle rect1; // Creating an object 'rect1' of class Rectangle
rect1.width = 5;
rect1.height = 10;
rect1.calculateArea(); // Output: Area of the rectangle: 50
return 0;
}

2. Abstraction

Abstraction is the process of hiding the implementation details of an object and exposing only the essential features to the outside world. It allows developers to work with high-level concepts rather than dealing with low-level implementation.

In C++, abstraction is achieved using abstract classes and pure virtual functions.

Code Example:

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

class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle." << endl;
}
};

3. Encapsulation

Encapsulation is the practice of bundling data and the methods that operate on that data within a single unit (i.e., the class). It helps in data hiding and prevents direct access to the class's internal representation.

Code Example:

cpp
class BankAccount {
private:
double balance;

public:
void deposit(double amount) {
balance += amount;
}

double getBalance() {
return balance;
}
};

4. Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common base class during runtime. It enables flexibility in handling different data types through a uniform interface.

Code Example:

cpp
class Animal {
public:
virtual void makeSound() {
cout << "Animal makes a sound." << endl;
}
};

class Dog : public Animal {
public:
void makeSound() {
cout << "Dog barks." << endl;
}
};

class Cat : public Animal {
public:
void makeSound() {
cout << "Cat meows." << endl;
}
};

5. Inheritance

Inheritance allows a class (subclass/derived class) to inherit properties and behaviors from another class (superclass/base class). It promotes code reuse and hierarchical organization of classes.

Code Example:

cpp
class Vehicle {
protected:
string brand;

public:
void setBrand(string _brand) {
brand = _brand;
}
};

class Car : public Vehicle {
private:
int numWheels;

public:
void setNumWheels(int num) {
numWheels = num;
}
};

These fundamental concepts of Object-Oriented Programming in C++ form the backbone of writing efficient and organized code. Properly using these concepts can lead to better code design and maintainability.


      Post a Comment

      0 Comments