c++中关于带有多个类和头文件的传递程序中的继承、隐私和对象的问题

C++ questions on Inheritance, privacy, and objects in a shipping program with multiple classes and headers

本文关键字:程序 问题 对象 隐私 继承 于带 c++ 文件      更新时间:2023-10-16

我的程序应该打印来自EndPoint toString方法的"to"answers"from"地址,但我不太清楚如何实现它。这是我的代码。如何让Package::Package构造函数中的toString方法打印端点的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() << "nnTo:n" <<    receiver << 
        "nnWeight: " << 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),但它不会像那样编译。我想我只是不明白如何将端点信息发送到包对象。

以下是我的错误:

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

Package.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

Package.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 << "nnTo:n" << receiver << 
        "nnWeight: " << 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学习…),我已经让程序按照预期编译和工作,除了一个小而重要的问题。

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

代码中的

:
Package regular{ homer, donald, 25.0, 0.20 };
您将错误的变量传递给构造函数的参数,该参数是第一个和第二个参数的端点对象

你所拥有的是:

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

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

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

修复问题1将修复这个

我不知道为什么你得到第三个,因为你有iomanip在你的主