From Structure to Class
In C: you concentrate on writing functions: Groups of actions for performing some task are formed into functions; Functions are grouped to form programs. Data plays primarily a supportive role for performing the actions. In C++: you concentrate on creating your own user-defined types, or, classes. Data and a set of functions that manipulate the data form a class. The data components: data members. The function components: member functions. An instance of a built-in type can be called a variable, an instance of a class is called an object. class in C++ is a natural evolution of the C notion of struct. After getting an acquaintance with classes, you will realize, that structures in C++ are classes with public members only. C structure, which simply prints in a time format, is given below:
In this program we meet with the conditional operator, the only ternary operator in C++, which takes three operands. The argument of a print function, which is evidently of type void, is a const reference to the structure type Time. It is elegant to call for the Time variable via reference, and safety, because this is const. Let us make a step towards a class-like style:
What's a difference? We included print into the member functions of structure Time, and instead of setting time through the Time variable t, we set it with using setTime. This allows us to get a direct access to other Time members, hr, mn, sc.
Exercize:
Now, if we wish to arrive at the same functionality in terms of
Class Time, the changes are really
minimal. In the code below you meet with a new
special function Time(),
which is a
The output should be like this:
Even such a short code becomes more readable, if their parts are re-arranged into header, source file and driver (that is a common place of C, too): Header: Declarations & Class Definition
The following directive defines header file TIME_CLASS_1_H: #define TIME_CLASS_1_H. This means that the compiler's preprocessor puts TIME_CLASS_1_H on a list to indicate that it has been seen. The next directive, #ifndef TIME_CLASS_1_H, tests to see whether TIME_CLASS_1_H has been defined. If defined, then everything between this directive and #endif will be skipped. Why it is necessary to do? That is because C++ does not allow you to define a class more than once. In the code below we shall use the include directive #include "time_class_1.h" twice, and with no problems: the class will be defined only one time. Source file: Constructor, member functions, etc.
Main, or, driver for testing:
How to compile and execute them? You can create a makefile based on the commands:
The member access specifiers public and private are used to control access to a class’ data members and member functions. Private class members can be only accessed by member
functions (and friend functions) of the class.
Initializing Class Objects: Constructors Constructor: a class member function with the
same name as the class name.
Performance
can be improved with using a default constructor.
| |
class Time
{ public: Time(); ...... Time::Time() { hr=mn=sc=0; } |
class Time
{ public: Time(int = 0, int = 0, int = 0); ...... Time::Time(int hr, int mn, int sc) { setTime(hr, mn, sc); } |
Then, instead of formal setting of time with
setTime as,
for example, t.setTime(14,31,68), it will be possible to set data members
“directly” via t(14,31,68).
Then, t(14,31) means
t(14,31,0),
t(14) means
t(14,0,0),
t means
t(0,0,0).
Using Destructors: Name for the destructor:
~ClassName()
Destructors are the proper functions for classes whose objects contain dynamically allocated storage. Example of Constructor & Destructors hierarchies
| |
The first column includes the object’s numbers in order of their occurence in create_and_destroy_test.cpp. Please, before executing a program, fill the second column with the numbers in order of the same object’s destruction. NUMBER is a user-defined type called an enumeration. Very efficient in combination with the switch statement. Local variable declared with the keyword static is known only in function in which it is defined. static local variable retains its value when the function is exited. When the function is called next time, static local variable contains the value at the moment of function's last exiting. |
![]() |
Exercize: Add new functionalies to our Time class.
|