An object is an instance of a class (an instance of the data structure); objects share the same functions with other objects of the same class, but each object (each instance) has its own copy of the data structure.
A class thus defines two aspects of the objects: the data
they contain, and the behavior they have
In addition to member functions, a class's behavior is
also defined by:
constructor for that object: what
to do when you create a new object
destructor for that object: what
to do when you delete an object
A private member can only be read, written, or called by a member function of that class.
class Stack {
public:
int top; // Index of the top of the stack.
int stack[10]; // The elements of the stack.
void Push(int value); // Push an integer, checking for overflow.
};void
Stack::Push(int value) {
ASSERT(top < 10); // stack should never overflow
stack[top++] = value;
}This class has two data members, top and stack, and one member function, Push. The notation class::Push denotes the function member of the class Stack. The function is defined beneath it.
In actual usage, the definition of class Stack would typically go in the file stack.h and the definitions of the member functions, like Stack::Push, would go in the file stack.cc.
If s is the pointer to a Stack object,we can access the top element as s->top, just as in C.
However, we can also call the member function using the syntax: s->Push(17);
Inside a member function, one may refer to the members of the class by their names alone.
The purpose of member functions is to encapsulate the functionality of a type of object along with the data that the object contains. A member function does not take up space in an object of the class.
C++ classes are similar to C structures in some ways.
C struct is a class that has only public data members
Private members
private members of a class are hidden to all but the member functions of that class.
public members are visible and accessible to everybody.
Both data and function members can be either public or private.
In C++, we suggest all data members of a class should be private. All operations on data should be via that class' member functions.
Keeping data private adds to the modularity of the system, since you can redefine how the data members are stored without changing how you access them.
In our stack example, if we introduce a Full() function, then we don't need to look at the top or stack members outside of the class. Thus we can rewrite the program as follows:
class Stack {
public:
void Push(int value); // Push an integer, checking for overflow.
bool Full(); // Returns TRUE if the stack is full, FALSE otherwise.
private:
int top; // Index of the top of the stack.
int stack[10]; // The elements of the stack.
};bool
Stack::Full() {
return (top == 10);
}Now we can rewrite Push this way:
void
Stack::Push(int value) {
ASSERT(!Full());
stack[top++] = value;
}Before, given a pointer to a Stack object, say s, any part of the program could access s->top, in potentially bad ways. Now, since the top member is private, only a member function, such as Full(), can access it.
Constructors and the operator new
constructor is a member function of the class, with the name of the function being the same as the class name. It specifies how the object should be initialized:
class Stack {
public:
Stack(int sz); // Constructor: initialize variables, allocate space.
void Push(int value); // Push an integer, checking for overflow.
bool Full(); // Returns TRUE if the stack is full, FALSE otherwise.
private:
int size; // The maximum capacity of the stack.
int top; // Index of the lowest unused position.
int* stack; // A pointer to an array that holds the contents.
};Stack::Stack(int sz) {
size = sz;
top = 0;
stack = new int[size]; // Let's get an array of integers
}To create a new object of type Stack, we write:
Stack* s = new Stack(17);
The new function takes the place of malloc() in C. It creates (i.e. allocates) the object and then calls the constructor function.
It is crucial that you always define a constructor for every class you define, and that the constructor initialize every data member of the class.
Data allocated with new (such as s) is stored on the heap, and remains after the function returns; heap data must be explicitly disposed of using delete, described below.
Destructors and the operator delete
Destructor is a member function of class.It specifies how the object space should be freed:
class Stack {
public:
Stack(int sz); // Constructor: initialize variables, allocate space.
~Stack(); // Destructor: deallocate space allocated above.
void Push(int value); // Push an integer, checking for overflow.
bool Full(); // Returns TRUE if the stack is full, FALSE otherwise.
private:
int size; // The maximum capacity of the stack.
int top; // Index of the lowest unused position.
int* stack; // A pointer to an array that holds the contents.
};Stack::~Stack() {
delete [] stack; // delete an array of integers
}To deallocate Stack object s we created before by new, we write:
delete s;
Always delete objects that you created (with new) !