Class-in-C++ .

In this session i have discussed class in C++ .


A Class is a collection of data members and member functions . A class is a blue print .

Important Point -

1-create the class .

2-create the object of class .

3-call the function with dot operator using object .

Class Syntax :

    class Employee
    {
        private :
            int empid; //data member
            string empname;//data member
        public :
               void display();//memeber function
    };
    

Example : Write a program that demonstrate the use of class with simple example .

    #include<iostream>
    using namespace std;  
    class Employee
    {
       private :
            int empid;
            string empname;
       public :
       void getdata()
       {
          cout<<"\nEnter the Employee ID : ";
          cin>>empid;
          cout<<"\nEnter the Employee Name : ";
          cin>>empname;
       }
       void putdata()
       {
            cout<<"\nEmployee ID : "<<empid;
            cout<<"\nEmployee Name : "<<empname;
       }
    };
    int main()
    {
        Employee obj;  //object created
        obj.getdata(); //getdata function called
        obj.putdata(); //putdata function called
        return 0;
    }
 
    
OUTPUT : Enter the Employee ID : 1
         Enter the Employee Name : Rakesh
         Employee ID : 1
         Employee Name : Rakesh

Example : Write a program that demonstrate the use of class .

    #include<iostream>
    using namespace std;  
    class Operation
    {
        int x=20, y=10, result = 0;
        public :
        void sum()
        {
            result=x+y;
            cout<<"\nSum : "<<result;
        }
        void sub()
        {
            result = x - y;
            cout<<"\nSub : "<<result);
        }

    };
    int main()
    {
        Operation obj;
        obj.sum();
        obj.sub();
    }    
    

    
OUTPUT : Sum : 30
         Sub : 10 

Example : Write a program in which use a function to reverse the given number using class .

    #include<iostream>
    using namespace std;  
    class Operation
    {
        int rem=0, rev=0;
        public :
        void reverse(int n)
        { 
            while(n>0)
            {
                rem = n % 10;
                rev = (rev * 10 )+ rem;
                n = n / 10;
            }
            cout<<"Reverse Number is "<<rev;
        }
    };
    int main()
    {
        Operation obj;
        obj.reverse(123);
    }
    
OUTPUT : Reverse Number is : 321

Example : Write a program in which use a method to sum the individual digit of the given number using class .

    #include<iostream>
    using namespace std;  
    class Operation
    {
        int rem=0, s=0;
        public :
        void Sum(int n)
        { 
            while(n>0)
            {
                rem = n % 10;
                s = s+ rem;
                n = n / 10;
            }
            cout<<"Sum Number is "<<s;
        }
    }
    int main()
    {
        Operation obj;
        obj.sum(123);
    }
    
OUTPUT : Sum : 6
          

Welcome to AndroidATPoint.

back image                     next image