Member functions of a derived class - c++

When a class is derived from a base class, will the member functions of the base class become member function of the derived class. That is, for example if I write:
class A
{
public : void f();
};
class B : public A
{
public : void f1();
};
Now if the question is to name the member functions in class B, then will f() also become a member function of class B ?

then will f() also become a member function of class B ?
No. f() is still a member function of class A, class B just inherits it. In your case it's a public member function and public inherit, means you can call f() on a B object.
B b;
b.f();
On the other hand, class B can define own member function f():
class A
{
public : void f();
};
class B : public A
{
public : void f(); // will hide A::f()
};

Yes, class B has two functions: f1() and f().
If f() was protected in A, too, but f() could only be used from within the member functions of A and B (and from friends).
And if f() was private, B would have only one function f1().

class A {
public : void f();
};
class B : public A
{
public : void f1();
};
Now code is valid.
It is visible. Even when you change f1 name to f it is still visible in B class. If you wanna use it use scope operator ::.
Like this:
A::f()
More about basic inheritence
http://www.learncpp.com/cpp-tutorial/112-basic-inheritance-in-c/

Related

Errors on postfix operator ++ in a class derived from a template class [duplicate]

In a derived class If I redefine/overload a function name from a Base class,
then those overloaded functions are not accessable/visible to derived class.
Why is this??
If we don't overload the oveloaded function from the base class in derived class
then all the overloaded versions of that function are available to derived class
objects, why is this??
what is the reason behind this. If you explain this in compiler and linker level
that will be more helpful to me. is it not possible to support this kind of scinario??
Edited
For examble:
class B
{
public:
int f() {}
int f(string s) {}
};
class D : public B
{
public:
int f(int) {}
};
int main()
{
D d;
d.f(1);
//d.f(string); //hidden for D
}
Now object 'd' can't access f() and f(string).
TTBOMK this doesn't have a real technical reason, it's just that Stroustrup, when creating the language, considered this to be the better default. (In this it's similar to the rule that rvalues do not implicitly bind to non-const references.)
You can easily work around it be explicitly bringing base class versions into the derived class' scope:
class base {
public:
void f(int);
void g(int);
};
class derived : public base {
public:
using base::f;
void f(float);
void g(float); // hides base::g
};
or by calling the explicitly:
derived d;
d.base::g(42); // explicitly call base class version
The functions are available, you just need to call them explicitly:
struct A {
void f(){}
};
struct B : public A {
void f() {}
};
int main() {
B b;
b.f(); // call derived function
b.A::f(); // call base function
}

Define the undefined method of base class in derived classes

I have a base class A and two classes B, C derived from A. Declaration of method func is given in class A. How can I define method func separately for B and C ?
class A {
public:
void func();
};
class B : public A {
//some members
};
class C : public A {
//some members
};
//define B's func here without changing the definition of the three classes
//define C's func here without changing the definition of the three classes
You have to make the method you want to overwrite "virtual" or "pure virtual" and if a class has virtual methods, also the destructor must be virtual:
class A {
public:
virtual ~A{};
virtual void func() = 0;
};
class B : public A {
void func() {};
};
class C : public A {
void func() {};
};
No, you cannot implement a member function for a class without it being declared in the class.
class A {
public:
void func();
};
class B : public A {
//some members
};
class C : public A {
//some members
};
void B::func() {}
void C::func() {}
/tmp/164435074/main.cpp:17:9: error: out-of-line definition of 'func' does not match any declaration in 'B'
void B::func() {}
^~~~
/tmp/164435074/main.cpp:18:9: error: out-of-line definition of 'func' does not match any declaration in 'C'
void C::func() {}
^~~~

c++ derived class with private access specifier

I have a derived class (class B) from a base class (class A). Class A has a protected virtual function foo() which I want to override and use it as private in derived class.
Class A{
protected:
virtual void foo() = 0;
}
I am wondering whether the following
Class B: public Class A
private:
virtual void foo();
and
Class B: private Class A
private:
virtual void foo();
are the same.
They are not the same. In the first example, B is-an-A, in the second it isn't. So in the first case you can have code such as
void foo(const A& a);
which accepts A and B as arguments. With private inheritance, you could not do that. For example,
A a;
B b;
foo(a); // OK with private and public inheritance
foo(b); // OK only with public inheritance, i.e. B is an A.
No, your two cases are not the same.
In the second case class B can't be casted to class A, because a private base class is hidden. in this aspect would get the same behavior as if class A would be a member of class B.
No both are not same
In public inheritance class A's foo() will be protected member in class B
In private inheritance class B can only access the public members of the class A so there will not present any foo() of class A in class B.

how can a derived class function call a function of the base class?

Derived class function cannot access even the public members of the base class when the access specifier is private. But how is it that the function 'xyz' of my derived class able to call 'showofb'?
I even tried it by calling the function 'showofb' in the constructor of C. In both cases it works.
How is it able to call the function 'showofb' ?
class B
{
public:
B()
{
cout<<":B:"<<endl;
}
void showofb()
{
cout<<"show of b"<<endl;
}
};
class C : private B
{
public:
C()
{
cout<<":C:"<<endl;
}
void xyz()
{
showofb();
}
};
int main()
{
C c1;
c1.xyz();
}
Private inheritance inherits the public members of the parent as the private members of the child. A class can call its own or inherited private members.
Consider this:
class B
{
public:
B()
{
cout<<":B:"<<endl;
}
void showofb()
{
cout<<"show of b"<<endl;
}
};
class C : private B
{
public:
C() {}
};
class D : public B
{
public:
D(){};
}
int main()
{
C c1;
c1.showofb(); // WONT WORK
D d1;
d1.showofb(); // WILL WORK
}
B::showofb() is a public function. So it can be called by C. If you modify B to make showofb private, C will no longer be able to call it.
The private inheritance means that all public and protected members of B are inherited as private by C. So C can still call public and protected members of B, but any classes derived from C will not be able to call members of B.
user1001204, you appear to have a mistaken concept of private inheritance. That class C inherits from class B via private inheritance means that the inheritance relationship is hidden to anything that uses class C. Private inheritance does not hide the inheritance relationship inside Class C itself.

overloaded functions are hidden in derived class

In a derived class If I redefine/overload a function name from a Base class,
then those overloaded functions are not accessable/visible to derived class.
Why is this??
If we don't overload the oveloaded function from the base class in derived class
then all the overloaded versions of that function are available to derived class
objects, why is this??
what is the reason behind this. If you explain this in compiler and linker level
that will be more helpful to me. is it not possible to support this kind of scinario??
Edited
For examble:
class B
{
public:
int f() {}
int f(string s) {}
};
class D : public B
{
public:
int f(int) {}
};
int main()
{
D d;
d.f(1);
//d.f(string); //hidden for D
}
Now object 'd' can't access f() and f(string).
TTBOMK this doesn't have a real technical reason, it's just that Stroustrup, when creating the language, considered this to be the better default. (In this it's similar to the rule that rvalues do not implicitly bind to non-const references.)
You can easily work around it be explicitly bringing base class versions into the derived class' scope:
class base {
public:
void f(int);
void g(int);
};
class derived : public base {
public:
using base::f;
void f(float);
void g(float); // hides base::g
};
or by calling the explicitly:
derived d;
d.base::g(42); // explicitly call base class version
The functions are available, you just need to call them explicitly:
struct A {
void f(){}
};
struct B : public A {
void f() {}
};
int main() {
B b;
b.f(); // call derived function
b.A::f(); // call base function
}