gpt4 book ai didi

C++ 继承无法正常工作

转载 作者:行者123 更新时间:2023-11-27 23:56:53 27 4
gpt4 key购买 nike

#include <iostream>
using namespace std;

class Item {
private:
int _code;
int _color;
int _brand;
double _height;
double _length;
double _width;
double _weight;
double _price;
int _type;
bool _doesItHaveThis;

public:
Item();
Item(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis);
void setCode(int code);
void setColor(int color);
void setBrand(int brand);
void setHeight(double height);
void setLength(double length);
void setWidth(double width);
void setWeight(double weight);
void setPrice(double price);
void setType(int type);
void setDoesItHaveThis(bool doesItHaveThis);
int getCode();
int getColor();
int getBrand();
double getHeight();
double getLength();
double getWidth();
double getWeight();
double getPrice();
int getType();
bool getDoesItHaveThis();
virtual ~Item();
void display();
};
//----------------------------------------------------------
Item::Item()
{
_code = 0;
_color = 0;
_brand = 0;
_height = 0;
_length = 0;
_width = 0;
_weight = 0;
_price = 0;
_type = 0;
_doesItHaveThis = 0;
}
//----------------------------------------------------------
Item::Item(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis)
{
_code = code;
_color = color;
_brand = brand;
_height = height;
_length = length;
_width = width;
_weight = weight;
_price = price;
_type = type;
_doesItHaveThis = doesItHaveThis;
}
//----------------------------------------------------------
void Item::setCode(int code)
{
_code = code;
}
//----------------------------------------------------------
void Item::setColor(int color)
{
_color = color;
}
//----------------------------------------------------------
void Item::setBrand(int brand)
{
_brand = brand;
}
//----------------------------------------------------------
void Item::setHeight(double height)
{
_height = height;
}
//----------------------------------------------------------
void Item::setLength(double length)
{
_length = length;
}
//----------------------------------------------------------
void Item::setWidth(double width)
{
_width = width;
}
//----------------------------------------------------------
void Item::setWeight(double weight)
{
_weight = weight;
}
//----------------------------------------------------------
void Item::setPrice(double price)
{
_price = price;
}
//----------------------------------------------------------
void Item::setType(int type)
{
_type = type;
}
//----------------------------------------------------------
void Item::setDoesItHaveThis(bool doesItHaveThis)
{
_doesItHaveThis = doesItHaveThis;
}
//----------------------------------------------------------
int Item::getCode()
{
return _code;
}
//----------------------------------------------------------
int Item::getColor()
{
return _color;
}
//----------------------------------------------------------
int Item::getBrand()
{
return _brand;
}
//----------------------------------------------------------
double Item::getHeight()
{
return _height;
}
//----------------------------------------------------------
double Item::getLength()
{
return _length;
}
//----------------------------------------------------------
double Item::getWidth()
{
return _width;
}
//----------------------------------------------------------
double Item::getWeight()
{
return _weight;
}
//----------------------------------------------------------
double Item::getPrice()
{
return _price;
}
//----------------------------------------------------------
int Item::getType()
{
return _type;
}
//----------------------------------------------------------
bool Item::getDoesItHaveThis()
{
return _doesItHaveThis;
}
//----------------------------------------------------------
Item::~Item()
{
cout << "ITEM ELIMINATED" << endl;
}
//----------------------------------------------------------
void Item::display()
{
cout << "code = " << _code << ", color = " << _color << ", brand = "
<< _brand << ", height = " << _height << ", length = " << _length
<< ", width = " << _width << ", weight = " << _weight << ", price = "
<< _price << ", type = " << _type << ", doesItHaveThis = "
<< _doesItHaveThis << endl;
}
//----------------------------------------------------------
class Pens : public Item {
private:
int _code;
int _color;
int _brand;
double _height;
double _length;
double _width;
double _weight;
double _price;
int _type;
bool _doesItHaveThis;
int _packetSize;

public:
Pens();
Pens(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis, int packetSize);
void setPacketSize(int packetSize);
int getPacketSize();
virtual ~Pens();
void display();
};
//----------------------------------------------------------
Pens::Pens()
{
_code = 0;
_color = 0;
_brand = 0;
_height = 0;
_length = 0;
_width = 0;
_weight = 0;
_price = 0;
_type = 0;
_doesItHaveThis = 0;
_packetSize = 0;
}
//----------------------------------------------------------
Pens::Pens(int code, int color, int brand, double height, double length, double width,
double weight, double price, int type, bool doesItHaveThis, int packetSize)
{
_code = code;
_color = color;
_brand = brand;
_height = height;
_length = length;
_width = width;
_weight = weight;
_price = price;
_type = type;
_doesItHaveThis = doesItHaveThis;
_packetSize = packetSize;
}
//----------------------------------------------------------
void Pens::setPacketSize(int packetSize)
{
_packetSize = packetSize;
}
//----------------------------------------------------------
int Pens::getPacketSize()
{
return _packetSize;
}
//----------------------------------------------------------
Pens::~Pens()
{
cout << "PEN ELIMINATED" << endl;
}
//----------------------------------------------------------
void Pens::display()
{
cout << "code = " << _code << ", color = " << _color << ", brand = "
<< _brand << ", height = " << _height << ", length = " << _length
<< ", width = " << _width << ", weight = " << _weight << ", price = "
<< _price << ", type = " << _type << ", doesItHaveThis = "
<< _doesItHaveThis << ", packetSize = " << _packetSize << endl;
}
//----------------------------------------------------------
void main()
{
Pens I1(1, 2, 3, 4.1, 2.0, 3.4, 3.3, 3.2, 5, 1, 0);
I1.setBrand(999);
I1.setDoesItHaveThis(0);
I1.setHeight(34.62);
I1.display();
}

这是我的代码,我想知道为什么 Pens 类没有正确地继承 Item 类的公共(public)方法。当我运行这段代码时,setBrand(999)、setDoesItHaveThis 和 setHeight 从输出中可以看出不起作用。谁能告诉我做错了什么?

最佳答案

使用多态的方法是继承基类的成员。如果您改为重复它们,那么这将声明同名的另一个成员。

struct base
{
int member1 = 0; // the =0 means that these members
int member2 = 0; // will be default initialised to 0
base() = default;
explicit base(int i, int j=0)
: member1(i), member2(j) {}
};

struct derived
: base
{
int member1 = 1; // not to be confused with base::member1
int member3 = 4;
derived() = default;
explicit derived(int i, int j=0, int k=1, int m=4)
: base(i,j), member1(k), member3(m) {}
int foo() const
{ return member1 * base::member1; }
};

这里,derived有两个成员member1:一个继承自base,另一个不继承。这两者很容易被程序员混淆(但不会被编译器混淆)。所以应该避免这样的结构(好的编译器会警告你)。

关于C++ 继承无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42055083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com