Class in C++

Class in C++

In this post we will see the concept of class and how to use class in C++ program. Class is a way to bind data and associated functions together. It allows to hide data and functions from external user if necessary. When defining a class, we can create a new abstract data type and we can treat it like any other built in data type.

Generally, a class specification has two parts :
1. Class Declaration.
2. Class Function Definition.

Generally a class declaration looks like :

– The keyword class specifies that whatever follows it is an abstract data type class-name.
– The body of a class is enclosed within braces and terminated by semicolon (;).
– The body of the class contains declaration of class memebrs, which are variables and functions. They are generally grouped under two sections namely private and public. These are known as visibility labels. A colon (:) follows these keyords.
– We can access private members within a class only. It hides data from external use. It is a key feature of OOP.
– We can access public members from anywhere outside the class.
– If both the visibility labels are missing, then by default, the membersof the class are private. Such a class is completely hidden from outside world and does not serve any purpose.

Now we will see an example to understand this concept more clearly.

Q. Write a program in C++ to demonstrate use of class.

1. Program :

//To demonstrate use of class
using namespace std;
class Demo
    private :
        int a, b;
    public :
        void setvalue()
            cout<<"\n Enter values for a and b :";

        void display()
            cout<<"\n Addition of "<< a <<" and"<< b <<" is : "<<(a+b);
int main()
    Demo d;     //creating object

    return 0;    

2. Output :

class in C++ program

3. Program Parts :

1. #include is a pre-processor directive. It is used to include header files.
2. iostream is header file which has certain commands that c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. class is a keyword to create classes.
5. int is data type. It indicates that the program returns a value.
6. main() is main function that indicates the compiler that the user written programs starts from here.
7. cout is the keyword use to print a message.
8. cin is the keyword use to store values in variables.
9. return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation :

We create a class Demo. In this class, in private section we declare two variables. In public section we calculate addition of two numbers. First we ask user to enter numbers and then we perform addition. In main function, first we create an object of the class. This object help us to access members of the class. And finally we end the program.


So this was the concept of class and we saw how to use class in C++ program. We hope you understood it clearly. Id you have any doubt regarding any step or concept just do mention it in the comment section below. Do share it with your friends and programming buddies.

For daily updates do follow us on Instagram! Visit again.



Leave a Reply

Your email address will not be published. Required fields are marked *