我正在制作一个表面,让用户输入信息并将其打印出来。
这就是它的样子。
main <- menu <- Reservation
<- BookingManager <- BookingRecord
我在预订中创建了一个矢量vector<string> CompanyName
, 这是添加公司名称的outputdataInfo()
,
void Reservation::outputdataInfo()
{
string CompName;
cout << "Company Name <-" << endl;
cin >> CompName;
Reservation::setCompanyName(string (CompName) );
cout << CompanyName.at(0) << endl;
// Use for test and it works
cout << CompanyName.size() << endl;
// Use for test and it works
cout << "End of Reservation, thank you." << endl;
}
公司名称的二传手:(工作(
void Reservation::setCompanyName(const string& cn)
{this->CompanyName.push_back(cn);}
但现在BookingRecord::outputdataInfo()
想要打印预订记录。
void BookingRecord::outputdataInfo()
{
cout << " ----- Booking Record -----" << endl;
Reservation::printBookingRecord();
}
我是这样写的(无法确认这是否正确(:
void Reservation::printBookingRecord() {
for (int i = 0; i < CompanyName.size(); i++) {
cout << " ---- Company ---- " << endl;
cout << "Name: " << CompanyName.at(i) << endl;
}
}
但是公司名称突然看起来像忘记了什么,或者像是重置了大小。 结果是BookingRecord::outputdataInfo()
无限不停地打印,但Reservation::printBookingRecord()
没有任何反应。这很奇怪,因为假设BookingRecord::outputdataInfo()
中没有for循环。 我想知道如何打印数据(Reservation::printBookingRecord()
由BookingRecord::outputdataInfo()
调用,但向量在"保留"( (或向量可用于其他类( 非常感谢:)
源代码(有点长(
//
// main.cpp
//
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "Menu.h"
#include "Reservation.h"
#include "BookingManager.h"
using namespace std;
int main(int argc, const char* argv[]) {
Menu m;
Reservation R;
BookingManager BM;
char choice;
do {
choice = m.menu();
switch (choice)
{
case 'R': case 'r':
R.outputdataInfo();
break;
case 'B': case 'b':
BM.outputdataInfo();
break;
default:
cout << "Invalid Alphabet. Please try again." << endl;
break;
}
} while (choice == 'R' || choice == 'r' || choice == 'B' || choice == 'b');
return 0;
}
//.....................
// Menu.h
//
#include <iostream>
#ifndef Menu_h
#define Menu_h
class Menu {
public: //Accessibility as public
char option;
char menu();
};
#endif
//.....................
// Menu.cpp
//
#include <iostream>
#include "Menu.h"
using namespace std;
char Menu::menu() {
cout << "" << endl;
cout << " BNC Exhibition Tour in European Cities" << endl;
cout << " Exhibition Recruitment " << endl;
cout << " " << endl;
cout << "Please type:" << endl;
cout << "R -> for Reservation Page" << endl;
cout << "B -> for Booking Manager Page" << endl;
cout << "And Press ENTER." << endl;
cin >> option;
cout << "" << endl;
return option;
}
//.............................
// Reservation.h
//
#include <iostream>
#include <vector>
using namespace std;
#ifndef Reservation_h
#define Reservation_h
class Reservation {
private:
vector<string> CompanyName;
public: //Accessibility as public
void outputdataInfo();
void setCompanyName(const string& cn);
Reservation();
~Reservation();
void printBookingRecord();
};
#endif
//.....................................
// Reservation.cpp
//
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
using namespace std;
#include "Reservation.h"
void Reservation::outputdataInfo()
{
cout << "Please input detail information first :" << endl;
string CompName;
cout << "Company Name <-" << endl;
cin >> CompName;
Reservation::setCompanyName(string (CompName) );
cout << CompanyName.at(0) << endl; //it works
cout << CompanyName.size() << endl; //it works
cout << "End of Reservation, thank you." << endl;
}
//////////////////////// S E T T E R ////////////////////
void Reservation::setCompanyName(const string& cn)
{
this->CompanyName.push_back(cn);
}
//////////////////////// S E T T E R ////////////////////
Reservation::Reservation() {}
Reservation::~Reservation() {}
/////////////////////// P R I N T ///////////////////////
void Reservation::printBookingRecord() {
for (int i = 0; i < CompanyName.size(); i++) {
cout << " ---- Company ---- " << endl;
cout << "Name: " << CompanyName.at(i) << endl;
}
}
//.............................
// BookingManager.h
//
#include <iostream>
#include <vector>
#ifndef BookingManager_h
#define BookingManager_h
class BookingManager {
public: //Accessibility as public
char option;
void outputdataInfo();
};
//..........................................
// BookingManager.cpp
//
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
#include "BookingManager.h"
#include "BookingRecord.h"
using namespace std;
void BookingManager::outputdataInfo() {
BookingRecord BR;
cout << "" << endl;
cout << " ----- Booking Manager -----" << endl;
cout << "" << endl;
cout << "Please type:" << endl;
cout << "B -> for Booking Record" << endl;
cout << "And Press ENTER." << endl;
cin >> option;
cout << "" << endl;
do {
switch (option)
{
case 'B': case 'b':
BR.outputdataInfo();
break;
default:
cout << "Invalid Alphabet. Please try again." << endl;
break;
}
} while (option == 'B' || option == 'b');
}
#endif
//...........................................
// BookingRecord.h
//
#include <iostream>
#include <vector>
#include "Reservation.h"
#ifndef BookingRecord_h
#define BookingRecord_h
class BookingRecord : public Reservation {
public: //Accessibility as public
void outputdataInfo();
};
#endif
//..........................................
// BookingRecord.cpp
//
#include <iostream>
#include <string>
#include <vector>
#include "Reservation.h"
#include "BookingRecord.h"
void BookingRecord::outputdataInfo()
{
cout << "" << endl;
cout << " ----- Booking Record -----" << endl;
cout << "" << endl;
cout << " Print all the information..." << endl;
Reservation::printBookingRecord();
}
// END
所以你的代码中有两个CompanyNames
。
一个在这里,R
变量的一部分。
int main(int argc, const char* argv[]) {
Menu m;
Reservation R;
另一个在这里
void BookingManager::outputdataInfo() {
BookingRecord BR;
BookingRecord
派生自Reservation
,所以它也包含一个CompanyName
。
我认为很明显,您在main
R
的CompanyName
中添加了一个名称,但在BookingManager::outputdataInfo
中打印出BR
CompanyName
。
类设计对我来说看起来是错误的。例如,您的方法缺少参数。当然,BookingManager::outputdataInfo
应该将BookingRecord
作为参数,以允许调用方指定他们想要输出的BookingRecord
。仅仅将BookingRecord
声明为BookingManager::outputdataInfo
中的局部变量没有任何意义。
在你急于编写大量代码之前,试着考虑一下你的类的设计。不同的类应该如何相互关联,它们需要哪些成员变量,它们需要什么方法,这些方法需要什么参数和返回类型。从类如何模拟现实世界的角度来考虑这个问题,而不是从你将如何实现功能的角度来考虑这个问题。那以后再说,先把设计做好。