Sta*_*uft 1 c++ pointers vector object
有一个作业,我应该在其中创建指向对象的指针的向量
稍后,我将使用继承/多态性来扩展类,以包括两天交付,第二天空运等费用。但是,现在我不在乎。当前程序的最终目标是仅打印出矢量中每个对象的内容(名称和地址)并找到其运输成本(重量*成本)。
我的麻烦与逻辑不符,我只是对与对象/指针/向量有关的几点感到困惑。但是首先我的代码。我基本上删去了目前不重要的所有内容,int main将具有用户输入,但是现在我硬编码了两个示例。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Package {
public:
Package(); //default constructor
Package(string d_name, string d_add, string d_zip, string d_city, string d_state, double c, double w);
double calculateCost(double, double);
~Package();
private:
string dest_name;
string dest_address;
string dest_zip;
string dest_city;
string dest_state;
double weight;
double cost;
};
Package::Package()
{
cout<<"Constucting Package Object with default values: "<<endl;
string dest_name="";
string dest_address="";
string dest_zip="";
string dest_city="";
string dest_state="";
double weight=0;
double cost=0;
}
Package::Package(string d_name, string d_add, string d_zip, string d_city, string d_state, string r_name, string r_add, string r_zip, string r_city, string r_state, double w, double c){
cout<<"Constucting Package Object with user defined values: "<<endl;
string dest_name=d_name;
string dest_address=d_add;
string dest_zip=d_zip;
string dest_city=d_city;
string dest_state=d_state;
double weight=w;
double cost=c;
}
Package::~Package()
{
cout<<"Deconstructing Package Object!"<<endl;
delete Package;
}
double Package::calculateCost(double x, double y){
return x+y;
}
int main(){
double cost=0;
vector<Package*> shipment;
cout<<"Enter Shipping Cost: "<<endl;
cin>>cost;
shipment.push_back(new Package("tom r","123 thunder road", "90210", "Red Bank", "NJ", cost, 10.5));
shipment.push_back(new Package ("Harry Potter","10 Madison Avenue", "55555", "New York", "NY", cost, 32.3));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
我需要复制构造函数吗?为什么?
解构对象指针向量的正确方法是什么?
任何帮助,将不胜感激。我在这里搜索了很多相关文章,并且我意识到我的程序将发生内存泄漏。我无法使用boost ::中的一种专用ptrs。现在,我更关心建立程序基础的问题。这样,我就可以真正掌握所需的功能。
谢谢。
指针向量可以重用于存储子类的对象:
class Person
{
public:
virtual const std::string& to_string () = 0;
virtual ~Person () { }
};
class Student : public Person
{
const std::string& to_string ()
{
// return name + grade
}
};
class Employee : public Person
{
const std::string& to_string ()
{
// return name + salary
}
};
std::vector<Pointer*> persons;
person.push_back (new Student (name, grade));
person.push_back (new Employee (name, salary));
person[0]->to_string (); // name + grade
person[1]->to_string (); // name + salary
Run Code Online (Sandbox Code Playgroud)
理想情况下,矢量应包装在一个类中。这使内存管理更加容易。它还有助于在std::vector不破坏现有客户端代码的情况下更改支持数据结构(此处为):
class PersonList
{
public:
Person* AddStudent (const std::string& name, int grade)
{
Person* p = new Student (name, grade);
persons.push_back (p);
return p;
}
Person* AddEmployee (const std::string& name, double salary)
{
Person* p = new Employee (name, salary);
persons.push_back (p);
return p;
}
~PersonList ()
{
size_t sz = persons.size ();
for (size_t i = 0; i < sz; ++i)
delete persons[i];
}
private
std::vector<Person*> persons;
};
Run Code Online (Sandbox Code Playgroud)
因此,我们可以将代码重新编写为:
{
PersonList persons;
Person* student = persons.AddStudent (name, grade);
Person* employee = persons.AddEmployee (name, salary);
student.to_string ();
employee.to_string ();
} // The memory allocated for the Person objects will be deleted when
// `persons` go out of scope here.
Run Code Online (Sandbox Code Playgroud)
熟悉三则规则将帮助您决定何时将复制构造函数添加到类中。另请阅读有关const正确性的信息。