从另一个类C ++中访问对象的类方法

时间:2015-07-07 19:29:15

标签: c++ class oop

我正在研究谷歌c ++课程数据库项目,并遇到了困境。我有两个用于创建作曲家对象的Composer和Database类以及一个用于存储作曲家数组的数据库对象。这些类在.h文件中定义,以包含在最终的.cpp文件中。与此同时,我正在为每个类测试每个类的test.cpp文件。我的composer类似乎按预期工作,但是当我在数据库中创建一个composer对象时,我的数据库类没有从composer类访问这些方法。任何帮助是极大的赞赏。文件如下。

作曲家类:

#include <iostream>
using namespace std;

const int kDefaultRanking = 10;

class Composer {

 public:
  Composer(): first_name_(), last_name_(), composer_yob_(), composer_genre_(), 
            ranking_(kDefaultRanking), fact_(){}

  void set_first_name(string in_first_name){
    first_name_ = in_first_name;
}
  void set_last_name(string in_last_name){
    last_name_ = in_last_name;
}
string get_last_name(){
    return last_name_;
}
  void set_composer_yob(int in_composer_yob){
    composer_yob_ = in_composer_yob;
}
  void set_composer_genre(string in_composer_genre){
    composer_genre_ = in_composer_genre;
}   
  void set_ranking(int in_ranking){
    ranking_ = in_ranking;
}
  void set_fact(string in_fact){
    fact_ = in_fact;
}

  void Promote(int increment){
    ranking_ -= increment;
}
    void Demote(int decrement){
    ranking_ += decrement;
}
  void Display(){
    cout << "First Name: " << first_name_ << endl;
    cout << "Last Name: " << last_name_ << endl;
    cout << "Year of Birth: " << composer_yob_ << endl;
    cout << "Genre: " << composer_genre_ << endl;
    cout << "Fact: " << fact_ << endl;
    cout << "Ranking: " << ranking_ << endl;
}

 private:
  string first_name_;
  string last_name_;
  int composer_yob_; // year of birth
  string composer_genre_; // baroque, classical, romantic, etc.
  string fact_;
  int ranking_;
};

作曲家测试:

#include <iostream>
#include "Composer.h"
using namespace std;

int main()
{
  cout << endl << "Testing the Composer class." << endl << endl;

  Composer composer;

  composer.set_first_name("Ludwig van");
  composer.set_last_name("Beethoven");
  composer.set_composer_yob(1770);
  composer.set_composer_genre("Romantic");
  composer.set_fact("Beethoven was completely deaf during the latter part of
                    his life - he never heard a performance of his 9th symphony.");
  composer.Promote(2);
  composer.Demote(1);
  composer.Display();
}

数据库类:

#include  <iostream>
#include "Composer.h"

const int kMaxComposers = 100;

class Database {
 public:
  Database(): composers_(), next_slot_(0){}

 Composer& AddComposer(string in_first_name, string in_last_name, 
                        string in_genre, int in_yob, string in_fact){
    Composer composer;
    composers_[next_slot_] = composer;
    composer.set_first_name(in_first_name);
    composer.set_last_name(in_last_name);
    composer.set_composer_genre(in_genre);
    composer.set_composer_yob(in_yob);
    composer.set_fact(in_fact);
    next_slot_++;
    }                                               
  Composer& GetComposer(string in_last_name){
    for(int i=0;i<kMaxComposers;i++){
      if (composers_[i].get_last_name() == in_last_name){
        composers_[i].Display();
      }
    }
  }
 void DisplayAll(){
    for(int i=0;i<kMaxComposers;i++){
      if(composers_[i].get_last_name().length() > 0){
        composers_[i].Display();
      }
    }
  };
 void DisplayByRank();

 private:
  Composer composers_[kMaxComposers];
  int next_slot_;
};

数据库测试:

#include <iostream>
#include "Database.h"
using namespace std;

int main() {
  Database myDB;

 Composer& comp1 = myDB.AddComposer("Ludwig van", "Beethoven", "Romantic", 1770, 
             "Beethoven was completely deaf during the latter part of his life - he never 
             heard a performance of his 9th symphony.");
  comp1.Promote(7);

  Composer& comp2 = myDB.AddComposer("Johann Sebastian", "Bach", "Baroque", 1685,
                "Bach had 20 children, several of whom became famous musicians as well.");
  comp2.Promote(5);

  Composer& comp3 = myDB.AddComposer("Wolfgang Amadeus", "Mozart", "Classical", 1756,
                "Mozart feared for his life during his last year - there is some evidence 
                that he was poisoned.");
  comp3.Promote(2);

  cout << endl << "all Composers: " << endl << endl;
  myDB.DisplayAll();
}

编译并运行testdatabase.cpp时,我得到了composer类的正确显示输出,但是当测试调用comp1.promote(#)时,没有任何事情发生,并且每个作曲家的排名打印为10。

2 个答案:

答案 0 :(得分:1)

而不是

Composer& AddComposer(string in_first_name, string in_last_name, 
                        string in_genre, int in_yob, string in_fact){
    Composer composer;
    composers_[next_slot_] = composer;

尝试

Composer& AddComposer(string in_first_name, string in_last_name, 
                        string in_genre, int in_yob, string in_fact){
    Composer &composer = composers_[next_slot_];

为什么这样做=&gt;这是C ++,而不是C#,你没有引用作曲家[x],而是复制了(目前)空作曲家的内容。

答案 1 :(得分:0)

结交朋友课可以提供帮助。