9. classes and objects (updates)

9. classes and objects (updates) - Definitions Example:...

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

View Full Document Right Arrow Icon
Definitions Example: 5 class CPoint { public: double x; double y; void setValue(double x_, double y_) { x = x_; y = y_; } } pt1, pt2; class name: CPoint access specifier member variables: double x , double y Instances (objects) of the CPoint class: pt1 , pt2 member function: void setValue(double, double)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Examples: 15 class CPoint { public: CPoint(double x_, double y_) {x = x_; y = y_;} CPoint() {x = 0.0; y = 0.0;} void setValue(double x_, double y_) {x = x_; y = y_;} double getX() const {return x;} double getY() const {return y;} private: double x, y; }; CPoint pt1(1.0, 2.0); CPoint pt2; The 1st constructor CPoint(double, double) is called The 2nd constructor CPoint() is called. Note that when the constructor has no arguments, no parentheses () should be included in the object declaration This is an example of function overload where more than one function have the same name but with different types and/or number of parameters. For overloaded functions, the compiler
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

9. classes and objects (updates) - Definitions Example:...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online