about-text

SoftOSet is the most remarkable programming site. SoftOSet provides the fresh conceptual and professional programs, articles, books, and other interesting and technical writings. SoftOSet is the most reliable programming site to learn the programs and the basics of the popular programming languages. SoftOSet is the hub of the informative and research based articles.

Call the Function of Class without creating an object Program in C++ Program

This C++ program is of the static keyword. How static keyword is used to access the function of a class without creating an object of the class. One thing to be noted that, In normal routine it is not possible to access the any function of the class without creating any object. Object creation is necessary, but using the static keyword we access the class using the class name scope resolution operator and function name.

 

Access the method of class without creating object In C++ using class

One thing to be noted that these programs are compiled and tested on the Dev C++  IDE. So if you want the same output from them download and install the Dev C++ from here and then run the code.

Code

#include<iostream>

using namespace std;

class Test{

            private:

                        static int n;

            public:

               static void show()

               {

                        cout<<"Value of N = "<<n<<endl;

               }

};

int Test::n=50;

int main()

{

            Test t;

            Test::show();

            return 0;

}

Output

And here is the sample output of this code. If you have any error about the code then contact us. We will surely help you to run your code and resolve your error.

 

 

 

 

 

Explanation

What I have do in this code is just simply that, I have included the header file of the <iostream>. If you are using any other compiler instead of Dev C++, then you must have to add some other headers according to the requirement of your compiler. The next statement is using namespace std;. It is just used in the Dev C++, if you or using any other compiler then you probably no need to add this statement.

Class Test

In test class we have declared a static variable n and a static function show using the static keyword.

int Test::n=50;

We have defined the static variable in this format because static variable always defined outside the body of the class.

 

main()

In main we have created no object of the Test class we have simply call the function show using the using the name of class and scope resolution operator and writing the name of the function.

Another Static Function Program

Code

 

#include<iostream>

using namespace std;

class Test{

            private:

                        static int n;

            public:

                        Test()

                        {

                                    n++;

                       

                        }

                        static void show()

                        {

                                                cout<<"You Have Created "<<n<<" Objects So Far ..............."<<endl;

                        }

};

int Test::n=0;

int main()

{

            Test::show();

            Test t,m,n,b;

            t.show();

            Test::show();

            return 0;

}

Output

And here is the sample output of this code. If you have any error about the code then contact us. We will surely help you to run your code and resolve your error.

 

Note: You only call the static function in this manner.

That’s it.

At the end I have returned the control back to the system using the

return 0;

statement.

Conclusion

Now I conclude the whole story in a single sentence

“Static function can be called without any object”.

If you have any type of bug or error in your code feel free to take guidance from us. Our team will try its best to help you to remove your bug.

 

Thank you

 

 

 

 

 

 

 

 


Post a Comment

Feel Free to Comment Us

Previous Post Next Post

Recents