Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 COMS 261 Computer Science I Title: Classes Date: November 4, 2005 Lecture Number: 27."— Presentation transcript:

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

2 2 Announcements

3 3 Review Classes –User defined data types

4 4 Outline Classes

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

6 VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; // default constructor (no arguments) VEC();

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

8 Application #include using namespace std; #include int main ( ) { VEC v1; return 0; }

9 VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; // default constructor (no arguments) VEC(); // output values in object void print(); float getX(); void setX(float val);

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

11 VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval);

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

13 VEC Class Definition: goes in vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval); VEC(float xval);

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 VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval); VEC(float xval, float yval = 0.0f); Default value for yval if not specified

17 Implementation Run CodeWarrior vec05 Definition: vec.cpp VEC::VEC(float xval, float yval) { x = xval; y = yval; } Default values are not specified in the implementation file

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 VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; VEC(); float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval, float yval = 0.0f); Default value for yval if not specified Run CodeWarrior vec06

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 VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval = 0.0f, float yval = 0.0f); VEC(VEC v); Run CodeWarrior vec08

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 VEC Class Definition: vec.h class VEC { }; public: private: // x and y components of the vector float x; float y; float getX(); void setX(float val); void setY(float val); float getY(); void print(); VEC(float xval = 0.0f, float yval = 0.0f); VEC(const VEC& v); Run CodeWarrior vec09 VEC& operator=(const VEC& v);


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

Similar presentations


Ads by Google