Accessing class method for object from within another class, C++ -
i working through google c++ course database project , have run quandary. have 2 classes composer , database creation of composer objects , database object store array of composers in. these classes defined in .h files included in final .cpp file. in meantime working through testing each class test.cpp file each. composer class appears working intended, database class not accessing methods composer class when create composer object in database. appreciated. files follows.
the composer class:
#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_; };
the composer test:
#include <iostream> #include "composer.h" using namespace std; int main() { cout << endl << "testing 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 deaf during latter part of life - never heard performance of 9th symphony."); composer.promote(2); composer.demote(1); composer.display(); }
the database class:
#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_; };
the database test:
#include <iostream> #include "database.h" using namespace std; int main() { database mydb; composer& comp1 = mydb.addcomposer("ludwig van", "beethoven", "romantic", 1770, "beethoven deaf during latter part of life - never heard performance of 9th symphony."); comp1.promote(7); composer& comp2 = mydb.addcomposer("johann sebastian", "bach", "baroque", 1685, "bach had 20 children, several of whom became famous musicians well."); comp2.promote(5); composer& comp3 = mydb.addcomposer("wolfgang amadeus", "mozart", "classical", 1756, "mozart feared life during last year - there evidence poisoned."); comp3.promote(2); cout << endl << "all composers: " << endl << endl; mydb.displayall(); }
when compiling , running testdatabase.cpp proper display output composer class, when test calls comp1.promote(#) nothing occurs , ranking prints out 10 each composer.
instead of
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;
try
composer& addcomposer(string in_first_name, string in_last_name, string in_genre, int in_yob, string in_fact){ composer &composer = composers_[next_slot_];
why works => c++, not c#, did not referenced composers[x], instead copied content of (at moment) empty composer.
Comments
Post a Comment