Classes in C++

category: 

Table of Contents

C++ is capable to provide not only the predefined classes but also the user-defined classes. Programmers can instantiate these classes any number of times. It contains two areas of declaration, one is called data member area and the other one is called the member function area. Both can be associated with different types of access specifiers. Classes can be inherited but at this point it is not the topic of discussion. The detail discussion can be seen in inheritance.

 Declaring classes

 There are different structures available in c++ language and classes are one of them. It is analogous to a struct. In struct type data structure we include data but in class type a function is included along the data that operate on that data.

The declaration of a class is almost the same as a struct, except that a function can be incorporated. The function concept is the same as discussed previously. Inside the class there are two ways to include a function. One is to declare the function in full and the second is to declare it as prototypes. But the convention is not to include a function if it is out the end of the class. It is the best consideration to provide the function implementation separately unless the request is different. We will discuss this in another tutorial.

An example of struct declaration is

  1. Struct date
  2. {
  3. int year;
  4. int month, day;

Now consider a class declaration

  1. class date
  2. {
  3.              private:
  4.                         int year;
  5.                         int month;
  6.                         int day;
  7.              public:
  8.                         void getyear();
  9.                         void findleapyear();
  10. };

In the first case, struct is a reserved word followed by the struct name or type. In the second case, the reserved word class replaces the struct. In the later case, date is a class name or class type. In both cases the curly braces delimit the body which is terminated by the semicolon. As mentioned, struct and classes are analogous to each other.

In the class declaration, year, month, day is declared as an int in the private section. In procedural language like ‘c’ these are called variables but the term used in c++ is the data members of the class. In other words, a data member is a variable declared in a class definition.

There are two function prototypes in public area. In ‘c’ they are called the prototypes of function. In c++, it is called member function prototypes of the class.

If you actually want to be a good programmer in c++, then you must have a good knowledge about the c++ class, which is the key element for object-oriented-programming in C++. Once you fully understand the concept of a class, you are then able to create new types that have all the features of integral types such as int or float. With the full concept of a class, later you will be able to design a user-defined class to as good or powerful as integral types.

The general format is

  1. Class class-name
  2. {
  3.        Private:
  4.                    Date-type data-datamember
  5.        Public:
  6.                    Return-type function-name(parameter-list)
  7. };

In the general format of a class, there are some reserved words. They are class, private and public. A colon (:) must follow the last two.

Classes have three parts. One is private, the second one is public and the third one is protected. Private part is that where the data member resides and public part is that where member function resides. This is the general concept of a class. As we move forward, we will see the example of protected.

This is not a rule of thumb that every data member and member function must be declared in private and public areas respectively. First we need to find out what information we want to keep private and what information we want to keep public. Concentration should be given to this idea first when declaring a class.

Here a small program dateclass.cpp that demonstrates the first two parts of a class mentioned before.

Program Example

  1. //program name: dateclass.cpp
  2. #include
  3. #include
  4. class date
  5. {
  6.           Private:
  7.                      int yy;
  8.           Public:
  9.                      void getyear()
  10. {
  11. cout
  12. cin>>yy;
  13. }
  14. void findleapyear()
  15. {
  16.              if((yy%4==0) && (((yy%100) != 0 || ((yy%400) ==0)))
  17.                 cout
  18.              else
  19.                 cout
  20. }
  21. };
  22. int main(void)
  23. {
  24. date d;
  25. clrscr();
  26. d.getyear();
  27. d.findleapyear();
  28. return 0;
  29. }

Assuming the input is 1977 and 1980

Run output

Enter a value to find the leap year

You entered a non-leap year

Enter a value to find the leap year

You entered a leap year

This program asks he user to input an integer value, i.e; value of the year. This value is then taken from the function getyear() and is then sent to the member function findleapyear() where it finds or compare the input value to figure out whether it is a leap year or not. This program is tested and works perfectly.