Header Ads Widget

What is class variable and method in C++ || class variable in C++ || class method in C++

 


 

What is class variable?

Class variable is a container of data member which provide memory space to store the data. The value stored of class variable that can be change and reused many times in a program. Class variable can be declare as private, public and protected member.

Syntax:-

            Datatype   variable_name;

Example:-

#include <iostream>
using namespace std;
class A
{
            private:
            int a;
            public:
            void show() 
            {
               a=10;
               cout<<”a=”<<a;
            }
};
int main()
{
A obj;
obj.show();
return 0;
}

Output

a=10



What is class method?

Class method is a group/block of code where the input and output operation performed. It done the process taking input from user, processed it and give output. Class method can always define as public.

Syntax:-

return_type  function_name()
{
            //code

}

Example:-

#include <iostream>
using namespace std;
class A
{
            private:
            int b;
            public:
            void show() 
            {
               b=10;
               cout<<”b=”<<b;
            }
};
int main()
{
A obj;
obj.show();
return 0;
}

Output

b=10

Post a Comment

1 Comments