Chapter 3.5

Traps and Tips

AmazonEven though I have given you an overview of C++ Class' features there are things you need to familiarize yourself with to avoid falling victim to them. Here is a list of tips related to C++ Class that you should be aware of:

All members of a class—data and methods—are private by default.

C++ automatically provides the following member functions for a class: a default constructor, a copy constructor, a default destructor, an assignment operator, and an address operator. Therefore, you can use them without defining them.

If you do not define any constructor, you can use the default constructor in instantiating a class because compiler automatically creates it for you. If, however, you define a constructor with a set of arguments, then the compiler will not create a default constructor for you. You need to define a default constructor explicitly if you want to use it.

If you want to create an array of objects of a class, the class must have a default constructor.

A class is almost exactly the same as a struct. You can define constructors, member functions, and so on, inside a struct just like a class. The only difference I can see is that the default context of data members of a class is private and that of a struct is public.