C++ class Keyword

The class keyword in C++ is used to define user-defined data types called classes. A class is a blueprint for creating objects, encapsulating data members (attributes) and member functions (methods) that operate on the data. It is a fundamental concept in object-oriented programming (OOP) that enables modular and reusable code.

Classes support features like encapsulation, inheritance, and polymorphism, which are core principles of OOP.

In this tutorial, we will explain how to use the class keyword in C++ with syntax, examples, and detailed explanations.


Syntax

</>
Copy
class ClassName {
public:
    // Public members
    data_type attribute_name;
    return_type method_name(parameter_list);

private:
    // Private members
    data_type attribute_name;
    return_type method_name(parameter_list);
};
class
The keyword used to define a class.
ClassName
The name of the class. It should follow standard naming conventions.
public
Specifies that the members are accessible from outside the class.
private
Specifies that the members are accessible only within the class. This is the default access specifier.

Examples

Example 1: Basic Class with Public Members

This example demonstrates a simple class with public data members and a method.

</>
Copy
#include <iostream>
using namespace std;

class Rectangle {
public:
    int width;
    int height;

    int area() {
        return width * height;
    }
};

int main() {
    Rectangle rect;
    rect.width = 5;
    rect.height = 10;

    cout << "Area: " << rect.area() << endl;
    return 0;
}

Output:

Area: 50

Explanation:

  1. The Rectangle class has two public attributes, width and height, and a public method, area().
  2. In the main() function, an object rect of type Rectangle is created.
  3. The attributes width and height are assigned values, and the area() method is called to calculate the area.
  4. The calculated area, 50, is printed to the console.

Example 2: Class with Private Members

This example demonstrates how to use private members in a class and access them through public methods.

</>
Copy
#include <iostream>
using namespace std;

class Circle {
private:
    double radius;

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

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

int main() {
    Circle c;
    c.setRadius(5.0);

    cout << "Area: " << c.getArea() << endl;
    return 0;
}

Output:

Area: 78.5

Explanation:

  1. The Circle class has a private attribute, radius, which cannot be accessed directly from outside the class.
  2. Public methods setRadius() and getArea() are provided to set the radius and calculate the area, respectively.
  3. In the main() function, the setRadius() method is used to set the radius of the circle.
  4. The getArea() method calculates the area, which is printed to the console as 78.5.

Key Points about class Keyword

  1. The class keyword is used to define a class in C++.
  2. A class encapsulates data members and member functions into a single entity.
  3. Access specifiers (public, private, protected) control the accessibility of members.
  4. Classes support OOP features such as inheritance, polymorphism, and abstraction.
  5. Objects are instances of a class that operate on the class’s attributes and methods.