C++ Object-Oriented Programming Basics | Classes, Objects, Constructors and this Pointer


This complete tutorial on C++ Object-Oriented Programming Basics explains the core concepts of object-oriented programming using C++. It covers classes and objects, access specifiers, constructors and destructors, the this pointer, and static data members and functions. The tutorial follows best coding practices and helps beginners build a strong foundation for advanced C++ topics and real-world software development.

Object-Oriented Programming Basics – Complete Tutorial

1. Class and Object

A class is a blueprint that defines properties (data members) and behaviors (member functions).

An object is an instance of a class.

Example:


#include <iostream>
using namespace std;

class Student {
public:
int id;
string name;

void display() {
cout << id << " " << name << endl;
}
};

int main() {
Student s1;
s1.id = 101;
s1.name = "Rahul";
s1.display();
}

Best Practices:

  1. Keep data members private
  2. Expose behavior through public functions

2. Access Specifiers

Access specifiers control the visibility of class members.

SpecifierAccessibility
publicAccessible everywhere
privateAccessible only inside class
protectedAccessible in class and derived classes

Example:


class Employee {
private:
int salary;

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

int getSalary() {
return salary;
}
};

3. Constructors and Destructors

Constructor

A special function that initializes objects.

Example:


class Car {
public:
Car() {
cout << "Constructor called" << endl;
}
};

Destructor

A special function that cleans up resources when an object is destroyed.

Example:


class Car {
public:
~Car() {
cout << "Destructor called" << endl;
}
};

Best Practices:

  1. Use constructors to initialize all data members
  2. Release resources in destructors

4. this Pointer

The this pointer holds the address of the current object.

Example:


class Person {
int age;
public:
void setAge(int age) {
this->age = age;
}
};

Why use this?

  1. Resolves naming conflicts
  2. Refers to current object instance

5. Static Data Members and Functions

Static Data Members

Shared among all objects of a class.

Example:


class Counter {
public:
static int count;
};

int Counter::count = 0;

Static Member Functions

Can access only static members.

Example:


class MathUtils {
public:
static int add(int a, int b) {
return a + b;
}
};

Usage:


cout << MathUtils::add(5, 10);

Best Practices:

  1. Use static members for shared data
  2. Call static functions using class name

Common Mistakes

  1. Making all members public
  2. Forgetting to define static members outside the class
  3. Overusing global variables instead of static members

Summary

In this chapter, you learned the fundamentals of object-oriented programming in C++, including classes, objects, access specifiers, constructors, destructors, the this pointer, and static members. These concepts form the backbone of advanced C++ programming and software design.