answersLogoWhite

0

Object initialisation is taken care of by the object's constructor(s). Initialisation is generally done during the initialisation stage of the constructor (immediately before the body of the constructor). Complex members such as pointers can be initialised to NULL in the initialisation stage. If new memory must be allocated to a member pointer, do it in the body of the constructor. Remember to clean up all allocated memory in the class destructor.

class cMyClass

{

public: // Construction.

cMyClass(); // Default constructor.

cMyClass(const cMyClass & MyClass); // Copy constructor.

cMyClass(const int i); // Overloaded constructor.

~cMyClass(); // Destructor.

private: // Member variables.

int m_int; // An integer.

int * m_pInt; // A pointer to an integer.

};

// Default constructor.

cMyClass::cMyClass(): // Colon indicates initialisation stage.

m_int( 0 ), // Comma-separated list of members to be initialised.

m_pInt( NULL ) // Optional; m_pInt will be initialised in the body.

{

// Body of the constructor:

// m_int is currently initialised to the value zero.

// m_pInt currently points to NULL.

m_pInt = new int;

*m_pInt = 0;

// m_pInt now points to new memory containing the value zero.

// Initialisation is complete!

}

// Copy constructor

cMyClass::cMyClass(const cMyClass & MyClass): // Initialisation

m_int( MyClass.m_int ), // Member list

m_pInt( NULL )

{

// m_int is initialised to the value of MyClass.m_int

// m_pInt currently points to NULL.

m_pInt = new int; // Allocate new memory.

*m_pInt = *MyClass.m_pInt; // Deep-copy the value.

// m_pInt now points to a copy of the value pointed to by MyClass.m_pInt.

// Initialisation is complete!

}

// Overloaded constructor

cMyClass::cMyClass(const int i): // Initialisation.

m_int( i ), // Member list.

m_pInt( NULL )

{

// m_int is initialised to the value of i.

// m_pInt currently points to NULL.

m_pInt = new int;

*m_pInt = 0;

// m_pInt now points to new memory containing the value zero.

// Initialisation is complete!

}

// Destructor. cMyClass::~cMyClass()

{

// Clean up allocated memory.

if( m_pInt ) delete m_pInt;

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Related Questions

What function will be called by default to initialize the member variables of the object of the class?

The constructor. It's run each time a new object is created, usually setup to initialize member variables, but it can do most anything.


How do you initialize variable?

initialize simple types: int i = 0; initialize objects: Object o = null; (in java)


What is copy constructor in object oriented programming?

The purpose of constructor in object oriented programming is to initialize data. Likewise copy constructors are used to initialize an object with data from another object.


Can you initialize an object without constructor in java?

No. if you wish to create an object that you plan on using in a java program then the answer is NO. You cannot initialize an object of a Java class without calling the constructor.


Is object a member of a class?

Yes, you would need to define your variables. Also initialize them


What is the difference between initialize of elements in a class with the use of constructor and without use of it?

when initializing a class elements by using constructor it will assigned to the elements when object creation is going on. by using other ways the elements will be initialize with default values when object creation


What is enables to test sound and graphics output?

[object Object]


What is a net or resultant force that enables an object to accelerate?

The net force that enables an object to accelerate is a force greater than zero.


How care enables individual to make choices is provided?

[object Object]


Why you use constructor instead of function?

For every class an empty constructor will be defined automatically by default unless you provide a constructor definition manually. Constructor in a class can be used to initialize variables or perfrom some basic functionallity whenever an object is created.


What is the role of the constructor?

Several meanings for "constructor". In object-oriented programming, it is the subroutine used to create an object. The programmer can add commands here, to get the object ready for his needs; for example, initialize the variables used in the object.


Is the color of object with in object itself?

The color of an object is a result of how that object interacts with light. Objects reflect, absorb, and transmit certain wavelengths of light, which our eyes perceive as color. The color is not within the object itself, but rather is a perception created by our visual system based on the light interacting with the object.