Classes

C++ supports many ways to create user-defined types, one way is through a class declaration. A class defines a set of members so that any object whose type is that class will have it's own set of those members.

The following is a class declaration:

struct card { string rank; string suit; };

A new type has been created, the name of this type is card. The keyword struct can be used in a class declaration to indicate that all members should be accessible from any region of code. The members of this class are both data members: rank and suit.

A variable of this type can be declared and the data members of this variable can be accessed using the member-of operator:

card c; c.rank = "Queen"; c.suit = "Hearts";

One can think of the data members of a class type variable as the variables inside the variable.

A class can have member functions:

struct card { string rank; string suit; void print() { cout << rank << " of " << suit; } };

The member function can be called using the member-of operator and the function call operator with an existing object:

c.print();

The body of a member function may access other members specified in the enclosing class, the actual member to be accessed will always be relative to the object from which the member function was called (known as the "this" object).

The members of a class always have an access mode which restricts that member from being accessed from certain regions of code. Consider the following, more advanced, class:

#include <iostream> #include <cmath> using namespace std; double degrees_to_radians(double d) { const double pi = 4 * atan(1); return (d * pi / 180); } class vector_2d { public: double magnitude() const { return sqrt(pow(x, 2) + pow(y, 2)); } double direction() const { return atan2(y, x); } double first() const { return x; } double second() const { return y; } void set(double mag, double dir) { double r_dir = degrees_to_radians(dir); x = mag * cos(r_dir); y = mag * sin(r_dir); } private: double x, y; }; int main() { double mag, dir; vector_2d v; cout << "Set magnitude: "; cin >> mag; cout << "Set direction: "; cin >> dir; v.set(mag, dir); cout << "The components are: " << v.first() << ' ' << v.second() << endl; return 0; }

Here the keyword class is used instead of struct. Unlike struct, the default access mode for class is private; whereas the default access mode for struct is public. Other than this difference, both class and struct are the same in C++.

The access mode for a region of class members can be specified as shown in the above code. Any code inside or outside the class may access public members; however, only code inside the same class may access private members.

Even though x and y are private in vector_2d, their values can be fetched through the first and second public members functions; however, x and y cannot be directly assigned to from outside the class. The only way x and y can be modified from outside code is indirectly through the set public member function.

Tuning the access modes of various class members allows one to craft a well-defined interface over data that outside code need not necessarily know about.

Notice the const keyword used after all the member functions except set, a function marked as const indicates that that member function does not make any assignments to data members, allowing that member function to be called on variables declared as const.