C++ Class

In C++, a class is a user-defined data type that serves as a blueprint or template for creating objects. It defines the properties (data members) and behaviors (member functions) that objects of that class will possess. The class acts as a container for related data and functions, providing a way to organize and structure code.

Here's the basic syntax for defining a class in C++:

Example
class ClassName {
public:
    // Data members (variables)
    dataType member1;
    dataType member2;
    // ...

    // Member functions (methods)
    returnType functionName(parameterList) {
        // Function body
    }
    // ...
};

Let's break down the components of a class:

  1. Class Name: The class name is a user-defined identifier that uniquely identifies the class. It follows the same naming rules as other identifiers in C++.

  2. Data Members: Data members are variables declared within the class. They represent the properties or attributes of objects created from the class. You can declare data members of any valid C++ data type. By default, data members are private, but you can specify different access specifiers to control their visibility and accessibility.

  3. Member Functions: Member functions are functions declared within the class. They define the behaviors or actions that objects of the class can perform. Member functions can access and manipulate the data members of the class. Like regular functions, member functions have a return type, a name, and optional parameters.

  4. Access Specifiers: C++ provides three access specifiers: public, private, and protected. These specifiers determine the accessibility of the class members. By default, class members are private, meaning they can only be accessed within the class itself. However, you can specify public or protected access to make them accessible outside the class.

Once a class is defined, you can create objects (instances) of that class by declaring variables of the class type. Each object has its own set of data members and can invoke the member functions defined within the class.

Here's an example that demonstrates the usage of a class in C++:

Example
class Circle {
private:
    double radius;

public:
    void setRadius(double r) {
        radius = r;
    }

    double calculateArea() {
        return 3.14 * radius * radius;
    }
};

int main() {
    Circle myCircle;  // Create an object of the Circle class

    myCircle.setRadius(5.0);  // Set the radius of the circle
    double area = myCircle.calculateArea();  // Calculate the area of the circle

    cout << "Area of the circle: " << area << endl;

    return 0;
}

In this example, we define a Circle class with a private data member radius and two public member functions setRadius() and calculateArea(). We create an object myCircle from the Circle class, set its radius using the setRadius() function, and calculate its area using the calculateArea() function.

Classes are a fundamental part of object-oriented programming in C++. They provide a mechanism for encapsulating data and behavior into reusable and modular units, promoting code organization, reusability, and maintainability.