Table of Contents
An object has two parts, one is a data that it remembers and the second one is the action that it can perform. Once it is all set that how it will determine the member data and member functions and how it will play a role in the program becomes easy. Member data is similar to variables in procedural languages or in other words, in a normal function, which should not be accessed by an object. The lifetime of the value of the data member is throughout the entire object as long as the object is alive.
In the example program dateclass.cpp, date is a class where memory allocation is not done at this point. Once we declare a variable of the class date, memory is allocated. In c++, this variable is called an object. An object is an instance of a class, in our case d is an instance of the class date. It is the same as variable with all the functionality specified in the class’s definition.
The class date is just the definition, which does not allocate memory. It is an information to the compiler what a date is, what data and functions it contains.
The first statement in the program dateclass.cpp, d is an object of class date. The declaration of an object is the same as declaring a variable for the structure. In general, an object is similar to defining a variable of different types. Whether a variable or an object, a memory space is set aside at the time of declaration not at the time of definition.
Difference between class and object
The difference is very much simple. To keep the explanation simple, suppose there is an aeroplane and its technical manual. If there is anything wrong in the aeroplane, the first thing is to check its technical manual. By going through the process, the manual will tell you everything you need to know about that particular section so that you can go through and build it. You can have a look at the technical manual but that won’t fly you from one place to another. But you can build the same with the help of the technical manual or a better modification can be made in the existing one. The technical manual is a class and the aeroplance is an object.
Different methods of declaring member functions
There is another way of defining member functions. One way is to define it within a class the other way is to define it outside the class. It is a good programming practice to define the function outside the class if the code looks large. This can enhance the readability of the program.
Suppose we have five functions and all of them are defined in the class. It would be hard to find an appropriate function that what actually it performs and what kind of information it can digest and what kind of information it can return. If we declare these five functions outside the class then it would be easier for the programmer to modify these functions in future. We need only the function prototypes inside the class that can give us more flexibility in terms of readability and modifiability.
To declare a member function outside a class declaration the syntax is shown next.
The general format
Return-type class-name :: member-function(parameter-list)
The most familiar part that is the return-type, which is the first part of the declaratory. It is then followed by the class-name to which the member function belongs. Two colons (:: ) followed by the class-name with no space in between the colons, followed by the member function with or without the parameter-list. The two consecutive coloncs are called the scope resolution operator. If you look again to the general format, it is the same as normal function declaration except the class-name with the two colons.
Let us take a look at the second method of declaring a function in a program dateclass1.cpp, which produces the same tested result with complete performance.
- //program name: dateclass1.cpp
- class date
- int yy;
- void getyear();
- void findleapyear();
- void date::getyear()
- cout<<”enter a year value to find the leap year \n”;
- void date::findleapyear()
- If (yy%4==0) && (((yy%100 !=0) || ((yy%400)==0)))
- cout<<”you entered a leap year”<
- cout<<”That’s not a leap year”<
- int main (void)
- date d;