Search⌘ K
AI Features

Illustrate Construction of Object

Explore how C++ constructors work, focusing on their role in initializing objects after memory allocation. Understand that constructors do not allocate space but set up object values, demonstrated through the use of the this pointer and practical examples.

We'll cover the following...

Problem

Does the constructor actually “construct” an object? Can this be proven?

Coding solution

Here is a solution to the problem above.

C++
// Program to illustrate construction of object
#include <iostream>
class Sample
{
public :
Sample( )
{
std :: cout << "Address of object passed to this function = "
<< this << std :: endl ;
}
} ;
int main( )
{
Sample s ;
std :: cout << "Address of object s = " << &s << std :: endl ;
return 0 ;
}

Explanation

The constructor doesn’t allocate space for an object. ...