我很难弄清楚如何在 C++ 类之间正确执行组合。这是我遇到困难的一个例子:
#include <iostream>
using namespace std;
class Super
{
private:
int idNum;
string Name;
public:
Super(int, string);
void setSuper();
};
Super::Super(const int id, const string superName)
{
idNum = id;
Name = superName;
}
class Sidekick
{
private:
int sideIdNum;
string sideName;
Super hero;
public:
Sidekick(int, string, Super);
void setSidekick();
};
int main()
{
Super sm;
Sidekick sk;
sm.setSuper();
sk.setSidekick();
return 0;
}
void Super::setSuper()
{
int superID;
string sname;
cin >> superID;
cin >> sname;
Super(superID, sname);
}
void Sidekick::setSidekick()
{
int id;
string siName;
Super man;
cin >> id;
cin >> siName;
//How do I perform action that automatically obtains the attributes
//from the object "hero" from Super?
}