在c ++中使用复制构造函数的逻辑错误

时间:2014-07-14 16:43:33

标签: c++

在以下程序中,我的预期输出是显示客户对象感兴趣的项目列表,但它显示空值。当我调用showitem()时,没有打印任何值。请帮我解决我的代码中的逻辑错误。

class item
{

    int itemno;
    string name;
public:
    item()
    {
        itemno=0;
    }
    item(int r,string n)
    {
        itemno = r;
        name = n;
    }
    void showitem()
    {
        cout<<"Item no is:"<<itemno<<"\n";
        cout<<"Name is :"<<name<<"\n";
    }
};

class customer
{
public:
    int customerno;
    string customername;
    string address;
    int totalitem;
    item *iteminterested;

    customer()
    {
        iteminterested=0;
        totalitem=0;
    }
    customer(customer &custref)
    {
        customerno=custref.customerno;
        customername=custref.customername;
        address=custref.address;
        iteminterested=new item[custref.totalitem];
        for(int i=0;i<custref.totalitem;++i)
        {
            iteminterested[i]=custref.iteminterested[i];
        }
        totalitem=custref.totalitem;
    }
    customer(int no,string cusname,string add,item *temp,int total)
    {
        customerno=no;
        customername=cusname;
        address=add;
        iteminterested=new item[total];

        for(int i=0;i<totalitem;++i)
        {
            iteminterested[i]=temp[i];
        }
        totalitem=total;
    }
    void showcustomer()
    {
        cout<<"customer name is"<<customername<<"\n";
        cout<<"customer number is"<<customerno<<"\n";
        cout<<"customer address is"<<address<<"\n";
        cout<<"customer " <<customername <<"intersted in \n";
        for(int k=0;k<totalitem;k++)
        {
            iteminterested[k].showitem();
        }
    }
};

int main()
{
    customer steve;
    item itemarray[]={item(3,"sandwiches"),item(4,"paperbags"),item(5,"biscuits"),item(6,"coke"),item(10,"biscuits"),item(9,"pen"),item(1,"pencil"),item(2,"eraser")};
    steve=customer(2,"steve","aus",itemarray,5);
    steve.showcustomer();
    customer mark(steve);
    mark.showcustomer();
    mark.showcustomer();
    steve.showcustomer();
    return 0;
}

2 个答案:

答案 0 :(得分:1)

在客户构造函数中,您必须在分配项目之前设置totalitem。它没有为循环初始化。或者使用总计进行循环。

答案 1 :(得分:-1)

#include <iostream>
#include <string>

using namespace std;

class Item {
  int itemno;
  string name;
public:
  Item()
    : itemno(0) { }
  Item(int r, string n)
    : itemno(r),
      name(n) { }
  void showitem() const {
    cout << "Item no is: " << itemno << "\n";
    cout << "Name is: " << name << "\n"; } };


class Customer {
public:
  int customerno;
  string customername;
  string address;
  int totalitem;
  Item* iteminterested;

  Customer()
    :  totalitem(0),
       iteminterested(0) { }
  Customer(const Customer& custref)
    : customerno(custref.customerno),
      customername(custref.customername),
      address(custref.address),
      totalitem(custref.totalitem) {
    iteminterested = new Item[totalitem];

    for (int i = 0; i < totalitem; ++i) {
      iteminterested[i] = custref.iteminterested[i]; } }
  Customer(int no, string cusname, string add, Item* temp, int total)
    : customerno(no),
      customername(cusname),
      address(add),
      totalitem(total) {
    iteminterested = new Item[total];

    for (int i = 0; i < totalitem; ++i) {
      iteminterested[i] = temp[i]; } }
  void showcustomer() const {
    cout << "customer name is: " << customername << "\n";
    cout << "customer number is: " << customerno << "\n";
    cout << "customer address is: " << address << "\n";
    cout << "customer " << customername << " intersted in \n";

    for (int k = 0; k < totalitem; ++k) {
      iteminterested[k].showitem(); } } };

int main() {
  Customer steve;
  Item itemarray[] = {
    Item(3, "sandwiches"),
    Item(4, "paperbags"),
    Item(5, "biscuits"),
    Item(6, "coke"),
    Item(10, "biscuits"),
    Item(9, "pen"),
    Item(1, "pencil"),
    Item(2, "eraser") };
  steve = Customer(2, "steve", "aus", itemarray, 5);
  steve.showcustomer();
  Customer mark(steve);
  mark.showcustomer();
  mark.showcustomer();
  steve.showcustomer();
  return 0; }