关于具有多个类和标头的运输程序中的继承、隐私和对象的 C++ 问题

Posted

技术标签:

【中文标题】关于具有多个类和标头的运输程序中的继承、隐私和对象的 C++ 问题【英文标题】:C++ questions on Inheritance, privacy, and objects in a shipping program with multiple classes and headers 【发布时间】:2016-11-04 19:43:44 【问题描述】:

我的程序应该打印来自 EndPoint toString 方法的“to”和“from”地址,但我不太清楚如何实现它。这是我的代码。如何获取 Package::Package 构造函数中的 toString 方法来打印 EndPoint 的 toString 方法的内容?

// ShippingProgram.cpp :
//

#include "stdafx.h"
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include <iostream>
#include "Package.h" // Package class definition
using namespace std;

// constructor 

EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) 
    name = nameInfo;
    address = addressInfo;
    city = cityInfo;
    state = stateInfo;
    zip = zipInfo;



string EndPoint::toString() const 
    ostringstream output;
    output << fixed << setprecision(2); // two digits of precision   
    output << name << "\n" << address << "\n" <<
        city << ", " << state << " " << zip;
    return output.str();



Package::Package(EndPoint senderInfo, EndPoint receiverInfo, double weightInfo, double costPerOzInfo) 

    weight = weightInfo; // should validate                     
    costPerOz = costPerOzInfo;



void Package::calculateCost(double)



double Package::calculateCost() const 
    return weight * costPerOz;



string Package::toString() const 
    ostringstream output;
    output << fixed << setprecision(2); // two digits of precision   
    output << "\nFrom:\n" << senderInfo.toString() << "\n\nTo:\n" <<    receiver << 
        "\n\nWeight: " << weight << endl <<
        "\nShipping cost:\n" << calculateCost();
    return output.str();

主要方法:

#include <iostream>
#include <iomanip>
#include "stdafx.h"
//#include "TwoDayPackage.h"
//#include "OvernightPackage.h"
#include "Package.h"
using namespace std;

int main()

    // Three address records.

    EndPoint homer "Homer Simpson", "742 Evergreen Terrace", "Springfield",
        "FL", 32401 ;

    EndPoint donald "Donald Duck", "1313 Webfoot Walk", "Duckburg",
        "CA", 95501;

    EndPoint kermit "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 ;

    // This calls the base class constructor (regular fee).

    Package regular homer, donald, 25.0, 0.20 ;

    // Defines output precision for floating point numbers (iomanip).

//  cout << fixed << setprecision(2);

    // Prints package parameters.

    cout << "Regular package processed." << endl;
    cout << regular.toString();
    cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl;
    cout << homer.toString(); 
    // First derived class (two-day fee added).

    /* TwoDayPackage twoday donald, kermit, 17.5, 0.20, 2.0 ;

    cout << "Two-day package processed." << endl;
    cout << twoday.toString();
    cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl;

    // Second derived class (overnight fee added).

    OvernightPackage overnight kermit, homer, 14.2, 0.20, 0.50 ;

    cout << "Overnight package processed." << endl;
    cout << overnight.toString();
    cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl;
    */
    

这个项目要求我创建一个带有继承的运输程序。它必须包括一个私有的“EndPoint”类,它包含发送者和接收者信息,以及一个编译所有内容并将其放入字符串的“Package”类。

我的错误在于我如何让我的 Package 构造函数能够包含来自我的 EndPoint 类的信息。由于 main 方法在 Package 类必须位于的位置(EndPoint、EndPoint、Weight、Cost)进行了格式化,但它不会像那样编译。我想我只是不明白如何将 EndPoint 信息发送到 Package 对象。

这是我的错误:

    没有构造函数实例"Package::Package" matches the argument list argument types are: (EndPoint, EndPoint, double, double) Error C2440 'initializing': cannot convert from 'initializer list' to 'Package' Error C3861 'setprecision': identifier not found

包.h

#pragma once
#ifndef PACKAGE_H
#define PACKAGE_H    
#include <string>   
#include <iostream>
using namespace std;

