Professional Documents
Culture Documents
Outline
Classes in C++
access modifiers: private, public, protected struct versus class class methods
defined defined separately inlining static instance variables accessors mutators managers
default ctor, dtor, copy ctor, other ctors
A Motivating Example
You declare a simple structure:
struct Robot { float locX; float locY; float facing; }; Robot r1;
What if you never want locX or locY to be negative? Someone unaware of your restriction could set:
r1.locX = -5;
ClassName is a new type (just as if declared as struct with typedef in C) Possible access modifiers:
public private protected
Fields following a modifier are of that access until the next modifier is indicated Fields can be variables (as in C structures) or functions
A Simple Class
class Robot { public: float getX() { return locX; } float getY() { return locY; } float getFacing() { return facing; } void setFacing(float f) { facing = f; } void setLocation(float x, float y); private: float locX; float locY; float facing; };
Example:
void useRobot() { Robot r1; r1.setLocation(-5,-5); // Legal to call
In a class, until an access modifer is supplied, the fields are assumed to be private In a struct, the fields are assumed to be public
Class Methods
Functions associated with a class are declared in one of two ways:
ReturnType FuncName(params) { code }
function is both declared and defined (code provided)
ReturnType FuncName(params);
function is merely declared, we must still define the body of the function separately
Defined Methods
class Robot { public: float getX() { return locX; } }; Robot r1;
The function getX is defined as part of class Robot To call this method:
cout << r1.getX() << endl; // prints r1s locX
Inline Functions
In an inline function, the C++ compiler does not make a function call, instead the code of the function is used in place of the function call (and appropriate argument substitutions made) Why?
Inline functions dont have the overhead of other functions Things like accessing/changing fields of a class instance should be fast Including the definition of a function is an implicit request to make a function inline
Inline Requests
Not all requests to make a function inline are honored
generally C++ examines the complexity of the function and the use of parameters in the function should only request inline definition for short functions
Can also explicitly request to make class methods and other functions inline (add inline keyword before return type in function declaration and definition)
A Simple Class
class Robot { public: void setLocation(float x, float y); private: float locX; float locY; float facing; }; void Robot::setLocation(float x, float y) { if ((x < 0.0) || (y < 0.0)) cout << Illegal location!! << endl; else { locX = x; locY = y; } }
Since a class function is always called on a class instance, locX, locY are those fields of that instance Example:
Robot r1; Robot r2; r1.setLocation(5,5); r2.setLocation(3,3);
// locX is from r1 // locY is from r2
Object-Oriented Idea
Declare implementation-specific fields of class to be private Provide public accessor and mutator methods that allow other users to connect to the fields of the class
often provide an accessor and mutator for each instance variable that lets you get the current value of that variable and set the current value of that variable to change implementation, make sure accessors and mutators still provide users what they expect
Example:
void main() { Robot r1, r2; r1.initializeRobot(); r2.initializeRobot(); cout << Robot::numRobots << robots.\n;
Destructor (dtor)
A destructor is normally not critical, but if your class allocates space on the heap, it is useful to deallocate that space before the object is destroyed
C++ provides a default dtor that does nothing can only have one dtor
Example dtor
class Robot { public: char *robotName; Robot() { robotName = 0; } void setRobotName(char *name) { robotName = new char[strlen(name)+1]; strcpy(robotName,name); } ~Robot() { delete [] robotName; } }
Other ctors
It is often useful to provide constructors that allow the user to provide arguments in order to initialize arguments Form is similar to the copy ctor, except parameters are chosen by programmer:
class MyClass { public; MyClass(parameters) { code here } }
Example ctor
class Robot { public: Robot(float x, float y, float face) { locX = x; locY = y; facing = face; } } calling: Robot r1(5.0,5.0,1.5); Robot r2(5.0,10.0,0.0); Robot* rptr; rptr = new Robot(10.0,5.0,-1.5);
A Combination ctor
Can combine a ctor that requires arguments with the default ctor using default values:
class Robot { public: Robot(float x = 0.0, float y = 0.0, float face = 1.57075) { locX = x; locY = y; facing = face; } } calling: Robot r1; // ctor called with default args Robot r2(); // ctor called with default args Robot r3(5.0); // ctor called with x = 5.0 Robot r4(5.0,5.0; // ctor called with x,y = 5.0
Example ctor
class Robot { public: Robot(float x, float y, float face) { locX = x; locY = y; facing = face; } private: Robot() {} } calling: Robot r1(5.0,5.0,1.5); Robot r2; // ERROR, attempts to call default ctor