≡ Menu

Introduction to C++ Classes and Objects using a Hello World C++ Program

Classes and objects are fundamental concepts of object oriented languages. To learn an object oriented language such as C++, you should have a sound knowledge of classes and objects.

This tutorial explains concept of object and classes using an hello world example program.

This is the 1st article in our on-going new series on C++ programming.

Classes and Objects

If you have some basic knowledge of C programming language (Hello World C Program), then you must be familiar with the concept of structures. Here is how a structure looks in C:

struct random{
int num;
char ch;
float marks;

You’ll observe that a structure is just a collection of various types of data in C. In the above example, all these variables can be accessed through the structure object obj.

Just the way structure encapsulates only data, classes can encapsulate data as well as functions. Also, in the same way, object refers to instantiation of the class.

Here is an example of a class declaration in C++ :

class example{
int var1, var2;
void set_request(int a, int b);
int sum(){return (var1+var2);}

So you see that the above example contains data (var1 and var2) and also a couple of functions. Also, you will see a string ‘public:’ before the function declarations. This is nothing but an access specifier that specifies the access permissions of the data and functions defined after it.

There can be three access specifiers in C++ :

  • Private : This access specifier makes sure that the private members can be accessed from other members of the class only or from the friends of class (this is an advanced concept, will learn it later).
  • Protected : This access specifier makes sure that the protected members can be accessed from members of the same class ,from the friend classes and also from members of the derived classes.
  • Public : This access specifier makes sure that the public members can be accessed from anywhere through the object of the class.

Coming back to the example program, it’s time now to expand it further and define the function set_request.

Here is the example :

class example{
int var1, var2;
void set_request(int a, int b);
int sum(){return (var1+var2);}

void example::set_request(int a, int b)
    var1 = a;
    var2 = b;

In the above example, you will observe that the function set_request() is defined outside the class. The :: operator used in the definition is the syntax for defining class functions outside of the class. Basically, the :: operator is known as scope resolution operator, so this operator tells the compiler about the class scope of the function.

If you observe closely, you will find that the function sum() is defined within the class but the function set_request() is defined outside of the class. There is only one difference between the two, the functions declared inside the class are treated as Inline functions while those defined outside the class are treated as normal functions.

Now, lets complete our example program by defining a main() function and calling the functions of the class from it.

Here is the example :


class example{
int var1, var2;
void set_request(int a, int b);
int sum(){return (var1+var2);}

void example::set_request(int a, int b)
    var1 = a;
    var2 = b;

int main(void)
    std::cout<<"\n The sum is "<<class_obj.sum()<<"\n";

    return 0;


Here is the output :

$ ./example 

 The sum is 5

So we can see that inside the main() function, the class object class_obj was used to call the function set_request() with parameters 1 and 4. Next, a call to sum() was done with similar style to return the sum of numbers. The basic header file used for C++ programs in iostream.h as compared to stdio.h which is used in case of C programs.

NOTE : The members var1 and var2 are private members despite no access specifier was defined before declaring them. This is because private is the default access specifier and there is no requirement of mentioning it separately. Though it is advisable to do so to improve the code readability.  Also, as the variables var1 and var2 are private members of the class so they can only be accessed within class functions (and friends of class) but not inside the main() function directly.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Bob February 14, 2013, 10:33 am

    Thanks. Good that you can explain the basics with a few clear examples.

  • Bob February 14, 2013, 10:39 am

    I always have problems differentiating between private and protected. What is the key difference between them?

  • Arlen February 14, 2013, 1:45 pm

    The frequent question I get is why bother with classes, all this data hiding stuff, etc. There are many possible answers, but my answer is that for non-trivial programs, 90% of the costs associated with a program is during the post release phase, where the original developers either have forgotten what they did during the development phase or have went on to better positions or development teams. A well-thought-out C++ program compartmentalizes the data that the member functions operate on and expose the required functionality through a clearly defined interface for use elsewhere in the program. This allows for code maintenance to be preformed by less skilled developers than the original developers since they can focus on much smaller sections of code that are the only ones that touch the private data in the class they are working on. This also allows one to re-write slow classes to optimize overall program performance by changing the implementation of the member functions. As long as the signature of the functions are not changed, the rest of the program will not require modification.

  • F Diaz February 15, 2013, 7:56 am

    I´ve never learnt the right use of .h files during compilation so I´ve always writted the whole code in a .cpp file
    can you please make one tutorial for compiling from multilple files?

  • OM PRAKASH SINGH February 15, 2013, 8:03 am

    Really it is wonderful and useful for the new beginer like me.

  • Arlen February 15, 2013, 10:07 am

    Bob: Class member functions and data are private by default in C++, only member functions inside the class can access these functions and data. If you choose to make member functions or data inside a class public, then these functions and data are visible to parts of the program outside the class. Protected is used with base classes and derived classes. Assume you have developed a class that has most of the functionality of what you need, but you want to extend it a bit to handle a new situation. It may make sense to make the original class a base class, and derive a new, derived class from it. Any member functions and data in the base class that do not need to be accessed from the derived class can remain private, but if the derived class needs to access data or member functions in the base class but the main program has no need to directly access them, the base class data or member functions should be changed to protected. (Protected data or member functions are visible to derived classes, but are not visible outside the class).

  • DuskoKoscica March 1, 2013, 8:19 am

    Well, for the one that did not use heder files, and cpp files. You make class heder in h file, and cpp file contains implementation. In implementation, you will put bigger functions.
    The short functions are not for the cpp file that goes with the heder file…
    That is the way, but I do not use the way of copile, that would be ..
    The private is for this class, and protected for the class and inherited

  • Michael March 1, 2013, 11:49 pm

    Any Recommendation for Linux compiler options and library’s used for c++ would be nice. Being a novice to c++ I would like to know of a good Dev env for c++. Thanks for your time presenting this tutorial! Much appreciated.

  • DuskoKoscica March 4, 2013, 2:37 am

    YES the class is the way to go…
    But, I have few ideas. First of all, the UML is not used the way I would like it, it would be way better, in my oppinion, to us it instead of wizards, like MS Windows C++ does.
    And it should be impruved in some way, becuse it is not good for C++, for pointers and some other things.
    And, I think that the nice esy GUI, development enviroment, like QT would be must for any C++.
    C11 has some impruvements, but some are not solved.
    Like, Java is vey better when it comes to work with connecting and things…
    Delfi 7 had the best enviroment for small aps, it should be merged with C++ to create perfect thing for many programers

  • DuskoKoscica March 4, 2013, 2:41 am

    And yes, befor I forget this..
    Instead of large numbers of API, that Microsoft users need this days,
    I would create class MyPs to se what is on Ps, MyMonitor to manipulate the monitor, MyCpu for cpu and so on..
    The thing would impruve the coding in many ways..

  • Hemanth April 16, 2013, 1:40 am

    Gud basic stuff’s

  • vinod June 19, 2013, 2:44 pm

    Hi Ramesh Natarajan,

    A good tutorial again….really reminded my engineering classes in the college again…recalled the concepts which i used to study in balagurswamy text book.

    Thanks for the post…Have a great journey