<< Chapter < Page | Chapter >> Page > |
In C++ programming, classes are structures that contain variables along with functions for manipulating that data.
The functions and variables defined in a class are referred to as class members.
Class variables are referred to as data members , while class functions are referred to as member functions .
Classes are referred to as user-defined data types or programmer-defined data types because you can work with a class as a single unit, or objects, in the same way you work with variables.
When you declare an object from a class, you are said to be instantiating an object.
The most important feature of C++ programming is class definition with the class keyword. You define classes the same way you define structures, and you access a class’s data members using the member selection operator .
Example:
class Time {
public:
Time();
void setTime( int, int, int );
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
Once the class has been defined, it can be used as a type in object, array and pointer definitions as follows:
Time sunset, // object of type Times
ArOfTimes[5], // array of Times objects
*ptrTime; // pointer to a Times objects
The class name becomes a new type specifier. There may be many objects of a class, just as there may be many variables of a type such as int. The programmer can create new class types as needed. This is one reason why C++ is said to be an extensible language.
The principle of information hiding states that any class members that other programmers, or clients, do not need to access or know about should be hidden.
Many programmers prefer to make all of their data member private in order to prevent clients from accidentally assigning the wrong value to a variable or from viewing the internal workings of their programs.
Access specifiers control a client’s access to data members and member functions. There are four levels of access specifiers: public, private, protected, and friend.
Notification Switch
Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?