私はC++を初めて使いました。学校の割り当てには、基底クラスを作成し、2つの派生クラスを作成し、2つの関連するすべての関数の出力を表示する必要があります派生クラス。最初の派生クラスのみが含まれています
問題は、テストファイルに2つの派生クラスを含めるときに、最初に含めるクラスのみが問題になります。私がそれらを含む順序を逆にすれば、現在最初に含まれているものが動作しますが、2番目のものは動作しません。
これはなぜ起こっているのでしょうか?ここで
#ifndef CASHPAYMENT_H
#define CASHPAYMENT_H
#include <string>
#include "Payment.h"
using namespace std;
class CashPayment: public Payment {
public:
// Creates a CashPayment with an amount of 0
CashPayment();
// Creates a CashPayment with the given amount
CashPayment(double a);
// Creates a string out of all the CashPayment details
string paymentDetails();
};
#endif
CreditCardPayment.hファイルです:
#include "Payment.h"
#include "CashPayment.h" // This one will work
#include "CreditCardPayment.h" // This one won't. Unless you switch them
#include <iostream>
#include <string>
using namespace std;
// Tests the CashPayment and CreditCardPayment classes, derived from the Payment clas
int main() {
CashPayment firstCash(420);
cout << "First Cash Payment" << endl
<< "Amount: " << firstCash.getAmount() << endl
<< firstCash.paymentDetails() << endl;
CreditCardPayment firstCredit("Mingwu Chen", 123456789, 11, 2016, 100);
cout << "Number: " << firstCredit.getNumber() << endl
<< "Month: " << firstCredit.getMonth() << endl
<< "Year: " << firstCredit.getYear() << endl
<< "Amount: " << firstCredit.getAmount() << endl
<< firstCredit.paymentDetails() << endl;
}
はここに私のCashPayment.hファイルです:
#ifndef CASHPAYMENT_H
#define CASHPAYMENT_H
#include <string>
#include "Payment.h"
using namespace std;
class CreditCardPayment: public Payment {
private:
string name;
int cardNumber;
int expMonth;
int expYear;
public:
// Creates a CreditCardPayment with an amount of 0
CreditCardPayment();
// Creates a CreditCardPayment with the given card holder name, card number, expiry month, expiry year, and amount
CreditCardPayment(string n, int c, int m, int y, double a);
// Creates a string out of all the CreditCardPayment details
string paymentDetails();
// Returns the card holder name
string getName();
// Returns the card number
int getNumber();
// Returns the expiry month
int getMonth();
// Returns the expiry year
int getYear();
// Sets the card holder name t0 the given name
void setName(string n);
// Sets the card number to the given number
void setNumber(int c);
// Sets the expiry month to the given month
void setMonth(int m);
// Sets the expiry year to the given year
void setYear(int y);
};
#endif
ここPayment.hファイルだ
は、ここに私のテストファイルです:
#ifndef PAYMENT_H
#define PAYMENT_H
#include <string>
using namespace std;
class Payment {
private:
double amount;
public:
// Creates a Payment with an amount of 0
Payment();
// Creates a Payment with the given amount
Payment(double a);
// Returns the amount
double getAmount();
// Sets the amount to the given value
void setAmount(int a);
// Creates a string out of all the Payment details
string paymentDetails();
};
#endif
私はこれを理解しようと徹夜してきました。私は怪物的に困惑しています。すべてのアイデアや提案は非常に高く評価されます。
(PS:ここで多くの人が名前空間stdを使用したいとしていないようですが、それは私の先生が何を望んだ)
_ "それは私の先生が望んでいるものです。 –
ヘッダーファイル内の 'using namespace std;'は、複数の人が同じソースで作業している大規模なプロジェクトで大混乱を招く優れた方法です。しかし、それは学生のプロジェクトを傷つけることはほとんどありません。 –
ヒント:ヘッダーファイルをユニークにするものについて考えてみましょう。 copypasta monsta gotchaのように見えます – mascoj