2

Just need to know if my way of calling the function areacalc(), inside the function float getarea() is valid or not. just need to know if I can call a private member function inside an accessor function or not. areacalc() is a private member function and float getarea() is an accessor function.

class triangle
{
private:
    float s1,s2,s3;
    float s,area;
    void areacalc()
    {
        s=(s1+s2+s3)/2;
        area=sqrt(s*(s-s1)*(s-s2)*(s-s3));  
    }
public:

    float getarea ()                       
    {
       areacalc();
       return area;
    }
};
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67
  • 5
    You could ask your compiler. Just try it and see what happens. –  May 12 '19 at 15:06
  • The code is working fine ... i am getting the answer too ......i just need to know weather it is a right practice to call a private member function inside an accessor function or not –  May 12 '19 at 15:18
  • Ahhh, that's a different question then. Whether something CAN be done and whether something is good practice are two completely different things. –  May 12 '19 at 15:19
  • i have skipped lot of code here .... Other accessors ,mutators and constructors... are not written here –  May 12 '19 at 15:21
  • @ManpritSingh -- "I have skipped" -- yes, that's obvious. And appropriate for a minimal example. – Pete Becker May 12 '19 at 15:29
  • 1
    This **must be** a duplicate, but I can't fine one. Yes, member functions can call private member functions. And you're right to ask; "try it and see what happens" doesn't answer your question, since many compilers don't strictly adhere to the language definition. – Pete Becker May 12 '19 at 15:34
  • Yes, my "try and see" comment is a little terse, and deserves explanation. What I "should" have said is: in order to be able to answer that question, I would need to understand where OP's hesitation comes from. If the question contained: "sure, it compiles, but I'm still unsure because of xyz", then an an answer can be formed around that. As it stands it's just a bit too vague. –  May 12 '19 at 15:39
  • actually the code is working fine. and i have implemented my problem with two methods . i have already written the code of first method , –  May 12 '19 at 15:41
  • float getarea() { s=(s1+s2+s3)/2; area=sqrt(s*(s-s1)*(s-s2)*(s-s3)); return area; } }; –  May 12 '19 at 15:43
  • 1
    in this second way... i have deleted the private member function of finding area. –  May 12 '19 at 15:45
  • Possible duplicate of [When/why to make function private in class?](https://stackoverflow.com/questions/4505938/when-why-to-make-function-private-in-class) – JeJo May 12 '19 at 15:51

1 Answers1

1

Yes you can! More seriously that is exactly why private methods exists: they do internal operations and are called from other (possibly public) methods of the same class.

Public members (methods and data members) constitute what is often called the interface of the class. That means what can be directly used from outside of the class. Private (or protected which is the same for this point) constiture the private implementation. At a maturity level, the interface part is expected to be stable, while the implementation part can change at any time.

So your design is perfectly clean here:

  • float getarea() is a public accessor and can be used from anywhere
  • void calcarea() is a private method that is called from that public method

A nice encapsulation example.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252