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.
#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;
}
Person class with name and age attributes.main function, we instantiate a Person object named alice using the constructor.introduce method on the alice object.#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;
}
Counter class with a count attribute and methods to increment and get the count.main function, we create a vector of three Counter objects.#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;
}
DynamicObject class with a constructor and destructor that print messages.main, we demonstrate two ways of dynamically instantiating objects:new and deletestd::unique_ptr) with std::make_uniqueClass 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.
[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/