gpt4 book ai didi

c++ - 仅包含第一个派生类

转载 作者:搜寻专家 更新时间:2023-10-31 00:55:55 25 4
gpt4 key购买 nike

我是 C++ 的新手,对于一项学校作业,我们必须创建一个基类并从中创建 2 个派生类,然后显示这两个派生类的所有关联函数的输出。

问题是,当我尝试在我的测试文件中包含这两个派生类时,只有我包含的派生类首先起作用。如果我颠倒我包含它们的顺序,那么现在包含的第一个有效,但第二个无效

知道为什么会发生这种情况吗?

这是我的测试文件:

#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 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 文件:

#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:这里很多人似乎不喜欢使用namespace std,但这正是我老师想要的)

最佳答案

两个头文件都有:

#ifndef CASHPAYMENT_H
#define CASHPAYMENT_H

因此,无论哪个先包含,其内容实际得到编译。

关于c++ - 仅包含第一个派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40877477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com