1. Home
  2. Computing & Technology
  3. C / C++ / C#

Learn about C++ Classes and Objects

By David Bolton, About.com

8 of 9

Learn about C++ Constructors

Constructors

A constructor is a function that initilizes the members of an object. A constructor only knows how to build an object of its own class.

Constructors aren't automatically inherited between base and derived classes. If you don't supply one in the derived class, a default will be provided but this may not do what you want.

If no constructor is supplied then a default one is created by the compiler without any parameters. There must always be a constructor, even if it is the default and empty. If you supply a constructor with parameters then a default will NOT be created.

Some points about constructors

  • Constructors are just functions with the same name as the class.
  • Constructors are intended to initialize the members of the class when an instance of that class is created.
  • Constructors are not called directly (except through initializer lists)
  • Constructors are never virtual.
  • Multiple constructors for the same class can be defined. They must have different parameters to distinguish them.
There is a lot more to learn about constructors, for instance default constructors, assignment and copy constructors and these will be discussed in the next lesson.

On the next page : Don't forget destructors!

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. Learn about C++ Constructors

©2009 About.com, a part of The New York Times Company.

All rights reserved.