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();
}

So this is my code, and I'm wondering why the Pens class is not properly inheriting the public methods of the Item class. When I run this code, the setBrand(999), setDoesItHaveThis, and setHeight don't work from what I can tell from the output. Can anybody tell what I did wrong?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
  • 3
    Don't duplicate the variables from the base to the child. – drescherjm Feb 05 '17 at 17:32
  • 2
    That's a smurf-ton of code. Any chance of you whittling that down to a [mcve]? I ask this for you as much as us. Very often the process of crafting an MCVE exposes the bug and makes the question unnecessary. – user4581301 Feb 05 '17 at 17:32
  • 3
    You're defining new variables with the same names as the base class's. That won't work the way you think it will. I recommend picking up a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++. – Greg Kikola Feb 05 '17 at 17:33
  • 1
    You should try to reduce this to a simpler example that shows the problem. This is a lot of code to dig through. – Carcigenicate Feb 05 '17 at 17:33
  • Also be careful with underscore prefixes. They sometimes [mean something in C++](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) and it really sucks when you stumble over one of them. – user4581301 Feb 05 '17 at 17:34
  • 1
    That's a huge wall of code nobody is likely to study in detail. Remove all parts which are not necessary to reproduce the problem while still keeping the program compilable. – Christian Hackl Feb 05 '17 at 17:55

2 Answers2

2

The way to use polymorphism is to inherit the members of the base class. If you repeat them instead, then this will declare another member of the same name.

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; }
};

Here, derived has two members member1: one inherited from base and another not inherited. These two can be easily confused by a programmer (but not by the compiler). So such constructs should be avoided (and good compilers will warn you).

Walter
  • 44,150
  • 20
  • 113
  • 196
1

In the derived class call the base class constructor with all the arguments you need. And like pointed out in the comments do not redeclare all the base class variables as new members of the derived class. To set/get the base class member variables use the Setters and Getters that you have made public in your base class (these get inherited by the derived class). (Although I would make the setters protected if I were you). Also look in to initializer lists in constructors instead of initializing each member in the body - Read this

Community
  • 1
  • 1
nitimalh
  • 919
  • 10
  • 26