C + + is a widely used programming language that supports object-oriented programming (OOP). In C + +, a class is a blueprint for an object, defining its properties and methods. By creating instances of classes, we can create objects that can have properties and methods defined by the class. The core idea of object-oriented programming is to encapsulate data and the method of manipulating data, making the code more modular, reusable and easy to maintain.
For example, we can represent a student with a simple class:
```cpp
class Student {
public:
//Attributes
string name;
int age;
//Constructor
Student(string n, int a) : name(n), age(a) {}
//method
void setName(string n) { name = n; }
void setAge(int a) { age = a; }
string getName() const { return name; }
int getAge() const { return age; }
};
```
In this example, `Student` is a class with two properties (`name` and `age`) and a constructor. We also define some methods (`setName`, `setAge`, `getName` and `getAge`) for setting and getting the value of the property.
2024-12-03 17:25:11
author: shark-toolz
65 views