Techniques of Initializing a Class

There are various techniques used to initialize a class: initializing individual members or initializing the class as a whole.

To initialize a member of a class, access it and assign it an appropriate value.

This time, the program would render a reasonable result.

You can also initialize an object as a variable. This time, type the name of the variable followed by the assignment operator, followed by the desired values of the variables listed between an opening and a closing curly brackets; each value is separated with a comma. The first rule you must keep in mind is that the list of variables must follow the order of the declared members of the class. The second rule you must observe is that none of the members of the class must be another class. In the following example, one of the members of the class is another class, namely a string. Because of the way a variable of the class is declared, the program would not compile: