#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类的公共方法。当我运行这段代码时,setBrand(999),setDoesItHaveThis和setHeight不能从输出中看出来。谁能说出我做错了什么?
答案 0 :(得分:2)
使用多态的方法是继承基类的成员。如果你改为重复它们,那么这将声明同名的另一个成员。
derived
此处,member1
有两个成员base
:一个继承自<xsl:for-each-group select="tr" group-by="td[1]">
,另一个未继承。程序员(但不是编译器)很容易混淆这两个。所以应该避免使用这样的结构(好的编译器会警告你)。
答案 1 :(得分:1)
在派生类中,使用您需要的所有参数调用基类构造函数。并且像注释中指出的那样,不要将所有基类变量重新声明为派生类的新成员。要设置/获取基类成员变量,请使用在基类中公开的Setters和Getters(这些由派生类继承)。 (如果我是你,我会保护二传手)。还要查看构造函数中的初始化列表,而不是初始化正文中的每个成员 - Read this