A class is one of the fundamental building blocks in object-oriented programming. It acts as a blueprint for creating objects (instances), defining their attributes (data) and behaviors (methods or functions). Classes allow you to encapsulate data and functions in a logical, organized structure, making your code more reusable, maintainable, and scalable.
Class Definition:
class keyword (in languages like C++, Python, Java, etc.).Object:
Access Modifiers:
Encapsulation:
Here’s a breakdown of the structure of a class in C++:
#include <iostream>
using namespace std;
// Class Definition
class Car {
private: // Private members (cannot be accessed directly outside the class)
string model;
int year;
public: // Public members (can be accessed from outside the class)
// Constructor to initialize an object
Car(string m, int y) {
model = m;
year = y;
}
// Method to display car information
void displayInfo() {
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
}
// Getter method for 'model'
string getModel() {
return model;
}
// Setter method for 'model'
void setModel(string m) {
model = m;
}
};
int main() {
// Create an object of the class 'Car'
Car myCar("Toyota Camry", 2020);
// Accessing a public method
myCar.displayInfo();
// Using getter and setter methods
cout << "Current Model: " << myCar.getModel() << endl;
myCar.setModel("Honda Civic");
cout << "Updated Model: " << myCar.getModel() << endl;
return 0;
}
Class Definition:
Car contains two private attributes (model, year) and several public methods.Car(string m, int y) initializes the model and year attributes when an object is created.displayInfo() method prints the car's details.getModel() and setModel() are getter and setter methods to access and modify the model attribute.Creating Objects:
main function, the object myCar is created using the constructor with the model "Toyota Camry" and year 2020.displayInfo() method is called to print the car's details.getModel() and setModel() methods are used to access and update the model of the car.Instance Methods:
Static Methods:
Example (C++):
class Car {
private:
static int carCount; // Static variable to keep track of the number of cars
public:
Car() {
carCount++;
}
static int getCarCount() { // Static method to access the static variable
return carCount;
}
};
int Car::carCount = 0; // Initialize static variable
int main() {
Car car1;
Car car2;
cout << "Total cars: " << Car::getCarCount() << endl; // Access static method
return 0;
}
Constructors and Destructors:
Example:
class Car {
private:
string model;
int year;
public:
// Constructor to initialize the Car object
Car(string m, int y) {
model = m;
year = y;
cout << "Car object created: " << model << endl;
}
// Destructor to clean up resources
~Car() {
cout << "Car object destroyed: " << model << endl;
}
};
int main() {
Car myCar("Tesla", 2023);
// The destructor will be automatically called when myCar goes out of scope
}
In OOP, inheritance allows you to create a new class based on an existing class. The new class (child or subclass) inherits the attributes and methods of the existing class (parent or superclass), but can also have its own additional attributes and methods.
Example (Inheritance in C++):
#include <iostream>
using namespace std;
// Base Class
class Vehicle {
public:
void start() {
cout << "Vehicle is starting." << endl;
}
};
// Derived Class
class Car : public Vehicle {
public:
void drive() {
cout << "Car is driving." << endl;
}
};
int main() {
Car myCar;
myCar.start(); // Inherited from Vehicle class
myCar.drive(); // Defined in Car class
return 0;
}
Car inherits from Vehicle and can use the start() method from the parent class, as well as its own drive() method.public keyword in class Car : public Vehicle indicates that the Vehicle class's public members are accessible to the Car class.Polymorphism allows objects of different classes to be treated as objects of a common superclass. It can be implemented in two ways:
Example (Runtime Polymorphism with Virtual Functions):
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() { // Virtual function for dynamic binding
cout << "Some generic animal sound" << endl;
}
};
class Dog : public Animal {
public:
void sound() override { // Overriding the base class method
cout << "Bark!" << endl;
}
};
int main() {
Animal* animal = new Dog(); // Pointer of base class type pointing to derived class object
animal->sound(); // Calls the Dog class's sound method (runtime polymorphism)
delete animal;
return 0;
}
sound() is a virtual function in the base class Animal. When it is overridden in the Dog class, the runtime polymorphism ensures that the sound() method of the Dog class is called, even though the pointer is of type Animal.A class in OOP is a powerful tool for organizing and structuring code in a way that mimics real-world entities and their interactions. By using classes, you can:
By adhering to these principles, classes enable developers to create modular, reusable, and maintainable code that is easier to manage and scale.
Open this section to load past papers