2017-02-05 7 views
0
#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は、出力からわかる内容からは機能しません。誰かが私が間違っていたことを教えてくれる?C++継承が正しく機能していない

+3

変数をベースから子に複製しないでください。 – drescherjm

+2

これは面倒なコードです。あなたはそれを[mcve]に落とす可能性はありますか?私はあなたと同じくらいあなたにこれを頼みます。非常に頻繁にMCVEを作成するプロセスはバグを公開し、質問を不要にします。 – user4581301

+3

基本クラスと同じ名前の新しい変数を定義しています。それはあなたが思うように動作しません。私はC++で[良い本](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)を選ぶことをお勧めします。 –

答えて

1

派生クラスでは、必要なすべての引数を持つ基本クラスコンストラクタを呼び出します。そしてコメントの中で指摘されているように、すべての基本クラス変数を派生クラスの新しいメンバーとして再宣言しません。基本クラスのメンバ変数を設定/取得するには、基本クラスでパブリックにしたセッタとゲッタを使用します(これらは派生クラスによって継承されます)。 (私があなただったら私はセッターを保護するだろうが)。また、代わりに体内の各メンバーを初期化するコンストラクタでリストを初期化するには見える - は、基本クラスのメンバーを継承するRead this

2

多型を使用する方法があります。代わりにそれらを繰り返すと、同じ名前の別ののメンバーがと宣言されます。ここで

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と継承されていない別のから継承されたものを。これらの2つは、プログラマが簡単に混乱させることができます(ただし、コンパイラではありません)。したがって、そのような構造は避けるべきです(良いコンパイラが警告します)。

関連する問題