Classes and Objects - Rybd04/2143-OOP GitHub Wiki
Definitions
Class
A class is a blueprint or template for creating objects. It defines a set of attributes and methods that are then used to create objects
Object
An object is an instance of a class. It represents an implementation of a class with values assigned to attributes.
Explanation
A class is like a blueprint for a house with objects being the actual houses. Each house will have the same structure but they can have different attributes.
Basic Code Example
// Define a class
class Car {
public:
// Instance variables
string brand;
int year;
// Method to display car info
void displayInfo() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
// Create an object of the class
Car car1;
// Assign values to instance variables
car1.brand = "Toyota";
car1.year = 2022;
// Call method using the object
car1.displayInfo();
return 0;
}