1 / 25

COMS 261 Computer Science I

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:.

walter
Download Presentation

COMS 261 Computer Science I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COMS 261Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27

  2. Announcements

  3. Review • Classes • User defined data types

  4. Outline • Classes

  5. Classes • vec.h: definition file • vec.cpp: implementation file

  6. 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; };

  7. Default constructor • Implementation: vec.cpp #include <vec.h> VEC::VEC() { x = 0.0f; y = 0.0f; }

  8. Application #include <iostream> using namespace std; #include <vec.h> int main ( ) { VEC v1; return 0; }

  9. 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;

  10. Accessor Implementation float VEC::getX (){ return x; } void VEC::setX (float val) { x = val; }

  11. 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;

  12. Implementation • Definition: goes in vec.cpp VEC::VEC(float xval, float yval) { x = xval; y = yval; }

  13. 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;

  14. Implementation • Definition: vec.cpp VEC::VEC(float xval) { x = xval; y = 0.0; }

  15. 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

  16. 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;

  17. 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

  18. 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

  19. 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;

  20. 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

  21. 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

  22. 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;

  23. 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!!

  24. 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

  25. 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;

More Related