class_instantiation


Class Instantiation

Class instantiation is a fundamental concept in object-oriented programming that involves creating an instance or object of a class. This process allows you to work with concrete objects based on the blueprint defined by the class.

Example 1: Basic Class Instantiation

#include <iostream>
#include <string>

class Person {
private:
    std::string name;
    int age;

public:
    Person(std::string n, int a) : name(n), age(a) {}

    void introduce() {
        std::cout << "My name is " << name << " and I'm " << age << " years old." << std::endl;
    }
};

int main() {
    Person alice("Alice", 30);
    alice.introduce();

    return 0;
}

Explanation

Example 2: Multiple Instantiations

#include <iostream>
#include <vector>

class Counter {
private:
    int count;

public:
    Counter() : count(0) {}

    void increment() { count++; }
    int getCount() const { return count; }
};

int main() {
    std::vector<Counter> counters(3);

    for (auto& counter : counters) {
        counter.increment();
        counter.increment();
    }

    for (size_t i = 0; i < counters.size(); ++i) {
        std::cout << "Counter " << i << ": " << counters[i].getCount() << std::endl;
    }

    return 0;
}

Explanation

Example 3: Dynamic Instantiation

#include <iostream>
#include <memory>

class DynamicObject {
private:
    int value;

public:
    DynamicObject(int v) : value(v) {
        std::cout << "Object created with value: " << value << std::endl;
    }

    ~DynamicObject() {
        std::cout << "Object destroyed with value: " << value << std::endl;
    }

    int getValue() const { return value; }
};

int main() {
    // Using raw pointer
    DynamicObject* rawPtr = new DynamicObject(42);
    std::cout << "Raw pointer value: " << rawPtr->getValue() << std::endl;
    delete rawPtr;

    // Using smart pointer
    std::unique_ptr<DynamicObject> smartPtr = std::make_unique<DynamicObject>(100);
    std::cout << "Smart pointer value: " << smartPtr->getValue() << std::endl;

    return 0;
}

Explanation

Summary

Class instantiation in C++ involves creating objects from class definitions. This process allocates memory for the object and initializes its attributes using constructors. Instantiation can be done statically (on the stack) or dynamically (on the heap). When working with dynamically allocated objects, it's important to manage memory properly, either by manually deleting raw pointers or using smart pointers for automatic memory management. Proper instantiation and management of objects are crucial for creating efficient and memory-safe C++ programs.

Citations:

[1] https://www.scaler.com/topics/instantiate-java/ [2] https://www.vaia.com/en-us/explanations/computer-science/computer-programming/instantiation/ [3] https://realpython.com/python-class-constructor/ [4] https://www.lenovo.com/us/en/glossary/instantiation/ [5] https://diveintopython.org/learn/classes/object-instantiation [6] https://javascript.plainenglish.io/classes-in-javascript-898bb2ad3bda?gi=60786969c921 [7] https://www.geeksforgeeks.org/different-ways-to-instantiate-an-object-in-c-with-examples/ [8] https://hayford.dev/what-happens-on-ruby-class-instantiation/

Related

Previous Page | Course Schedule | Course Content