by Dinesh Thakur Category: Classes in C++

1: constructor is a member function of the class, with the name of the function being the same as the class name. It also specifies how the object should be initialized.

 

 Ways of calling constructor :

 

1) Implicitly : automatically by complier when an object is created.

2) Calling the constructors explicitly is possible, but it makes the code unverifiable.

 

2: class Point2D{

int x; int y;

public Point2D() : x(0) , y(0) {} //default (no argument) constructor

};

main(){

Point2D MyPoint; // Implicit Constructor call. In order to allocate memory on stack, the

default constructor is implicitly called.

Point2D * pPoint = new Point2D(); // Explicit Constructor call. In order to allocate memory

on HEAP we call the default constructor.

 



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content







Popular Article