Constructors and destructors in c++

Asked By 410 points N/A Posted on -
qa-featured

I am new to programming with OOP C++. And I have an issue understanding. I need to work on a program with Constructors and destructors. Guide me with the detail concept of Constructors and destructors in classes. Can someone guide me on how to do this with OOP C++?

SHARE
Best Answer by JBond
Best Answer
Best Answer
Answered By 0 points N/A #90534

Constructors and destructors in c++

qa-featured

Here is the definition of Constructor and its working.  A type of member function, that is automatically executed when an object of a class is created is known as “Constructor” of that class. The constructor has no return type and has the same name as that of class name. The constructor can work as a normal function but it cannot return any value. It is normally defined in classes to initialize data member.

Syntax Class_Name() { Constructor Body Here } You can check the code I am giving you, to check when the constructor will be called and what will be the effect. Class Hi { Private: Int n; Public: Hi() { cout<<”Constructor Created”<

Answered By 140 points N/A #90535

Constructors and destructors in c++

qa-featured

Waiting for the solution.

Answered By 410 points N/A #90537

Constructors and destructors in c++

qa-featured

The code you have given is very easy to understand. Thanks a lot. Guide me about the destructor and also define the difference between them.

Answered By 0 points N/A #90538

Constructors and destructors in c++

qa-featured

A type of member function, that is automatically executed when an object of a class is destroyed is known as “Destructor” of that class. The Destructor has no return type and has the same name as that of class name. The Destructor can work as a normal function but it cannot return any value. It also cannot accept any parameters. The Destructor name is preceded by tilde sign ~.

Syntax: The syntax of declaring destructor is as follows: ~class_Name() { cout<<”Destructor Called”<

Answered By 0 points N/A #90539

Constructors and destructors in c++

qa-featured

Constructor is a member function, used to create a data member of a class. Whenever a data member created, constructor called to assign the address of memory location. In constructor you can write a code that may be initialize a value to the data member. Destructor is a member function that is called, whenever you want to make the space or address of memory location free after destroying the data member.

Answered By 410 points N/A #90540

Constructors and destructors in c++

qa-featured

Guide me how the constructor will assign the memory location to the variable or data member of a class? And how destructor is used to de-allocate or make free the assigned memory location of data member, when it destroys?

 

Answered By 0 points N/A #90541

Constructors and destructors in c++

qa-featured

Constructor is like a member function of a class. All the member functions perform the functions or tasks on the data saved in memory locations. Constructor and destructor are special functions that are dedicated to allocation and de-allocation of memory locations on birth and on death of data members respectively.

Answered By 410 points N/A #90542

Constructors and destructors in c++

qa-featured

Thanks a lot JBond and MRobert. Thanks a lot for your considerations and help. It really works and I have solved the issue. Regards.

Related Questions