250 likes | 391 Views
COMS 261 Computer Science I. Title: Classes Date: November 4, 2005 Lecture Number: 27. Announcements. Review. Classes User defined data types. Outline. Classes. Classes. vec.h: definition file vec.cpp: implementation file. VEC Class. Definition: vec.h. class VEC {. public:.
E N D
COMS 261Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27
Review • Classes • User defined data types
Outline • Classes
Classes • vec.h: definition file • vec.cpp: implementation file
VEC Class • Definition: vec.h class VEC { public: VEC(); // default constructor (no arguments) private: float x; // x and y components of the vector float y; };
Default constructor • Implementation: vec.cpp #include <vec.h> VEC::VEC() { x = 0.0f; y = 0.0f; }
Application #include <iostream> using namespace std; #include <vec.h> int main ( ) { VEC v1; return 0; }
class VEC { }; VEC Class • Definition: vec.h public: VEC(); // default constructor (no arguments) void print(); // output values in object float getX(); void setX(float val); private: float x; // x and y components of the vector float y;
Accessor Implementation float VEC::getX (){ return x; } void VEC::setX (float val) { x = val; }
class VEC { }; VEC Class • Definition: vec.h public: VEC(); VEC(float xval, float yval); void print(); float getX(); float getY(); void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;
Implementation • Definition: goes in vec.cpp VEC::VEC(float xval, float yval) { x = xval; y = yval; }
class VEC { }; VEC Class • Definition: goes in vec.h public: VEC(); VEC(float xval, float yval); VEC(float xval); void print(); float getX(); float getY(); void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;
Implementation • Definition: vec.cpp VEC::VEC(float xval) { x = xval; y = 0.0; }
VEC Class • It may be more useful to add a constructor that takes one parameter and optionally takes another parameter • Default parameter • An initial value for x • Optional parameter for y • Allows us to construct VEC objects as: VEC v1(1.2); • Also called and initial value constructor
class VEC { }; VEC Class • Definition: vec.h public: VEC(); VEC(float xval, float yval); VEC(float xval, float yval = 0.0f); void print(); float getX(); float getY(); Default value for yval if not specified void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;
Implementation • Definition: vec.cpp VEC::VEC(float xval, float yval) { x = xval; y = yval; } Default values are not specified in the implementation file Run CodeWarrior vec05
Implementation • Syntax error • Improper member function overloading • Compiler cannot distinguish between the two parameter and the two parameter with an initial value constructor • Keep the constructor that provides flexibility
class VEC { }; VEC Class • Definition: vec.h Run CodeWarrior vec06 public: VEC(); VEC(float xval, float yval = 0.0f); void print(); float getX(); float getY(); Default value for yval if not specified void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;
VEC Class • If one default parameter is a good idea, how about a constructor with two default parameters • Optional parameter for x and y Run CodeWarrior vec07
VEC Class • The copy constructor • We would like to provide the flexibility of creating a copy of a VEC object • VEC v1(1.2f, 3.4f); • VEC v2(v1); • Include a constructor that makes a copy of an existing VEC object
class VEC { }; VEC Class • Definition: vec.h public: VEC(float xval = 0.0f, float yval = 0.0f); VEC(VEC v); void print(); float getX(); Run CodeWarrior vec08 float getY(); void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;
VEC Class • The copy constructor • If you do not implement a copy constructor, one will be implemented for you • By the compiler • It may not do what you want • It will for this course, but not for COMS 262!!
VEC Class • The assignment operator • We would like to provide the flexibility of assigning one VEC object to another • VEC v1(1.2f, 3.4f); • VEC v2; • V2 = v1; • Overload the assignment operator so a VEC can appear on the left and right side
class VEC { }; VEC Class • Definition: vec.h public: VEC(float xval = 0.0f, float yval = 0.0f); VEC(const VEC& v); VEC& operator=(const VEC& v); void print(); float getX(); Run CodeWarrior vec09 float getY(); void setX(float val); void setY(float val); private: float x; // x and y components of the vector float y;