question about inheritance


ok,so i have 3 classes,we'll just call them a,b and c.
c is a general purpose class,so a and b both inherit c.

now,everything works fine,but the problem is,when i call a and b in main i have access to the functions in c.
in c there are many functions,of which i only want 1 to be accesible in main,through a and b,and the others not to be.
how would i do this?
i tried setting all the functions except the one to private,didn't work.
i tried setting them to protected and the one func to public,didn't work

i'm really new to this,but having the classes inherit C(general purpose)
really brings a lot of order to my code.

thanks in advance for help :)
Last edited on
if you write class a : public c {...},
a has the same access permissions to the functions, as defined in c
Last edited on
The other option is:
Make just one function protected public and make others private. Then make a and b friends of c.

If this option did not solve your problem, then I'm not sure what will. C++ is not like java where there is a 4th access modifier namely package which limits the scope of some fields/methods to just classes defined in the same package. You might have to reconsider your design method
Last edited on
smac
using your method
in main when i call a.theFunctionIneed();
it says that that function is protected.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//here is what it looks like in the code


//generalPurpose.h
class GeneralPurpose
{
    private:
        int alphabetType_;
        GeneralPurpose();
        virtual ~GeneralPurpose();
        int Determinant2x2(int a[][2]);
        int mod(int a, int b);
        int gcd(int a, int b);
        char toChar(int x);
        int toNum(char x);
    protected:
        void setAlphabetType(int alphabetType);

};

//simpleciphers.h
class Simpleciphers: private GeneralPurpose
{....}
//medciphers.h
class Medciphers: private GeneralPurpose
Last edited on
Use public/protected inheritance to obtain the protected public method(s) as well

1
2
3
4
5
//simpleciphers.h
class Simpleciphers: public GeneralPurpose
{....}
//medciphers.h
class Medciphers: public GeneralPurpose
Last edited on
so to inherit private functions i need to put public
thats kinda confusing
but still
when i try to call
simpleciphers.setAlphabetType();
in main
it still says protected
Take a look at this and see if it makes sense:
http://ideone.com/eXhZTB

Using that, your class will look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//generalPurpose.h
class GeneralPurpose
{
    private:
        int alphabetType_;
        GeneralPurpose();
        virtual ~GeneralPurpose();
        int Determinant2x2(int a[][2]);
        int mod(int a, int b);
        int gcd(int a, int b);
        char toChar(int x);
        int toNum(char x);
    public:
        void setAlphabetType(int alphabetType);
        friend class Simpleciphers;
        friend class Medciphers;
};

//simpleciphers.h
class Simpleciphers: public GeneralPurpose
{....}
//medciphers.h
class Medciphers: public GeneralPurpose
Last edited on
Topic archived. No new replies allowed.