class EndPoint 
public:
    EndPoint(const std::string&, const std::string&, const std::string&, const std::string&, int = 0.0);

    void setName(const std::string&);
    std::string getName() const;

    void setAddress(const std::string&);
    std::string getAddresss() const;

    void setCity(const std::string&);
    std::string getCity() const;

    void setState(const std::string&);
    std::string getState() const;

    void setZip(int);
    int getZip() const;

    string toString() const;
protected:
    std::string name;
    std::string address;
    std::string city;
    std::string state;

    int zip;
;
class Package 
public:
    string toString() const;
    Package(const std::string&, const std::string&, double = 0.0, double = 0.0);

    void setSender(const std::string&);
    std::string getSender() const;

    void setReceiver(const std::string&);
    std::string getReceiver() const;

    void setWeight(double);
    double getWeight() const;

    void setCostPerOz(double);
    double getCostPerOz() const;

    void calculateCost(double);
    double calculateCost() const;

    double calculateCost(double weight, double costPerOz)
    
        double shipping;
        shipping = weight * costPerOz;

        cout << "The Base Cost = " << shipping << endl << endl;
        return shipping;
    



protected:
    std::string sender;
    std::string receiver;
    double weight; // gross weekly sales
    double costPerOz; // commission percentage
;

#endif

包.cpp

#include "stdafx.h"
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include "Package.h" // Package class definition
using namespace std;

// constructor 

EndPoint::EndPoint(const string& nameInfo, const string& addressInfo, const string& cityInfo, const string& stateInfo, int zipInfo) 
    name = nameInfo;
    address = addressInfo;
    city = cityInfo;
    state = stateInfo;
    zip = zipInfo;


void EndPoint::setName(const string& nameInfo) 
    name = nameInfo;


string EndPoint::getName() const  return name; 

void EndPoint::setAddress(const string& addressInfo) 
    address = addressInfo;


string EndPoint::getAddresss() const  return address; 

void EndPoint::setCity(const string& cityInfo) 
    city = cityInfo;


string EndPoint::getCity() const  return city; 

void EndPoint::setState(const string& stateInfo) 
    state = stateInfo;


string EndPoint::getState() const  return state; 

void EndPoint::setZip(int zipInfo) 
    zip = zipInfo;


int EndPoint::getZip() const 
    return zip;


string EndPoint::toString() const 
    ostringstream output;
    output << fixed << setprecision(2); // two digits of precision   
    output << name << "\n" << address << "\n" <<
        city << ", " << state << " " << zip;
    return output.str();

string EndPoint::getState() const  return state; 
Package::Package(const string& senderInfo, const string& receiverInfo, double weightInfo, double costPerOzInfo) 
    sender = senderInfo; // should validate                              
    receiver = receiverInfo; // should validate                                
    weight = weightInfo; // should validate                     
    costPerOz = costPerOzInfo;



void Package::setSender(const string& senderInfo) 
    sender = senderInfo; // should validate



string Package::getSender() const  return sender; 


void Package::setReceiver(const string& receiverInfo) 
    receiver = receiverInfo; // should validate



string Package::getReceiver() const  return receiver; 



void Package::setWeight(double weightInfo) 
    if (weightInfo < 0.0) 
        throw invalid_argument("The package weight must be >= 0.0");
    

    weight = weightInfo;



double Package::getWeight() const  return weight; 


void Package::setCostPerOz(double costPerOzInfo) 

    costPerOz = costPerOzInfo;



double Package::getCostPerOz() const 
    return costPerOz;



double Package::calculateCost() const 
    return weight * costPerOz;



string Package::toString() const 
    ostringstream output;
    output << fixed << setprecision(2); // two digits of precision   
    output << "From:\n" << sender << "\n\nTo:\n" << receiver << 
        "\n\nWeight: " << weight << endl <<
        "\nShipping cost: " << calculateCost();
    return output.str();

Main.cpp

