Classes differ from structures in that they allow for data hiding, abstraction and encapsulation, inheritance, and polymorphism. With a class definition, the class name is a type of name. A class is an abstract data type; a class specifies how objects of its type behave, are created and deleted, and are accessed.
The keywords private, protected, and public are used to specify three levels of access protection for data hiding and utility methods. private means that the member can only be accessed by the member functions and friends of the class. The private member is not directly accessible to the outside world. public means that member can be accessed by any function in the external environment. Public methods are interfaces to the outside world so that any other function can interact with it. protected means that the member can only be accessed by the class and member functions and classes derived from this class.
#include <iostream> class CClock { public: //constructors CClock(); CClock(int h, int m, int s); //getters - setters void setHour(int h); void setMinute(int m); void setSecond(int s); int getHour() const; int getMinute() const; int getSecond() const; void printTime(); private: int _hour; int _min; int _sec; };
Member functions of a class are typically defined outside the class header file. The scope resolution operator, ::, is used to assign the function to a class.
The first member functions we can look at are the constructors. Constructors are special methods that are called automatically when an object of a class type is created. The constructor or constructors job is to initialize the object’s state – it’s member variables. If no constructor is specified then the compiler provides one by default.
Since C++ does not have zero initialization of member variables such as ints, it is a good idea to modify the default constructor so that it sets a default value for each member.
//default constructor CClock::CClock() { _min = 0; _hour = 0; _sec = 0; } //initialize the member variables to specific values CClock::CClock(int h, int m, int s){ _min = m; _sec = s; _hour = h; }
Getters and setters are used to assign values to data members. Getters are usually declared to be const so as to ensure that the values aren’t accidentally modified during retrieval. Setters often do value checking to ensure that the new data is in the proper range.
//setters void CClock::setHour(int h){ //only set hour if it falls in the range 0 - 23 if(0 <= h && h < 24){ _hour = h; } } void CClock::setMinute(int m){ if(0 <= m && m < 60){ _min = m; } } void CClock::setSecond(int s){ if(0 <= s && s < 60){ _sec = s; } } //getters int CClock::getHour() const { return _hour; } int CClock::getMinute() const { return _min; } int CClock::getSecond() const { return _sec; }
Finally, we will set up a method to print the data in a formatted manner.
void CClock::printTime(){ if(_hour < 10){ cout << "0"; } cout << _hour << ":"; if(_min < 10){ cout << "0"; } cout << _min << ":"; if(_sec < 10){ cout << "0"; } cout << _sec; }
There are two ways to access a member of a class. We typically access a data or function member of the class object using ., the dot operator.When a pointer to an object of a class is used, we use the -> operator to access data or function members of the class.
#include "CClock.h" using namespace std; int main() { CClock clockOne; CClock clockTwo(22, 15, 00); cout << "Clock One after default initialization: "; clockOne.printTime(); cout << endl; cout << "Clock Two after being initialized to a specific time: "; clockTwo.printTime(); cout << endl; clockOne.setHour(3); clockOne.setMinute(33); clockOne.setSecond(15); cout << endl; clockOne.printTime(); return 0; }