How to Create a Class with Constructors and Destructors in C++? Last Updated : 27 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a class is a user-defined data type encapsulating data members and member functions. The constructors and destructors are special member functions of a class used for initializing objects and cleaning up resources respectively. In this article, we will discuss how to create a class with constructors and destructors in C++. Create a Class with Constructors and Destructors in C++In C++, the compiler automatically creates a default constructor and a destructor for a class. But we can also define our own constructor and destructor to customize the behavior of our class. ApproachDefine Constructor: Declare the constructor with the same name as the class without any return type. Add the parameters if required. Define this constructor inside its body.Define Destructor: Declare the destructor with a class name preceded by a tilde (~). The Destructors do not accept parameters and have no return type. But can still define it inside its body.Syntax to Create Constructors and Destructors// ConstructorClassName() { // Constructor logic}// Destructor implementation~ClassName() { // Destructor logic}C++ Program to Create a Class with Constructors and Destructors C++ // C++ Program to illustrate how to Create a Class with // Constructors and Destructors #include <iostream> using namespace std; class GFG { public: // Default Constructor GFG() { cout << "Constructor called" << endl; } // Destructor ~GFG() { cout << "Destructor called" << endl; } }; int main() { // Creating an object of MyClass GFG obj; // Object goes out of scope // destructor is called return 0; } OutputConstructor called Destructor called Comment More infoAdvertise with us Next Article How to Implement a Copy Constructor in a Derived Class in C++ S subramanyasmgm Follow Improve Article Tags : C++ Programs C++ cpp-class CPP Examples Practice Tags : CPP Similar Reads How to Declare a Copy Constructor in a Derived Class? In C++, a copy constructor is a constructor that initializes an object as a copy of an existing object of the same class. In this article, we will learn how to declare a copy constructor in a derived class. Declaring a Copy Constructor in a Derived ClassWe can declare a copy constructor in a derived 3 min read How to Implement a Copy Constructor in a Derived Class in C++ In object-oriented programming, a copy constructor is a special member function that initializes a new object as a copy of an existing object. In this article, we will learn how to implement a copy constructor in a derived class. Implementing Copy Constructor in a Derived Class in C++ In C++, when w 3 min read How to Create a Derived Class from a Base Class in C++? In C++, one of the fundamental concepts of Object Oriented Programming (OOPS) is inheritance, allowing users to create new classes based on existing classes. The class that inherits another class is called derived class and the class that is inherited is called base class. In this article, we will l 2 min read How to create a custom String class in C++ with basic functionalities In this article, we will create our custom string class which will have the same functionality as the existing string class.The string class has the following basic functionalities: Constructor with no arguments: This allocates the storage for the string object in the heap and assign the value as a 8 min read How to Define the Default Constructor in C++? In C++, a constructor that takes no parameters is called a default constructor. A default constructor gets automatically invoked when an object of a class is created without any arguments. It is mainly used to initialize legal initial values to the member variables or perform other setup tasks. In t 2 min read How to Create a Class with Private and Public Members in C++? In C++, the classes are blueprints for creating objects with specific properties and methods that provide a feature of access specifiers to the user through which they can control the access of the data members present in a class. In this article, we will learn how to create a class with private and 3 min read Like