Object-Oriented Programming Basics in C++

Photo by insung yoon on Unsplash

Object-Oriented Programming Basics in C++

Object-Oriented Programming (OOP) is a programming paradigm centered around the concept of objects. Objects are instances of classes, which act as blueprints. In OOP, code is organized around these objects, each with its own state and behavior.

Key Concepts

  • Class: A blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data.

  • Object: An instance of a class. Objects interact with one another and represent real-world entities.

#include <iostream>
using namespace std;

class Hero {
 public:
    string name;
    int power;
    char level;
    Hero() {
        name = "default-name";
        power = 0;
        level = 'A';
    }
};

int main() {
    Hero spiderMan;
    cout << spiderMan.name << endl;
    return 0;
}

Access Modifiers

Access modifiers control the visibility of class members:

  1. Public: Accessible from anywhere.

  2. Private: Accessible only within the class.

  3. Protected: Accessible within the class and by derived classes.

class Person {
 private:
    int age;
 public:
    void setAge(int a) { age = a; }
    int getAge() { return age; }
};

Constructors

Constructors are special member functions that initialize objects of a class. They have the same name as the class and do not return anything.

  • Default Constructor: No parameters.

  • Parameterized Constructor: Accepts parameters to initialize objects.

  • Copy Constructor: Initializes an object using another object of the same class.

class Hero {
 public:
    Hero() { cout << "A hero is born!" << endl; }
    Hero(string n, int p) : name(n), power(p) {}
    Hero(const Hero &h) { name = h.name; power = h.power; }
};

this Pointer

The this pointer holds the address of the current object. It is implicitly passed to non-static member functions.

class Hero {
 public:
    int power;
    Hero(int p) {
        this->power = p;
    }
};

Copy Mechanisms

Shallow Copy

Copies all member values. If the class contains pointers, the copied object points to the same memory location as the original object.

class Box {
 private:
    int* height;
 public:
    Box() { height = new int; }
    Box(const Box &b) { height = b.height; }
    ~Box() { delete height; }
};

Deep Copy

Creates a new memory allocation for dynamically allocated members, ensuring that changes to one object do not affect the other.

class Box {
 private:
    int* height;
 public:
    Box() { height = new int; }
    Box(const Box &b) { height = new int; *height = *(b.height); }
    ~Box() { delete height; }
};

Destructors

Destructors are special member functions that are invoked when an object is destroyed. They are used to free resources.

class Hero {
 public:
    ~Hero() { cout << "A hero is destroyed!" << endl; }
};

Static Members

Static Data Members

Static data members belong to the class rather than instances. All objects of the class share the same copy of the static data member.

class Test {
 public:
    static int x;
};
int Test::x = 10;

Static Member Functions

Static member functions can only access static data members. They are not associated with an object of the class.

class Test {
 public:
    static void setX(int a) { x = a; }
 private:
    static int x;
};

Summary

Object-Oriented Programming in C++ allows for code modularization, encapsulation, and reuse. Understanding the basics of classes, objects, access modifiers, constructors, destructors, and static members is crucial for mastering OOP in C++.

References

This blog serves as a comprehensive guide to understanding the fundamental concepts of OOP in C++, setting a strong foundation for more advanced topics in programming.