C++ Classes: The Basics
Attributes (or Member Variables or Member Values)
Definition: The data members of a class.
Types: An attribute can be any fully defined type (built-in, class, or pointer).
Access: Attributes are access via the dot operator.
Example: Define a class, TwoDimPoint, with attributes x and y.
// class definition
class TwoDimPoint {
public:
double x;
double y;
};
// object declaration
TwoDimPoint pt;
// object manipulation
pt.x = 4.03;
pt.y = 0.27;
cout << "(" << pt.x << ", " << pt.y << ")" << endl;
Note: See exception to using dot operator within member functions below.
Member Functions
Definition: Any function defined within the context of the class. A member function as "special access" the attributes of the class.
Accessor and Mutator Functions: Generally, we want to build classes that disallow access to their attributes. How then do we get and give values to the attributes? To do this we use Accessor and Mutator functions.
Example:
// modified class definition (or interface or specification) for TwoDimPoint
class TwoDimPoint {
public:
// accessor functions
double getX();
double getY() {return y;}
// mutator functions
void setX(double value) {x = value;}
void setY(double value);
private:
double x;
double y;
};
// class implementation
double TwoDimPoint::getX() {
return x;
}
void TwoDimPoint::setY(double value) {
y = value;
}
// object declaration and manipulation
TwoDimPoint pt;
pt.setX(1.2);
pt.setY(pt.getX());
Constructors: Specially-named member functions that give initial values to the attributes.
All constructors for a class have the same name as the class.
Example:
// modified class definition for TwoDimPoint
class TwoDimPoint {
public:
// constructors
TwoDimPoint() : x(0), y(0) {} // default constructor
TwoDimPoint(const TwoDimPoint &p); // copy constructor
TwoDimPoint(double v1, double v2); // two-parameter constructor
// accessor functions
double getX();
double getY() {return y;}
// mutator functions
void setX(double value) {x = value;}
void setY(double value);
// friend function
friend ostream& operator<<(ostream &out, TwoDimPoint &pt);
private:
double x;
double y;
};
// class implementation
TwoDimPoint::TwoDimPoint(const TwoDimPoint &p) : x(p.x), y(p.y) {
}
TwoDimPoint::TwoDimPoint(double v1, double v2) {
x = v1;
this->y = v2; // allowable, but generally *bad* style
}
double TwoDimPoint::getX() {
return x;
}
void TwoDimPoint::setY(double value) {
y = value;
}
ostream& operator<<(ostream &out, TwoDimPoint &pt) {
out << "(" << pt.x << ", " << pt.y << ")" << endl;
}
// object declaration and manipulation (declarations contain one error)
TwoDimPoint pt1(2.0);
TwoDimPoint pt2(3.25, 4.75);
TwoDimPoint pt3(pt1);
TwoDimPoint pt4 = pt2;
pt1.setX(1.2);
pt1.setY(pt1.getX());
pt2 = TwoDimPoint(-1.0, 1.0);
cout << "pt1 = " << pt1 << endl;
cout << "pt2 = " << pt2 << endl;
cout << "pt3 = " << pt3 << endl;
cout << "pt4 = " << pt4 << endl;
No Return Type: Unlike other functions, constructors have no specified return type (although its as though they return their class type).
Base Initializers: The section between the ":" and the opening function brace is called the "base initializer section." Only attributes (not general assignment) can be initialized here.
Default Constructor Rules:
If no constructors (at all) are defined, then the compiler supplies a default (zero-argument) constructor, which does nothing.
If any type of constructor is defined, then the default constructor is not automatically provided.
Initialization by like object
Call-by-value parameter
Returned function value
this: The this object is the implicit parameter to the member function. It is the object specified in the function call. Generally, we do not refer to this (only its attributes), except where necessary.
Protection Classes
Public:
Allows access to functions or attributes by any function.
Generally, the protection type for most member functions.
Private
Allows access to functions or attributes by only the member functions of the given class.
Generally, the protection type for class attributes.
Protected
Allows access to functions and attributes by class and derived class member functions.
Rarely used. Used should be justified in comments.