coms 261 computer science i

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

Upload: walter

Post on 18-Jan-2016

32 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: COMS 261 Computer Science I

1

COMS 261Computer Science I

Title: Classes

Date: November 4, 2005

Lecture Number: 27

Page 2: COMS 261 Computer Science I

2

Announcements

Page 3: COMS 261 Computer Science I

3

Review

• Classes– User defined data types

Page 4: COMS 261 Computer Science I

4

Outline

• Classes

Page 5: COMS 261 Computer Science I

Classes

• vec.h: definition file

• vec.cpp: implementation file

Page 6: COMS 261 Computer Science I

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

Default constructor

• Implementation: vec.cpp

#include <vec.h>

VEC::VEC() {

x = 0.0f;

y = 0.0f;

}

Page 8: COMS 261 Computer Science I

Application

#include <iostream>

using namespace std;

#include <vec.h>

int main ( ) {

VEC v1;

return 0;

}

Page 9: COMS 261 Computer Science I

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

Accessor Implementation

float VEC::getX (){

return x;

}

void VEC::setX (float val) {

x = val;

}

Page 11: COMS 261 Computer Science I

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

Implementation

• Definition: goes in vec.cpp

VEC::VEC(float xval, float yval) {

x = xval;

y = yval;

}

Page 13: COMS 261 Computer Science I

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

Implementation

• Definition: vec.cpp

VEC::VEC(float xval) {

x = xval;

y = 0.0;

}

Page 15: COMS 261 Computer Science I

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

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

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

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

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

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

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

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

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

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

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);