{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lect9_2 - Programmer Defined Types Classes and Constructors...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Programmer Defined Types - Classes and Constructors 1 Programmer Defined Types - Classes and Constructors When we declare an object, we often want to initialize some or all the data members of the object. To do this, the class definition requires special member functions called constructors . We redesign our class RationalNum so that it includes constructor functions. Our earlier version of the RationalNum class definition is reproduced below. class RationalNum { private: // data member declarations int numer; int denom; public: // member function declarations void setRationalNum(int, int); void outputRationalNum(); int getNumer(); int getDenom(); }; // end of class RationalNum // member function definitions int RationalNum::getNumer() { return numer; } // end getNumer int RationalNum::getDenom() { return denom; } // end getDenom void RationalNum::setRationalNum(int n, int d) { numer = n; denom = d; } // end setRationalNum void RationalNum::outputRationalNum() { cout << numer <<" / "<< denom << endl; } // end outputRationalNum
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Programmer Defined Types - Classes and Constructors 2 Consider the following code segment that uses RationalNum objects. RationalNum a; // <--- (1) a.outputRationalNum(); // outputs some irrelevant values The declaration in line (1) creates the object a . However, a.numer and a.denom are not initialized with any values. Thus, the call a.outputRationalNum() outputs some irrelevant values for a.numer and a.denom . Since the data values of an uninitialized object are meaningless, we have to set the object’s data members with meaningful values before we use the object. In our example case we can set the values as follows. RationalNum a; a.setRationalNum(9, 15); a.outputRationalNum(); // outputs 9/15 There is a better way to do this. We can add one or more constructor functions to our class. A constructor is a member function that is automatically called when an object of that class is created. A constructor can be used to initialize the data members and to do any other tasks that may be needed. We can define a constructor the same way that we define any other member function. However, a constructor function must satisfy the following two requirements. l. A constructor must have the same name as the class. For example, if the class is named RationalNum , then any constructor of this class must be named RationalNum . 2. A constructor function cannot return a value. Moreover, no return type, not even void , can be given in the function declaration. For example, suppose that each time a RationalNum object is created using the statement such as RationalNum a; we want to initialize a.numer = 0 and a.denom = 1 . We can accomplish this by adding a constructor function as a public member to our RationalNum class defini- tion. The constructor is shown below.
Background image of page 2
Programmer Defined Types - Classes and Constructors 3 RationalNum() { numer = 0; denom = 1; } Note that the above constructor takes no arguments. Such a constructor is called a default constructor . Once we have the default constructor, we can write code as follows.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}