Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMS 261 Computer Science I

Similar presentations


Presentation on theme: "COMS 261 Computer Science I"— Presentation transcript:

1 COMS 261 Computer Science I
Title: Classes Date: October 28, 2005 Lecture Number: 24

2 Announcements Project 2 Due 11/2/05 Exam 2 11/02/05

3 Review I/O manipulators

4 Outline Classes

5 Class Types Class construct
Allows programmers to define new data types for representing information Class type objects can have both attribute components and behavior components Data members Member functions Provides the object-oriented programming in C++

6 Terminology Client Object behaviors Object attributes
Program using a class Object behaviors Realized in C++ via member functions (methods) Object attributes Are known as data members in C++

7 Member Functions Provide an interface to data members, object access, and manipulation Create objects of the class Inspect, mutate, and manipulate object of the class Can be used to keep data members in a correct state setSize() setColor() draw()

8 Member Functions Constructors Inspectors or Accessors
Member functions that initialize an object during its definition Inspectors or Accessors Member functions that act as a messenger that returns the value of an attribute Mutators or Accessors Changes the value of an attribute

9 Member Functions Facilitators
Causes an object to perform some action or service

10 Vector Class Definition of a class to represent a 2D vector, (x, y)
Definition, not an implementation!! Class definitions are stored in header files vec.h Contain Constructor Accessors Facilitators

11 Vec Class Definition Defines the characteristics of an object
Defines the data members Defines the member functions public interface Sort of a mold Does not create an object Instantiation

12 Point Class Definition class VEC {
Allows public access to what follows public: Allows access by members of the vec class to what follows private: };

13 Public access public Declares public members of the class Typically
Member function declarations Interface of the class Allows access to the object’s public members (interface) outside the class

14 Private access private Declares private members of the class Typically
Data members Some members function Allows access to an object’s private members inside the class Inside member functions

15 Access Specifiers Public access Private access
All clients and class members have access to the public members Private access Only class members have access to the private members

16 vec Class Definition class VEC { public: VEC();
// default constructor (no arguments) private: };

17 Default constructor No parameter constructor VEC myVec;
Function overloading allows us to define more than one constructor Only one is the default constructor Other constructors must have a different number and/or type parameters Used when creating an object of the class when no parameters are specified VEC myVec;

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

19 Default constructor Implementation of member functions are put in the vec.cpp file We say vec.h: definition file vec.cpp: implementation file Standard include statements Also need to include “vec.h” The definition of the class

20 Default constructor Implementation in the VEC.cpp file
Member functions prefaced with the scope resolution operator VEC::VEC() { x = 0.0f; y = 0.0f; }

21 Application Now we have enough to write an application program that creates an object of the VEC class Instantiate a VEC object VEC v1; // defines an object of type VEC Of course the class is rather limited We can add some functionality Print member function

22 Implementation Member functions prefaced with the scope resolution operator void VEC::print () { cout << “X: “ << x << “ “; cout << “Y: “ << y << endl; }

23 Application Objects of the VEC class can now call the member function print VEC v1; v1.print(); Run CodeWarrior

24 Accessors Applications read and write the an objects data (private members) through accessor functions Read accessors getBlah(); Write accessors setBlah(val);

25 VEC Class Definition: goes in vec.h class VEC { public: VEC();
}; 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;

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

27 VEC Class It may be useful to add a constructor that takes two parameters An initial value for x and y Allows us to construct VEC objects as: VEC v1(1.2, 3.4); Called and initial value constructor

28 VEC(float xval, float yval);
VEC Class Definition: goes in vec.h class VEC { }; 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;

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

30 VEC Class It may be useful to add a constructor that takes one parameter An initial value for x Allows us to construct VEC objects as: VEC v1(1.2); Also called and initial value constructor

31 VEC(float xval, float yval);
VEC Class Definition: goes in vec.h class VEC { }; 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;

32 Implementation Definition: goes in vec.cpp VEC(float xval) { x = xval;
y = 0.0; } Run CodeWarrior

33 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

34 VEC Class Definition: goes in vec.h class VEC { public:
}; public: VEC(); VEC(float xval, float yval); VEC(float xval, float yval = 0.0f); 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;

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


Download ppt "COMS 261 Computer Science I"

Similar presentations


Ads by Google