Chapter 26

Making Constructive Arguments

In This Chapter

arrow Creating and invoking a constructor with arguments

arrow Overloading the constructor

arrow Constructing data members with arguments

arrow Initializing data members with the declaration.

The Student class in Chapter 25 is extremely simple — almost unreasonably so. After all, a student has a name and a student ID as well as a grade-point average and other miscellaneous data. I choose GPA as the data to model in Chapter 25 because I know how to initialize it without someone telling me — I could just zero out this field. But I can’t just zero out the name and ID fields; a no-named student with a null ID probably does not represent a valid student. Somehow I need to pass arguments to the constructor to tell it how to initialize fields that start out with a value that’s not otherwise predictable. This chapter shows you how to pass arguments to the constructor.

Constructors with Arguments

C++ allows the program to define a constructor with arguments as shown here:

  class Student{  public:    Student(const char* pszNewName, int nNewID)    {        int ...

Get Beginning Programming with C++ For Dummies, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.