Escolar Documentos
Profissional Documentos
Cultura Documentos
Information Hiding
Information hiding - Class implementation details are hidden from the clients view Public functions of a class provide the interface between the client code and the class objects
client code
specification
implementation
Different classes can have member functions with the same identifier, like Write()
Member selection operator is used to determine the object to whom member function Write() is applied
currentTime.Write(); numberZ.Write(); // Class Time // Class ComplexNumber
In the implementation file, the scope resolution operator is used in the heading before the function members name to specify its class
void Time::Write () const
{ } . . .
4
endTime
Private data:
hrs 17
Increment Write
Private data:
hrs mins 18 30
LessThan
secs Equal
LessThan
2 Equal
secs
Example
Constructors
A class constructor is a member function whose purpose is to initialize the private data members of a class object Objects generally need to initialize variables or assign dynamic memory during their process of creation to become operative and to avoid returning unexpected values during their execution. For example, what would happen if in the previous example we called the member function area() before having called function set_values()? Probably we would have gotten an undetermined result since the members x and y would have never been assigned a value. In order to avoid that, a class can include a special function called constructor, which is automatically called whenever a new object of this class is created
Class Constructors
The name of a constructor is always the name of the class, and there is no return type for the constructor A class may have several constructors with different parameter lists
11 11
Parameterized Constructor
Time::Time( /* in */ int initHrs, /* in */ int initMins, /* in */ int initSecs) // Constructor // Precondition: // 0 <= initHrs <= 23 && 0 <= initMins <= 59 // 0 <= initSecs <= 59 // Postcondition: // hrs == initHrs && mins == initMins // && secs == initSecs { hrs = initHrs; mins = initMins; secs = initSecs; }
13 13
departureTime
movieTime
Set Increment
Private data: hrs
Set
0
0 0
Increment Write
Write
mins
19
30 0
14
LessThan
secs
LessThan
secs
Equal
Equal
Destructors
The destructor fulfills the opposite functionality. It is automatically called when an object is destroyed, either because its scope of existence has finished (for example, if it was defined as a local object within a function and the function ends) or because it is an object dynamically assigned and it is released using the operator delete. The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no value. The use of destructors is especially suitable when an object assigns dynamic memory during its lifetime and at the moment of being destroyed we want to release the memory that the object was allocated.
Destructors
class SomeClass { public: : SomeClass(); ~SomeClass(); private: : };
Default constructor
//Correct //Incorrect