#include <iostream>
#include <iomanip>
#include "stdafx.h"
//#include "TwoDayPackage.h"
//#include "OvernightPackage.h"
#include "Package.h"
using namespace std;

int main()

    // Three address records.

    EndPoint homer "Homer Simpson", "742 Evergreen Terrace", "Springfield",
        "FL", 32401 ;

    EndPoint donald "Donald Duck", "1313 Webfoot Walk", "Duckburg",
        "CA", 95501;

    EndPoint kermit "Kermit Frog", "On the Swamp", "Leland", "MS", 38756 ;

    // This calls the base class constructor (regular fee).

    Package regular homer, donald, 25.0, 0.20 ;

    // Defines output precision for floating point numbers (iomanip).

    cout << fixed << setprecision(2);

    // Prints package parameters.

    cout << "Regular package processed." << endl;
    cout << regular.toString();
    cout << "Shipping Cost: $" << regular.calculateCost() << endl << endl;

    // First derived class (two-day fee added).

    /* TwoDayPackage twoday donald, kermit, 17.5, 0.20, 2.0 ;

    cout << "Two-day package processed." << endl;
    cout << twoday.toString();
    cout << "Shipping Cost: $" << twoday.calculateCost() << endl << endl;

    // Second derived class (overnight fee added).

    OvernightPackage overnight kermit, homer, 14.2, 0.20, 0.50 ;

    cout << "Overnight package processed." << endl;
    cout << overnight.toString();
    cout << "Shipping Cost: $" << overnight.calculateCost() << endl << endl;
    */
    

我已经注释掉了这里的代码块,因为我试图让第一部分工作,然后再深入研究其余部分。

编辑:

谢谢大家的建议!我进行了一些编辑并删除了大量额外的代码(getter 和 setter。我用 java 学习了...),并且我已经让程序按预期编译和工作,除了一个小但重要的问题。

【问题讨论】:

请edit您的问题提供minimal reproducible example。 顺便说一句,你不需要使用预编译头文件stdafx.h。对于较小的项目,构建时间并没有减少,尤其是在开发阶段。 我建议将EndPoint 放入单独的头文件和源文件中。 最简单的解决方法:#include &lt;iomanip&gt; 应该照顾 setprecision 我已经编辑了我的代码并实施了你们的一些建议,但遇到了一个有趣的问题,我已经编辑了我的原始问题以包含在内。谢谢! 【参考方案1】:

没有构造函数“Package::Package”的实例与参数匹配 列表参数类型为:(EndPoint, EndPoint, double, double)

在您的代码中:Package regular homer, donald, 25.0, 0.20 ; 您将错误的变量传递给构造函数的参数,这是第一个和第二个参数的 Endpoint 对象

你拥有的是:

Package(const std::string&, const std::string&, double = 0.0, double = 0.0);

它接受一个 std::string 对象作为第一个和第二个参数。

无法从“初始化列表”转换为“包”

解决问题 1 将解决此问题

我不知道为什么你会得到第三个,因为你的 main 中有 iomanip

【讨论】:

在该项目的说明中,它说将端点设置为私有(我没有这样做,因为它破坏了一切)。我只是不明白。如果我更改所有内容以使用端点,我会得到更多类型不匹配的错误,并且它说没有默认端点构造函数,但如果我创建一个繁荣另一个错误......我在这里拉头发。如何将端点传递给包构造函数? @BrenBailey 抱歉,但我昨天睡着了,要将端点传递给构造函数,你必须让它接受参数的端点,就像你在字符串中所做的那样,或者像这个 Package(const EndPoint& ep ,const EndPoint& ,double,double) ,顺便重载一下。

以上是关于关于具有多个类和标头的运输程序中的继承、隐私和对象的 C++ 问题的主要内容,如果未能解决你的问题,请参考以下文章

Java面向对象程序设计(抽象类和接口-----)

Java面向对象程序设计(抽象类和接口-----)

面向对象理解,封装继承多态知识总结

关于基类和派生类之间的关系

12类和对象继承

12类和对象继承