coms 261 computer science i
DESCRIPTION
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:. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/1.jpg)
1
COMS 261Computer Science I
Title: Classes
Date: November 4, 2005
Lecture Number: 27
![Page 2: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/2.jpg)
2
Announcements
![Page 3: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/3.jpg)
3
Review
• Classes– User defined data types
![Page 4: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/4.jpg)
4
Outline
• Classes
![Page 5: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/5.jpg)
Classes
• vec.h: definition file
• vec.cpp: implementation file
![Page 6: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/6.jpg)
VEC Class
• Definition: vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat x;float y;
// default constructor (no arguments)VEC();
![Page 7: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/7.jpg)
Default constructor
• Implementation: vec.cpp
#include <vec.h>
VEC::VEC() {
x = 0.0f;
y = 0.0f;
}
![Page 8: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/8.jpg)
Application
#include <iostream>
using namespace std;
#include <vec.h>
int main ( ) {
VEC v1;
return 0;
}
![Page 9: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/9.jpg)
VEC Class
• Definition: vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat x;float y;
// default constructor (no arguments)VEC();// output values in objectvoid print();
float getX();void setX(float val);
![Page 10: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/10.jpg)
Accessor Implementation
float VEC::getX (){
return x;
}
void VEC::setX (float val) {
x = val;
}
![Page 11: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/11.jpg)
VEC Class
• Definition: vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat x;float y;
VEC();
float getX();
void setX(float val);void setY(float val);
float getY();
void print();
VEC(float xval, float yval);
![Page 12: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/12.jpg)
Implementation
• Definition: goes in vec.cpp
VEC::VEC(float xval, float yval) {
x = xval;
y = yval;
}
![Page 13: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/13.jpg)
VEC Class
• Definition: goes in vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat 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);
![Page 14: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/14.jpg)
Implementation
• Definition: vec.cpp
VEC::VEC(float xval) {
x = xval;
y = 0.0;
}
![Page 15: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/15.jpg)
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
![Page 16: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/16.jpg)
VEC Class
• Definition: vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat 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 yvalif not specified
![Page 17: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/17.jpg)
Implementation
Run CodeWarrior vec05
• Definition: vec.cpp
VEC::VEC(float xval, float yval) {
x = xval;
y = yval;
} Default values are not specifiedin the implementation file
![Page 18: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/18.jpg)
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
![Page 19: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/19.jpg)
VEC Class
• Definition: vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat 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 yvalif not specified
Run CodeWarrior vec06
![Page 20: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/20.jpg)
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
![Page 21: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/21.jpg)
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
![Page 22: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/22.jpg)
VEC Class
• Definition: vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat 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
![Page 23: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/23.jpg)
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!!
![Page 24: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/24.jpg)
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
![Page 25: COMS 261 Computer Science I](https://reader036.vdocument.in/reader036/viewer/2022062409/56814cf8550346895dba0687/html5/thumbnails/25.jpg)
VEC Class
• Definition: vec.hclass VEC {
};
public:
private:
// x and y components of the vectorfloat 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);