#6-Classes and objects in C++-Theory with programs

Classes and objects in C++


A class is a user defined data type.

So, we can also say that class is an extension of the structure in C programming language.

But, let’s notice that there is a one big difference between these two.

In structure, by default; all the members of the structure are public.

But, in class, all the members of the class are private; by default.

Let’s further discuss about class. A class is the collection of data members and member functions.

Class is an example of encapsulation.

Class class_name







Access-specifiers or visibility modes-:

The access specifiers are nothing but the security keywords in c++. By security keyword, I mean to say, private, public and protected. And, till we don’t discuss about inheritance, private and protected are assumed to be the same thing because they will produce the same effect here.

So, the three access specifiers are as follows-:

  • Private
  • Public
  • Protected

So, now let’s explain the above three points in detail

  • Private-:

By default, all the members of class are private.

The private informations of the class is not available for functions and classes, outside the class. Only the member functions of a class can have access upon the private data members of the class.(This is important property)

  • Public-:

The public access specifiers helps us to have access of information publicly; i.e even from outside of the class.

  • Protected-:

Let me repeat this thing again. The private and protected access specifiers have the same effect till there is no inheritance in the program. Protected information of a class is available in derived class but private information of a class is not available in a derived class.

Now, let’s discuss about the defining of a member function of a class either inside or outside of the class definition-:

So, at first we will learn about defining member function of a class inside a class.

# Defining member function of a class inside a class-:

# Defining member function outside the class-:

That :: is a new thing. It is also called as scope resolution operator. At this level, there is not much to know about the scope resolution operator, except that it is used for declaring classes member functions outside of the class.

Now, let’s discuss a complete program that shows the concept of class and object; as well as defining a member function inside the class and defining the member function outside of a class.

(Q) Write a program with a class that represents time and member functions to read and display the time.

The member function to read time should be declared inside the class and should be defined outside of a class. The member function to display time should be defined inside of a class.

Now, the output of the above code will be like this-:

SO, let’s discuss how the program flow goes like; in this program.

First, you should know that the program starts from int main(). i.e the flow of the program has nothing to do with the functions that are declared inside class, outside class etc. And, remember that the operation on class is also not done at first.

It all starts with the int main() function.

So, inside the main function(), at first, we have; time t1,t2. This is a way of declaring an object.

As discussed earlier in  this article, the way of making objects of a class is that we do, simply by following the below syntax-:

class_name object_name;

And, here, we did-:

time t1,t2;

i.e we made 1 object as t1 and the another object as t2.

Now, we call the read_time() function by t1 at first. SO, by now, we will go inside the-:

void read_time();

and this will take us to

void time::read_time()


//code as above


SO, inside that function that is defined outside the class(SO WE USED THE SCOPE RESOLUTION OPERATOR ::)

So, the user will be asked to enter the values for hour, minute and second.

Then we again do-:


so, same thing for t1 will happen for t2.

Finally, we go to-:


This will takes us to the display function, where the output will be shown as




And, the same thing will happen for the object t2, which is shown as-:


Here, in the above figure, we can see that the 2 objects t1 and t2 contains the members hr,min and sec as defined inside the class.

And, some memory gets allocated for storing them, we get values from the user and finally display the output.

So, this was the program flow explanation. Once, you will reach in a stage of programming, where you won’t need this type of explanation of code and will start understanding the code by yourself.

I believe that, it will happen sooner if you follow my blog with due interest and hard work.

I still think that, explanation of the code is not necessary. What do you think? Comment me on the comment section. You can also message me on facebook page(at the end of the page if you are using mobile, and at the right side if you are using computer)

Write in your copy, what you understood from this blog, don’t read only. Write the source code of the program that I do here in your own code::blocks. And, that’s the only way to be a good programmer in C++.

So, this was all for today. I hope you understood the concepts of classes and objects very well. I also believe that you will not have to go to read any other blogs, once you read this blog seriously. So, stay conscious, keep learning.


Be the first to comment

Leave a Reply

Your email address will not be published.