仅包含第一个派生类

时间:2016-11-29 23:10:31

标签: c++

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

4 个答案:

答案 0 :(得分:4)

两个头文件都有:

#ifndef CASHPAYMENT_H
#define CASHPAYMENT_H

因此,首先包含哪一个,其内容实际上是编译的。

答案 1 :(得分:1)

#ifndef CASHPAYMENT_H
#define CASHPAYMENT_H

(stuff)

#endif

这称为include-guard,它会在程序编译时停止多次包含文件。

你不应该在所有三个头文件上都有CASHPAYMENT_H - 每个文件都需要一个唯一 include-guard。实际上并不重要,只要它在您的项目中是唯一的,并且大写的头文件名是一个很好的约定。

作为#ifndef的替代方案,您可以使用#pragma once,它可以执行相同的操作:

#pragma once

(stuff)

(注意文件末尾没有#endif。)

它不符合C ++标准,但几乎所有编译器都支持它,并且更清晰。

答案 2 :(得分:0)

在CreditCardPayment.h文件中输入:

#ifndef CREDITCARDPAYMENT_H
#define CREDITCARDPAYMENT_H

答案 3 :(得分:-1)

您必须复制代码,最好从IDE中的工具构建类。这将自动为您添加标题防御者。您的付款明细功能应为虚拟