@gg123xyzthis code is a bit more complicated since the previous code was too crude.. the first example is bad because the Goblin class set is accessible from the unicorn class:
#include<iostream>
#include<string>
using namespace std;
class Creature
{
public:
Creature();
void SetName(string name);
string GetName();
private:
protected:
string Name;
};
class Goblin : private Creature
{
public:
Goblin();
void SetName(string name);
string GetName();
};
class Unicorn : private Creature
{
public:
Unicorn();
Goblin Gobby;
};
int main()
{
Goblin Gobby;
cout << Gobby.GetName() << endl;
Unicorn uni;
system("pause");
}
Creature::Creature()
{
cout << "A Creature has been created\n";
}
void Creature::SetName(string name)
{
Name = name;
}
string Creature::GetName()
{
return Name;
}
Goblin::Goblin()
{
SetName("Gobby");
}
void Goblin::SetName(string name)
{
Name = name;
}
string Goblin::GetName()
{
return Name;
}
Unicorn::Unicorn()
{
Gobby.SetName("Green Goblin");
cout << Gobby.GetName() << endl;
}
this second example is good because the Goblin Set is not accessible from the unicorn class:
#include<iostream>
#include<string>
using namespace std;
class Creature
{
public:
Creature();
void SetName(string name);
string GetName();
private:
protected:
string Name;
};
class Goblin : private Creature
{
public:
Goblin();
string GetName();
};
class Unicorn : private Creature
{
public:
Unicorn();
Goblin Gobby;
};
int main()
{
Goblin Gobby;
cout << Gobby.GetName() << endl;
Unicorn uni;
system("pause");
}
Creature::Creature()
{
cout << "A Creature has been created\n";
}
void Creature::SetName(string name)
{
Name = name;
}
string Creature::GetName()
{
return Name;
}
Goblin::Goblin()
{
SetName("Gobby");
}
string Goblin::GetName()
{
return Name;
}
Unicorn::Unicorn()
{
//Gobby.SetName("Green Goblin"); commented out because setname is inaccessible
SetName("Uni");
cout << Gobby.GetName() << endl;
cout << GetName() << endl;
}