C++ (Chapter 3 Constructor and Destructor)


Default Constructors

A default constructor is a constructor that takes no arguments.
If you write a class with no constructor at all, C++ will write a default constructor for you, one that does nothing
 A simple instantiation of a class (with no arguments) calls the default constructor: Rectangle r;

To create a constructor that takes arguments: -
 indicate parameters in prototype: Rectangle(double, double);
 - use parameters in the definition:
 Rectangle::Rectangle(double w, double len)
{ width = w; length = len; }

If all of a constructor's parameters have default arguments, then it is a default constructor. For example:
Rectangle::Rectangle(double w=0.0, double len=0.0)
{ width = w; length = len; }

- Creating an object and passing no arguments will cause this constructor to execute.
Rectangle r;

so jom main main sikit ngan C++ korang

Post a Comment

0 Comments