C++ Classes & Objects

Understanding object-oriented programming fundamentals

🏗️ What are Classes & Objects?

Classes are blueprints for creating objects in C++. Objects are instances of classes that contain data (attributes) and functions (methods) to work with that data.


// Simple class example
class Car {
public:
    string brand;
    string model;
    int year;
};

// Creating an object
Car myCar;
myCar.brand = "Toyota";
                                    

Output:

Car object created with brand: Toyota

Key OOP Concepts

📋

Class

Blueprint or template for objects

class Student {
    string name;
    int age;
};
🎯

Object

Instance of a class

Student student1;
Student student2;
📊

Attributes

Data members of a class

class Person {
    string name;
    int age;
};
⚙️

Methods

Functions inside a class

class Calculator {
    int add(int a, int b) {
        return a + b;
    }
};

🔹 Creating Your First Class

A simple class like Dog introduces core object-oriented concepts: attributes and methods. Attributes (e.g., name, breed, age) define state, while methods (e.g., bark(), displayInfo()) define behavior. This foundational exercise teaches encapsulation by bundling related data and functions into a single, reusable unit, forming the basis for more complex software design.

#include <iostream>
#include <string>
using namespace std;

class Dog {
public:
    // Attributes
    string name;
    string breed;
    int age;
    
    // Method
    void bark() {
        cout << name << " says Woof!" << endl;
    }
    
    void displayInfo() {
        cout << "Name: " << name << endl;
        cout << "Breed: " << breed << endl;
        cout << "Age: " << age << " years" << endl;
    }
};

Class Structure:

Dog class with attributes: name, breed, age
Methods: bark(), displayInfo()

🔹 Creating and Using Objects

Objects are instances of a class, created to use the defined blueprint in a program. For example, from a Dog class, you can instantiate objects like buddy and max, each with unique attribute values. Interacting with these objects through their methods brings the class to life, demonstrating how object-oriented programming models real-world entities and interactions.

int main() {
    // Create objects
    Dog dog1;
    Dog dog2;
    
    // Set attributes for dog1
    dog1.name = "Buddy";
    dog1.breed = "Golden Retriever";
    dog1.age = 3;
    
    // Set attributes for dog2
    dog2.name = "Max";
    dog2.breed = "German Shepherd";
    dog2.age = 5;
    
    // Use methods
    dog1.displayInfo();
    dog1.bark();
    
    cout << endl;
    
    dog2.displayInfo();
    dog2.bark();
    
    return 0;
}

Output:

Name: Buddy
Breed: Golden Retriever
Age: 3 years
Buddy says Woof!

Name: Max
Breed: German Shepherd
Age: 5 years
Max says Woof!

🔹 Complete Example

A self-contained example, such as a Rectangle class, showcases a class in its entirety. It includes attributes (length, width), a constructor for initialization, and methods to calculate area and perimeter. This complete picture illustrates how classes encapsulate data and operations, providing a template for creating multiple, independent objects that share common behavior.

#include <iostream>
#include <string>
using namespace std;

class Rectangle {
public:
    double width;
    double height;
    
    double calculateArea() {
        return width * height;
    }
    
    double calculatePerimeter() {
        return 2 * (width + height);
    }
    
    void displayInfo() {
        cout << "Rectangle: " << width << " x " << height << endl;
        cout << "Area: " << calculateArea() << endl;
        cout << "Perimeter: " << calculatePerimeter() << endl;
    }
};

int main() {
    Rectangle rect;
    rect.width = 5.0;
    rect.height = 3.0;
    
    rect.displayInfo();
    
    return 0;
}

Output:

Rectangle: 5 x 3
Area: 15
Perimeter: 16

🧠 Test Your Knowledge

What is an object in C++?