-9

I am writing a c++ program where i need to call a private member function from main function. Please look at my code:

#include <iostream>

using namespace std;

class cricket
{
private:
    int target_score;
    int overs_bowled;
    int extra_time;
    int penalty;

    void cal_penalty()
    {
        if(extra_time <=10)
            penalty = 1;

            else if(extra_time <=20)
             penalty = 2;

             else
                    penalty = 5;
    }

    public:
        void extradata()
        {
            cout << "\nEnter Target Score : ";
            cin >>target_score;

            cout <<"\nEnter Overs bowled : ";
            cin >> overs_bowled;

            cout << "\nEnter extra time : ";
            cin >> extra_time;
        }

        void displaydata()
        {
            cout << "\nTarget Score : "<< target_score;
            cout << "\nOvers Bowled: " << overs_bowled;
            cout << "\nExtra Time : " << extra_time;
            cout << "\nPenalty : " <<penalty;
        }
};

int main()
{
 cricket c1;
c1.extradata();
c1.displaydata();

return 0;
}

Here i am getting all the output correctly but i am confused how to display the value of penalty after checking the extratime. Please edit my program so that i can get the value of penalty based on the input of extra time

bhaskar07
  • 115
  • 2
  • 3
  • 12

4 Answers4

3

You can't call private member functions from main. Your public member functions can call their private functions though.

I guess you need to recalculate your penalty after receiving data:

void extradata()
{
        cout << "\nEnter Target Score : ";
        cin >>target_score;

        cout <<"\nEnter Overs bowled : ";
        cin >> overs_bowled;

        cout << "\nEnter extra time : ";
        cin >> extra_time;

        cal_penalty();
}
DanDan
  • 10,462
  • 8
  • 53
  • 69
1

I am writing a c++ program where i need to call a private member function from main function.

You cannot. That's precisely the point of making it private in the first place!

Either make the function itself public or add another public function to call the private one:

class cricket
{
private:
    // ...
    void cal_penalty_impl()
    {
        // Your original code goes here
        // ...
    }

public:
    // ...
    void cal_penalty()
    {
        cal_penalty_impl();
    }
};

This solution is nice and flexible.


For the sake of completeness, there is one other way, actually. You can make main a friend of cricket:

class cricket
{
    friend int main();
    // ...
};

But that's very esoteric, confusing, unexpected, strange and unmaintainable; one of the things good to know but bad to do.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
0

You can only call private member functions from within the class, or from its friends (friends, however, should be avoided).

If the function should be callable from outside the class, make it public.

  • -1 for "friends should be avoided". They should be avoided for `main`, of course, but are generally a great feature to enhance encapsulation. https://isocpp.org/wiki/faq/friends#friends-and-encap – Christian Hackl Aug 15 '14 at 09:56
  • @ChristianHackl: [Note that the definition of "avoid" does not imply that it indicates that something should never be used/encountered/et cetera.](http://en.wiktionary.org/wiki/avoid) Therefore saying `friend` should be avoided is no different (or less valid) than saying `goto` should be avoided. Both have valid use cases wherein they make code more readable, more maintainable, et cetera. The issue is that in neither case are those uses cases exceptionally common. – Robert Allan Hennigan Leahy Aug 15 '14 at 09:59
  • @RobertAllanHenniganLeathy: I know what "avoid" means. And it is still wrong; `friend` may not be a feature as commonly used as `public` or `private`, but you will encounter it a million times more often in well-written code than `goto`. To say that something should "never be used" is always wrong, so that's a moot point. Given the right circumstances (including non-technical ones, typically time pressure + horrible legacy code base), even a `goto` may be a good choice. – Christian Hackl Aug 15 '14 at 10:04
  • 1
    @ChristianHackl. The link you posted stated `...Thus the ability to choose between member functions (x.f()) and friend functions (f(x)) allows a designer to select the syntax that is deemed most readable, which lowers maintenance costs.` ***Imho this is a weak argument***. But the one about floating point arithmetic stands. – UmNyobe Aug 15 '14 at 10:05
  • @UmNyobe: it becomes stronger when you consider operator overloading. The part about "You often need to split a class in half when the two halves will have different numbers of instances or different lifetimes" is more important anyway IMO. From experience, iterator classes are a great example application of this pattern. – Christian Hackl Aug 15 '14 at 10:09
  • @ChristianHackl: Or you could just implement the iterator as a nested class. – Robert Allan Hennigan Leahy Aug 15 '14 at 10:11
  • @RobertAllanHenniganLeahy: http://stackoverflow.com/questions/5013717/are-inner-classes-in-c-automatically-friends – Christian Hackl Aug 15 '14 at 10:20
  • @ChristianHackl: The standard states: "A nested class is a member and as such has the same access rights as any other member." §11.7 [class.access.nest] – Robert Allan Hennigan Leahy Aug 15 '14 at 10:23
0

You can only call it inside your class , ofcourse you can call it inside public method so you can use it in your main (out of your class).