繁体   English   中英

C ++链继承

[英]C++ chain inheritance

我试图为具有3个继承的类( PersonData- > CustomerData- > PreferredCustomer )的超市创建一个节省百分比的程序。 整个程序必须遵循以下UML图:

UML图

我在上三等班时遇到问题; 特别是在类的第一个构造函数中初始化值。 我在Visual Studio中收到一条错误消息:“形式参数Pa的重新定义”和“形式参数“ dl”的重新定义”。

这是带有错误的构造函数:

PreferredCustomer(string aLname, string aFname, string aAddress, 
        string aCity, string aState, int aZip, string aPhone, int cn, 
        bool ml, double Pa, double dl)   // constructor 1
{
    double Pa;
    double dl;
}

第三个子构造函数中的参数已从父构造函数中重载。 参数cn,ml,Pa,dl已全部添加到此参数中。

我不确定为什么会出现这些错误? 另外,我应该创建虚函数还是使用指针? 感谢您的任何帮助。

我的程序源代码(供参考):

#include <iostream>
#include <string>
using namespace std;

class PersonData // PersonData parent class defined
{
private:
    string lastName;
    string firstName;
    string address;
    string city;
    string state;
    string phone;
    int zip;

public:
    PersonData() // Default Constructor initialization
    {
        lastName = " ";
        firstName = " ";
        address = " ";
        city = " ";
        state = " ";
        zip = 0;
        phone = " ";
    }

    PersonData(string aLname, string aFname, string aAddress, string aCity, string aState, int aZip, string aPhone) // Constructor 1
    {
        lastName = aLname;
        firstName = aFname;
        address = aAddress;
        city = aCity;
        state = aState;
        zip = aZip;
        phone = aPhone;
    }

    // Accesor Functions
    string getLastName() const
    {
        return lastName;
    }

    string getFirstName() const
    {
        return firstName;
    }

    string getAddress() const
    {
        return address;
    }

    string getCity() const
    {
        return city;
    }

    string getState() const
    {
        return state;
    }

    int getZip() const
    {
        return zip;
    }

    string getPhone() const
    {
        return phone;
    }

    // Mutator Functions
    void setLastName(string aLname)
    {
        lastName = aLname;
    }

    void setFirstName(string aFname)
    {
        firstName = aFname;
    }

    void setAddress(string aAddress)
    {
        address = aAddress;
    }

    void setCity(string aCity)
    {
        city = aCity;
    }

    void setState(string aState)
    {
        state = aState;
    }

    void setZip(int aZip)
    {
        zip = aZip;
    }

    void setPhone(string aPhone)
    {
        phone = aPhone;
    }
};

class CustomerData :public PersonData // CustomerData child class of PersonData base class
{
private:
    int customerNumber;
    bool mailingList;

public:
    CustomerData() // Default constructor
    {
        customerNumber = 0;
        mailingList = 0;
    }

    CustomerData(int cNum, bool mailL) // Constructor 1
    {
        setCustomerNumber(cNum);
        setMailingList(mailL);
    }

    // Accessor Functions for child class
    int getCustomerNumber() const
    {
        return customerNumber;
    }

    bool getMailingList() const
    {
        if (mailingList != 0)
        {

            cout << "On Mailing List!: ";
            return mailingList;
        }

        else (mailingList == 0);
        {
            cout << "Not on mailing list!: ";
            return mailingList;
        }
    }

    // Mutator Functions for child class
    void setCustomerNumber(int cNum)
    {
        customerNumber = cNum;
    }

    void setMailingList(bool mailL)
    {
        mailingList = mailL;
    }
};

class PreferredCustomer :public CustomerData // child class of CustomerData child class
{
private:
    double purchasesAmount;
    double discountLevel;
public:

    PreferredCustomer() // default constructor
    {
            purchasesAmount = 0;
            discountLevel = 0;
    }

    PreferredCustomer(string aLname, string aFname, string aAddress, 
        string aCity, string aState, int aZip, string aPhone, int cn, 
        bool ml, double Pa, double dl)   // constructor 1
    {
        double Pa;
        double dl;
    }

    // Mutator Functions
    void setPurchasesAmount(double Pa)
    {
        purchasesAmount = Pa;
    }

    // Accessor Functions
    double getPurchasesAmount() const
    {
        return purchasesAmount;
    }

    double getDiscountLevel() const
    {
        return discountLevel;
    }

    void addPurchase(double P) const
    {

    }
};

int main()
{
    CustomerData Cdata;       // for access of child class functions
    PersonData Pdata;         // for access of parent class functions
    PreferredCustomer PCdata; // for access of preferred customer class functions
    string     temp1;         // Temporary variable for string values
    int        temp2,         // Temporary variable for integer values
        max = 100,            // For-loop maximum loop value
        i;                    // i variable
    bool       temp3;         // Temporary variable for bool values

    for (i = 1; i <= max; i++)
    {
        // Input Data
        cout << "Please input first Name: ";
        getline(cin, temp1);
        Pdata.setFirstName(temp1);

        cout << "Please input last Name: ";
        getline(cin, temp1);
        Pdata.setLastName(temp1);

        cout << "Please input address: ";
        getline(cin, temp1);
        Pdata.setAddress(temp1);

        cout << "Please input city: ";
        getline(cin, temp1);
        Pdata.setCity(temp1);

        cout << "Please input state: ";
        getline(cin, temp1);
        Pdata.setState(temp1);

        cout << "Please input Zip code: ";
        cin >> temp2;
        Pdata.setZip(temp2);

        cin.ignore(); // discards unread char from cin

        cout << "Please input Phone Number: ";
        getline(cin, temp1);
        Pdata.setPhone(temp1);

        cout << "Enter 1 to be included in mail list," << endl;
        cout << "Enter 0 to not be included in mail list: ";
        cin >> temp3;
        Cdata.setMailingList(temp3);

        Cdata.setCustomerNumber(i); // set customer number

        cout << endl << endl;

        cout << "Name: " << Pdata.getFirstName() << ", " << Pdata.getLastName() << " \n";
        cout << "Customer Number: " << Cdata.getCustomerNumber() << "\n";
        cout << "Address: " << Pdata.getAddress() << "\n";
        cout << "City: " << Pdata.getCity() << "\n";
        cout << "State: " << Pdata.getState() << "\n";
        cout << "Phone: " << Pdata.getPhone() << "\n";
        cout << "Zip: " << Pdata.getZip() << "\n";
        cout << Cdata.getMailingList() << "\n";

        cout << endl << endl;
        cin.ignore(); // discards unread char from cin
    }

    char c;
    cin >> c;

    return 0;
}

构造函数的目的是获取传入的参数,并根据类声明构造对象,并根据需要初始化成员。 它不仅必须初始化类,而且还必须初始化所有基类,这可以通过将参数传递给基构造函数来完成。

PreferredCustomer(string aLname, string aFname, string aAddress, 
            string aCity, string aState, int aZip, string aPhone, int cn, 
            bool ml, double Pa, double dl)   // constructor 1
{
    double Pa;
    double dl;
}
  1. 您没有初始化任何东西
  2. 您正在声明两个与传入的参数匹配(并与之冲突)的局部参数
  3. 您无需调用基础的构造函数(大部分工作是如何完成的)

    PreferredCustomer(...) :CustomerData(...)

    CustomerData(...) :PersonData(...)

  4. 您不初始化此类的成员

     purchasesAmount = pa; discountLevel = dl; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM