In Object-Oriented Programming (OOP), classes and objects are fundamental concepts. They form the core of OOP by allowing the modeling of real-world entities and behaviors within a program. Let’s go over both of these concepts in detail.
A class is essentially a blueprint or a template for creating objects. It defines a type of object that includes both data and methods that operate on that data. The class is a logical entity that describes the attributes and behaviors common to all objects of that type.
Data Members (Attributes):
Member Functions (Methods or Behaviors):
Access Modifiers:
Constructor:
Destructor:
#include <iostream>
using namespace std;
// Define the class
class Car {
private:
string brand; // Data member
int speed; // Data member
public:
// Constructor to initialize the object
Car(string b, int s) {
brand = b;
speed = s;
}
// Method to display car details
void displayDetails() {
cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl;
}
// Method to accelerate the car
void accelerate() {
speed += 10;
cout << "The car accelerates. New speed: " << speed << " km/h" << endl;
}
// Method to brake the car
void brake() {
speed -= 10;
cout << "The car slows down. New speed: " << speed << " km/h" << endl;
}
};
// Main function to create objects and call methods
int main() {
Car myCar("Toyota", 100); // Create an object of the class
myCar.displayDetails(); // Call method to display car details
myCar.accelerate(); // Call method to accelerate the car
myCar.brake(); // Call method to brake the car
return 0;
}
In the example above:
Car is the class.brand and speed are the data members.displayDetails(), accelerate(), and brake() are the member functions that define the behavior of the Car object.Car(string b, int s) initializes the Car object with specific brand and speed values when an object is created.An object is an instance of a class. When a class is defined, it serves as a template, and an object is created from that template. Objects hold the data (state) defined by the class and provide the behavior (methods) described by the class.
Instantiation:
Car myCar("Toyota", 100); creates an object myCar of type Car.State:
Behavior:
#include <iostream>
using namespace std;
class Book {
private:
string title;
string author;
public:
// Constructor to initialize Book object
Book(string t, string a) {
title = t;
author = a;
}
// Method to display book details
void displayInfo() {
cout << "Title: " << title << ", Author: " << author << endl;
}
};
int main() {
// Creating objects of the class Book
Book myBook1("1984", "George Orwell");
Book myBook2("To Kill a Mockingbird", "Harper Lee");
// Calling methods on objects
myBook1.displayInfo();
myBook2.displayInfo();
return 0;
}
In this example:
myBook1 and myBook2 are objects of type Book. They each have their own state (title and author).displayInfo() is called on both objects to display their details.Objects are created in two common ways:
Automatic (Local) Object:
Book myBook("1984", "George Orwell");Dynamic (Heap) Object:
new in C++), the object persists until it is explicitly destroyed using delete.Book* myBook = new Book("1984", "George Orwell");delete to free the dynamically allocated memory.Imagine modeling a Library System. You might have a Book class:
Book could define data members like title, author, ISBN, and methods like borrow() or returnBook().Book class could represent individual books in the library, each with its own title, author, and other attributes.Understanding the concepts of classes and objects is essential to mastering Object-Oriented Programming, as they allow for the organization of complex systems in a modular and reusable way.
Open this section to load past papers