Classes
Classes
In C++, a class is like a blueprint or a template for creating objects. Think of it as a recipe that
tells you what ingredients (data) and steps (functions) are needed to create something. In
programming, these "ingredients" are called attributes (or data members), and the "steps" are
called methods (or member functions).
class Car {
// Attributes (also called data members)
string brand;
string model;
int year;
public:
// Constructor (a special method to initialize objects)
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
}
// Method to display car details
void displayDetails() {
cout << "Brand: " << brand << "\nModel: " << model << "\nYear: " << year << endl;
}
};
Breaking It Down
1. Class Declaration:
o We start by using the class keyword followed by the class name, which is Car in
this case. The class name should start with an uppercase letter by convention.
2. Attributes:
o Inside the class, we declare the attributes. Here, brand, model, and year are
attributes of the Car class.
o These attributes are declared as private by default, meaning they cannot be
accessed directly from outside the class.
3. Constructor:
• A constructor is a special method with the same name as the class. It initializes the
attributes when an object is created.
• In this example, the constructor Car(string b, string m, int y) sets the brand, model, and
year attributes using the values passed to it.
4. Methods:
• The displayDetails method is a public function that prints out the details of the car. The
public keyword means this method can be accessed from outside the class.
• Once you have defined a class, you can create objects from it. Each object is an instance
of the class, with its own set of attributes.
• Here’s how you can create and use objects of the Car class:
Types of classes:
We come across many different types of classes
• Abstract
• Sealed
• Static
Abstract Class
An abstract class is a class that cannot be instantiated directly. It contains at least one pure
virtual function (a function with no implementation), which must be implemented in a derived
class.
#include <iostream>
// Abstract class
class Animal {
public:
};
public:
};
int main() {
Dog dog;
return 0;
2. Sealed Class
A sealed class is a class that cannot be inherited from. In C++, this can be done using the final
specifier.
Example:
#include <iostream>
class Vehicle final { // 'final' prevents inheritance
public:
void drive() {
std::cout << "Driving the vehicle!" << std::endl;
}
};
int main() {
Vehicle vehicle;
vehicle.drive(); // Output: Driving the vehicle!
return 0;
}
• Key Property: Cannot be inherited by any other class.
• Purpose: To stop further inheritance and modifications from other classes.
Static Class
You can create a class where all members are static, meaning the class is not intended to be
instantiated, and its members belong to the class itself rather than an object.
#include <iostream>
class MathHelper {
public:
static int add(int a, int b) {
return a + b;
}
static int multiply(int a, int b) {
return a * b;
}
};
int main() {
// No need to create an instance of MathHelper
std::cout << "Addition: " << MathHelper::add(3, 4) << std::endl; // Output: 7
std::cout << "Multiplication: " << MathHelper::multiply(3, 4) << std::endl; // Output: 12
return 0;
}
• Key Property: All members are static, meaning they can be accessed without creating an
instance of the class.
• Purpose: To group utility functions or data that belong to the class rather than individual
objects.