如何使类成员函数在c ++中的另一个类成员函数上工作?

时间:2013-02-24 05:16:51

标签: c++ class function

我从here获得了大部分代码,但我应该使用类。

我想要做的是,如果我致电cube.moveTopc(),我希望它能够在cube.display()上工作。 display()将输出展开的rubiks多维数据集,然后moveTopc()移动多维数据集的顶部。

class Rubiks
{
public:

char Top[10];
char Bottom[10];
char Front[10];
char Left[10];
char Back[10];
char Right[10];        

void display() { 

    char topColor = 'B';
    char bottomColor = 'G';
    char frontColor = 'W';
    char backColor = 'Y';
    char leftColor = 'O';
    char rightColor = 'R';
    std::fill (Top, Top + 10, topColor);
    std::fill (Bottom, Bottom + 10, bottomColor);
    std::fill (Front, Front + 10, frontColor);
    std::fill (Back, Back + 10, backColor);
    std::fill (Left, Left + 10, leftColor);
    std::fill (Right, Right + 10, rightColor);   

std::cout<<"                                     "<<Top[0]<<Top[1]<<Top[2]<<"\n";
std::cout<<"                                     "<<Top[3]<<Top[4]<<Top[5] << "\n";
std::cout<< "                                     " << Top[6]<< Top[7] << Top[8]    <<"\n\n";

std::cout<<"                                 " << Left[0] << Left[1] << Left[2];
std::cout<< " " << Front[0] << Front[1] << Front[2];
std::cout<<" " << Right[0] << Right[1] << Right[2] << "\n";

std::cout<< "                                 " << Left[3] << Left[4] << Left[5];
std::cout<< " " <<Front[3] <<Front[4] << Front[5];
std::cout<< " " <<Right[3] << Right[4] <<Right[5]<< "\n";

std::cout<< "                                 " << Left[6] <<Left[7] << Left[8];
std::cout<< " " << Front[6] << Front[7] << Front[8];
std::cout<< " " << Right[6] << Right[7] << Right[8]<<"\n\n";

std::cout<< "                                     " << Bottom[0] << Bottom[1] << Bottom[2] << "\n";
std::cout<< "                                     " << Bottom[3] <<Bottom[4] << Bottom[5] << "\n";
std::cout<< "                                     " << Bottom[6] << Bottom[7] << Bottom[8] << "\n\n";

 std::cout<< "                                     " << Back[6] << Back[7] << Back[8] << "\n";
std::cout<< "                                     " <<Back[3] << Back[4] << Back[5] << "\n";
std::cout<< "                                     " <<Back[0] << Back[1] << Back[2] <<                 "\n\n";

 }

void moveTopc()
{  
 char cornerSave, sideSave;
 char spotSave0, spotSave1, spotSave2;     
     cornerSave = Top[0];
     Top[0] = Top[6];
     Top[6] = Top[8];
     Top[8] = Top[2];
     Top[2] = cornerSave;

     sideSave = Top[1];
     Top[1] = Top[3];
     Top[3] = Top[7];
     Top[7] = Top[5];
     Top[5] = sideSave;

     spotSave0 = Front[0];
     spotSave1 = Front[1];
     spotSave2 = Front[2];
     Front[0] = Right[0];
     Front[1] = Right[1];
     Front[2] = Right[2];
     Right[0] = Back[2];
     Right[1] = Back[1];
     Right[2] = Back[0];
     Back[2] = Left[0];
     Back[1] = Left[1];
     Back[0] = Left[2];
     Left[0] = spotSave0;
     Left[1] = spotSave1;
     Left[2] = spotSave2;
 }   

2 个答案:

答案 0 :(得分:0)

也许你在C ++中谈论阶级友谊 检查此链接 http://www.cplusplus.com/doc/tutorial/inheritance/

答案 1 :(得分:0)

好吧,我想我得到你想要的。 将多维数据集的初始化转移到显示构造函数:

Rubiks::Rubiks()
{
char topColor = 'B';
    char bottomColor = 'G';
    char frontColor = 'W';
    char backColor = 'Y';
    char leftColor = 'O';
    char rightColor = 'R';
    std::fill (Top, Top + 10, topColor);
    std::fill (Bottom, Bottom + 10, bottomColor);
    std::fill (Front, Front + 10, frontColor);
    std::fill (Back, Back + 10, backColor);
    std::fill (Left, Left + 10, leftColor);
    std::fill (Right, Right + 10, rightColor); 
}

之后你可以调用display()并且输出将如你所愿(转换后的立方体状态