Reputation: 1
Is there a way to access superclass methods from a subclass? I made Apple class as a subclass of Fruit class, but I can not access to setName function in Fruit class from an object of Apple class. Could you give me any advice?
#include <iostream>
#include <string>
using namespace std;
class Fruit {
public:
Fruit(string str)
{
cout << "Fruit class" << endl;
name = str;
cout << name << endl;
}
void setName(string str) {
name = str;
}
private:
string name;
};
class Apple:Fruit {
public:
Apple() :Fruit("apple"){
cout << "Apple class" << endl;
}
void setName(string str) {
name = str;
}
};
int main()
{
Apple apple;
apple.setName("Orange"); //I can not access to setName function from apple
return 0;
}
Upvotes: 0
Views: 545
Reputation: 1
First, change class Apple to
class Apple: public Fruit
then, use this method
apple.Fruit::setname()
Upvotes: 0
Reputation: 14607
Use public
inheritance like this:
class Apple : public Fruit
The default visibility for class
is private
if it's not specified. That is why you're not able to access the public
members of the base class because they're now private due to their private
visibility.
Contrary to a class
, the default visibility for a struct
is public
i.e.:
struct Base {};
struct Derived : Base {}; // public inheritance
In your code, the overridden setName()
method in the derived class is redundant as it cannot manipulate the private data member name
directly. You'll have to use the base class method to set the name
in your overridden method. As of now, you're not doing anything else in that method so you don't need it.
Here's your working code (live):
#include <iostream>
#include <string>
using namespace std;
class Fruit {
public:
Fruit(string str)
{
cout << "Fruit class" << endl;
name = str;
cout << name << endl;
}
void setName(string str) {
name = str;
}
private:
string name;
};
class Apple : public Fruit { // public inheritance
public:
Apple() :Fruit("apple"){
cout << "Apple class" << endl;
}
// void setName(string str) { // Redundant!
// name = str; // `name` is not accessible here!
// }
};
int main()
{
Apple apple;
apple.setName("Orange"); // Accessible here
return 0;
}
For more relevant information, refer to this:
Difference between private, public, and protected inheritance
Upvotes: 2
Reputation: 2399
Use public inheritance
class Apple:public Fruit
in class Default is Private
class Apple:Fruit
is same as
class Apple:private Fruit
You made private here, So you can not access member of Fruit by object of Apple even if it is public.
class Apple:public Fruit
{
public:
Apple() :Fruit("apple")
{
cout << "Apple class" << endl;
}
};
Or if you want it is to private use like this
class Apple:Fruit
{
public:
Apple() :Fruit("apple")
{
cout << "Apple class" << endl;
}
void setName(string str)
{
Fruit::setName(str);
}
};
Upvotes: 0
Reputation: 596352
Your Apple
class is using private inheritance (the default when the type of inheritance is not specified), so all of Fruit
's public
methods are private
in Apple
and thus are not accessible for main()
to call. You need to use public inheritance instead to fix that.
Also, there is no need to re-implement setName()
in Apple
. The inherited setName()
from Fruit
will suffice, once it is inherited as a public method in Apple
.
class Apple : public Fruit {
public:
Apple() : Fruit("apple"){
cout << "Apple class" << endl;
}
};
Upvotes: 0