Classes and Objects

Classes and objects are fundamental concepts used to implement object-oriented programming (OOP). They provide a way to model real-world entities using data and behaviors. A class is a blueprint for creating objects, and an object is an instance of a class.

What is a Class?

A class in C++ defines a new data type that can encapsulate both data (member variables) and functions (member functions or methods) that operate on that data. It serves as a template to create objects with similar characteristics.

Basic Syntax of a Class

class ClassName {
public:
    // Public member variables and functions
    int publicVariable;
    void publicFunction();

private:
    // Private member variables and functions
    int privateVariable;
    void privateFunction();
};

In this example:

  • The class keyword is used to define a class named ClassName.
  • Classes can have public, private, and protected sections, which control the access levels for the class members.
  • Public members can be accessed from outside the class, while private members can only be accessed within the class itself. By default, all members are private if no access specifier is provided.

What is an Object?

An object is an instance of a class. When you create an object, you allocate memory for the class’s data members and can use its member functions.

Creating an Object

ClassName obj; // Creates an object of ClassName

Example of a Simple Class and Object

Let’s create a simple class named Car with some attributes and behaviors.

#include <iostream>
using namespace std;

// Define a class named Car
class Car {
public:
    // Attributes (member variables)
    string brand;
    string model;
    int year;

    // Behaviors (member functions)
    void displayInfo() {
        cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;
    }
};

int main() {
    // Create an object of the Car class
    Car myCar;

    // Access and set the object's attributes
    myCar.brand = "Toyota";
    myCar.model = "Camry";
    myCar.year = 2020;

    // Call the object's member function
    myCar.displayInfo();

    return 0;
}

In this example:

  • The Car class has three public member variables: brand, model, and year.
  • It also has a member function displayInfo() to print the details of the car.
  • An object myCar is created, and its attributes are set and accessed.

Access Specifiers

C++ provides three access specifiers to control access to class members:

  1. Public: Members declared as public can be accessed from outside the class.
  2. Private: Members declared as private can only be accessed within the class itself. This is the default access specifier if none is provided.
  3. Protected: Members declared as protected can be accessed by derived classes and within the class itself.

Example of Access Specifiers

#include <iostream>
using namespace std;

class Sample {
public:
    int publicVar; // Accessible from anywhere

private:
    int privateVar; // Accessible only within the class

protected:
    int protectedVar; // Accessible within the class and derived classes
};

int main() {
    Sample obj;
    obj.publicVar = 10; // Allowed, as publicVar is public
    // obj.privateVar = 20; // Error, privateVar is private
    // obj.protectedVar = 30; // Error, protectedVar is protected

    return 0;
}