0
class
Can someone explain to me what is class in c++?
2 Réponses
+ 4
Class is a fundamental concept of object-oriented programming. In short, it contains data (fields) and behavior (methods). This topic is covered in the intermediate C++ course in great detail.
0
In C++, a class is a blueprint for creating objects. It defines the properties and behaviors that objects of that class will have. Essentially, a class encapsulates data (attributes) and functions (methods) that operate on that data.
Here's a basic example of a class in C++:
```cpp
// Define a class called 'Car'
class Car {
public:
// Attributes (data members)
string brand;
string model;
int year;
// Methods (member functions)
void displayInfo() {
cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;
}
};
```
In this example, the `Car` class has three attributes (`brand`, `model`, and `year`) to represent properties of a car, and one method (`displayInfo()`) to display information about the car.
To create objects (instances) of the `Car` class, you can do the following:
```cpp
int main() {
// Create an object of the Car class
Car myCar;
// Set values for the attributes
myCar.brand = "Toyota";
myCar.model = "Corolla";