Q:

C++ Static Variables and Functions | Find output programs | Set 5

belongs to collection: C++ find output programs

0

This section contains the C++ find output programs with their explanations on C++ Static Variables and Functions (set 5).

Program 1:

#include <iostream>
#include <stdlib.h>
using namespace std;

class Sample {
    static int A;

public:
    Sample()
    {
        A++;
    }
    static void fun()
    {
        cout << A << endl;
    }
};
int Sample::A = 0;

int main()
{
    void (*F_PTR)();

    Sample S1, S2;

    F_PTR = Sample::fun;
    F_PTR();

    return 0;
}

Program 2:

#include <iostream>
#include <stdlib.h>
using namespace std;

class Sample {
public:
    static void fun1()
    {
        cout << "Hello ";
    }
    static void fun2()
    {
        cout << "Hiii ";
    }
};

int main()
{
    void (*F_PTR)();

    Sample S1, S2;

    F_PTR = Sample::fun1;
    F_PTR();

    F_PTR = Sample::fun2;
    F_PTR();

    return 0;
}

Program 3:

#include <iostream>
#include <stdlib.h>
using namespace std;

static class Sample {
public:
    static void fun1()
    {
        cout << "Hello ";
    }
    static void fun2()
    {
        cout << "Hiii ";
    }
};

int main()
{
    void (*F_PTR)();

    Sample S1, S2;

    F_PTR = Sample::fun1;
    F_PTR();

    F_PTR = Sample::fun2;
    F_PTR();

    return 0;
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer  Program 1:

Output:

2

Explanation:

Here, we created a class Sample that contains a static data member A, constructor, and one static member.

In the main() function, here we created the function pointer that is initialized with static member function of class, there is no need to mention static keyword with a function pointer. And, we created two objects S1 and S2 then we called static member function fun(). Then it will print 2 on the console screen.

Answer  Program 2:

Output:

Hello Hiii

Explanation:

Here, we created a class Sample and two static member functions fun1() and fun2(). And, in the main() function, we created a function pointer that points fun1() and fun2() and called both functions. Then they will print "Hello Hiii" on the console screen.

Answer  Program 3:

Output:

Hello Hiii

Explanation:

Here, we created a class Sample and two static member functions fun1() and fun2(). And, in the main() function, we created a function pointer that points fun1() and fun2() and called both functions. Then they will print "Hello Hiii" on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ find output programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C++ Operator Overloading | Find output programs | ... >>
<< C++ Static Variables and Functions | Find output p...