Geek Slack

Learn C++
    About Lesson


    C++ Classes and Objects


    C++ Classes and Objects

    1. Class Declaration and Object Creation

    Example of defining a class and creating objects:

    #include <iostream>
    #include <string>
    
    // Class declaration
    class Car {
    private:
        std::string brand;
        int year;
    
    public:
        // Constructor
        Car(std::string b, int y) {
            brand = b;
            year = y;
        }
    
        // Member function
        void displayInfo() {
            std::cout << "Brand: " << brand << ", Year: " << year << std::endl;
        }
    };
    
    int main() {
        // Create objects of class Car
        Car car1("Toyota", 2020);
        Car car2("BMW", 2022);
    
        // Call member function on objects
        car1.displayInfo();
        car2.displayInfo();
    
        return 0;
    }

    2. Access Specifiers: Public and Private

    Example demonstrating private and public access specifiers:

    #include <iostream>
    
    // Class declaration
    class Rectangle {
    private:
        double length;
        double width;
    
    public:
        // Constructor
        Rectangle(double l, double w) {
            length = l;
            width = w;
        }
    
        // Public member function to calculate area
        double calculateArea() {
            return length * width;
        }
    
        // Public member function to set dimensions
        void setDimensions(double l, double w) {
            length = l;
            width = w;
        }
    };
    
    int main() {
        // Create object of class Rectangle
        Rectangle rect(5.0, 3.0);
    
        // Calculate and display area
        std::cout << "Area of rectangle: " << rect.calculateArea() << std::endl;
    
        // Update dimensions and calculate area again
        rect.setDimensions(7.0, 4.0);
        std::cout << "Updated area of rectangle: " << rect.calculateArea() << std::endl;
    
        return 0;
    }

    3. Constructors and Destructors

    Example of constructors and destructor in a class:

    #include <iostream>
    #include <string>
    
    // Class declaration
    class Person {
    private:
        std::string name;
        int age;
    
    public:
        // Constructor
        Person(std::string n, int a) : name(n), age(a) {
            std::cout << "Constructor called for " << name << std::endl;
        }
    
        // Destructor
        ~Person() {
            std::cout << "Destructor called for " << name << std::endl;
        }
    
        // Member function
        void displayInfo() {
            std::cout << "Name: " << name << ", Age: " << age << std::endl;
        }
    };
    
    int main() {
        // Create objects of class Person
        Person person1("Alice", 30);
        Person person2("Bob", 25);
    
        // Call member function on objects
        person1.displayInfo();
        person2.displayInfo();
    
        return 0;
    }

    Conclusion

    Classes and objects in C++ provide a powerful mechanism for organizing and structuring code into reusable components. This chapter covered the basics of defining classes, creating objects, using constructors and destructors, and utilizing access specifiers. Understanding classes and objects allows you to implement encapsulation, abstraction, and modular design principles in C++ programming.