0

I have a problem which I can't solve. I'm creating an cd class. This cd have 10 char items saved on it. The constructor completes the table.

class CD {
 protected:
     char* data_ = new char[10];
     bool* is_in_ = new bool;
 public:
     CD() {
         for (int i = 0; i < 10; i++) {
             cout << "Podaj znak: ";
             cin >> data_[i];
        }
     }

Next I`m creating a child class which can save 20 char items.

class BR : public CD {
 protected:
    char* improve_data_ = new char[20];
 public:
    BR() {
        for (int i = 0; i < 20; i++) {
            cout << "Podaj znak(BR): ";
            cin >> improve_data_[i];
        }
    }

My problem is that when I`m creating an BR object it automally calling cd constructor by which BR object calling cd constructor and its own constructor. My question is that is it possible not to call parent constructor?

Mat
  • 202,337
  • 40
  • 393
  • 406
Pejdz
  • 1
  • 2
  • 1
    No it's not. Why would you want this? What's the problem you're trying to solve? – Holt Jan 12 '22 at 16:07
  • 2
    Why are you using `new[]` when all you need to do is declare `char data[10];`? The same thing with `bool *is_in;` -- why not simply `bool is_in`;? – PaulMcKenzie Jan 12 '22 at 16:08
  • 2
    even if would be possible to not call the base constructor, you still have data and is_in in your derived class. You should start thinking about inheritance in general. The derived class contains ALL from the base class AND in addition which is defined in the derived one. And to initialize the base class elements, it is absolutely necessary that base class constructor is called. – Klaus Jan 12 '22 at 16:11
  • Handy reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Jan 12 '22 at 16:33

1 Answers1

0

Not thats not possible. If BR inherits from CD then each instance of BR has a CD instance that needs to be constructed as part of a BR instance. You could use different constructors, but you should rather provide an overload for the input operator rather than forcing every construction of an object to read input from the user.

std::istream& operator>>(std::istream& in, CD& cd) {
    for (int i = 0; i < 10; i++) {
         cout << "Podaj znak: ";
         cin >> cd.data_[i];
    }
}


std::istream& operator>>(std::istream& in, BR& br) {
    for (int i = 0; i < 20; i++) {
        cout << "Podaj znak(BR): ";
        cin >> br.improve_data_[i];
    }
}

Those operators need to be friends of the classes to be able to access the private members.

However, if the only difference between the two classes is the number of items in the array, you could also drop the inheritance and use a std::vector instead. Another issue is that you use dynamic allocation for no good reason. You should drop any new. The bool member should be a bool not a bool*. And using dynamic allocation for the array is also in contradiction to having two distinct classes. Choose one: Either make BR and CD two different types, one with a static array of size 10 and the other with size 20 or use dynamic allocation (via std::vector) but both at the same time is not a sound design.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • It seems to be a good solution, thanks a lot. – Pejdz Jan 12 '22 at 16:13
  • yes, use `std::vector` if everything else should work the same - if not - these two classes should not be a parent and a child but should be two siblings of a common base class. In computer science holding 20 values is not a particular case of holding 10 values. – mmomtchev Jan 12 '22 at 16:58