inheritance-

Hi,

I want to write a class. I should use a public function to open a file and get variables from it, do some calculations with these variables and then write my outputs to another file.

This function is an ordinary public function.

But in this function i should also use another function to calculate something. How can i use it? should i define this second function as protected and if so, how is the implementation?
closed account (o3hC5Di1)
Hi there,

This tutorial explains inheritance: http://cplusplus.com/doc/tutorial/inheritance/

Basically, if this calculate function you talk about will only be used internally in the class, and thus not a part of its interface to the outside world (ie code using the class), then it should be private (if it should be hidden from derived classes as well), or protected (if derived classes should inherit it and be able to use it).

Implementation can be as simple as:

1
2
3
4
5
6
7
class example
{
    public:
        foo() { bar(); }
    protected:
        bar() { /* ... */ }
};


Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.