-
class inheritence
Is it possible to inherit from a class, and remove some of the functions? Like below, is it possible to remove the function Do3() from the Thing2 class and remove Do2 from the Thing3 class? So it's not available when you make a new Thing2?
Code:
class Thing1
{
int a;
int b;
int c;
void Do1();
void Do2();
void Do3();
}
class Thing2: public Thing1
{
int z;
void DoMore();
}
class Thing3: public Thing1
{
int y;
void DoLess();
}
-
No, and that would be terrible if you could, it would destroy the entire idea of inheritance and polymorphism.
If the child class cannot do *EVERYTHING* the parent class can, it should not BE a child class. Either derive both from the same parent class, or make them entirely different classes.
-
The purpose of a base class is to have something that is just that, a base. One of the nicest things about inheritance is that you can have pointers or references to a base type, which actually point to the derived type, and you can use the base class operations on them (i.e. you do not actually have to know exactly what class you are dealing with). If you were able to remove some of these operations from a derived class, the result would be a disaster.
-
Here's how you do it neandrake:
Code:
class A
{
public:
void foo1() {}
void foo2() {}
void foo3() {}
};//A
class B : private A // B controls access to A
{
public:
void foo1() {A::foo1();}
void foo2() {A::foo2();}
// you can't call foo3()! fool
};//B
gg
-
-
Well, when you block acess to one variable, you have to provide access to the others:
Code:
class A
{
public:
int a, b, c;
void foo1() {}
void foo2() {}
void foo3() {}
};//A
class B : private A // we control access to A
{
public:
void foo1() {A::foo1();}
void foo2() {A::foo2();}
// you can't call foo3()! fool
int& get_a() {return a;}
int& get_b() {return b;}
// no c for you!
};//B
gg
-
thanks codeplug, I got your email, lol
I really appreciate the help
I was going to email you back through the board, but you have that option turned off.
Were you interested in looking at my winsock class?
btw, for variables, wouldn't the function get_a() be return A::a; ?
-
also, are constructors of A called when you create a new B?
-
The "A::" isn't needed since the compiler knows what you want.
If you declared a method in B like: "void a()", then "A::a" tells the compiler wich "a" your talking about.
gg
-
Probably not important but...
Well, you're not really removing something from the base class. Actually, everything is a matter of access rights, there is no way a derived class could not have a member of its parent, just because they have to behave like their parents in some cases so the data must have the same structure.
All you can do is restrict these rights, when you declare, for example, private access to some fields of a class, you're saying that your intent is not to let external functions and derived class functions have it manipulated.
As well, when you declare, like codeplug did, a superclass to be private, all you are saying is the members inherited from that parent should be private to the class.
Though, your derived class has access to those members, so you don't have to write A::a.
-
Standards-compliant compilers have a different way of granting access to members of private/protected base members. The using keyword.
Code:
class A
{
public:
void do();
void dont();
};
class B : private A
{
public:
// Grant access to do:
using A::do;
};
-
Isn't it only needed for multiple inheritance?
-
Don't know about needed, but it certainly is the modern way to make members of private base classes visible.
-
Just didn't understand what you were talking about. Never mind. -_-
-
Yes, that's much better way of doing it.
gg