0

Why do I have to call PoweredDevice in SuperCopier's constructor? Copier's constructor already initialize when a Copier object is directly created.

Is not there any easy way to encapsulate Copier? (just use it's constructor and do not care it's bases.)

Below code is from http://www.learncpp.com/cpp-tutorial/128-virtual-base-classes/ with little modifications.

#include <iostream>

class PoweredDevice
{
public:
    PoweredDevice(int power)
    {
        std::cout << "PoweredDevice: " << power << '\n';
    }
};

class Scanner: virtual public PoweredDevice // note: PoweredDevice is now a virtual base class
{
public:
    Scanner(int scanner, int power)
        : PoweredDevice(power) // this line is required to create Scanner objects, but ignored in this case
    {
        std::cout << "Scanner: " << scanner << '\n';
    }
};

class Printer: virtual public PoweredDevice // note: PoweredDevice is now a virtual base class
{
public:
    Printer(int printer, int power)
        : PoweredDevice(power) // this line is required to create Printer objects, but ignored in this case
    {
        std::cout << "Printer: " << printer << '\n';
    }
};

class Copier: public Scanner, public Printer
{
public:
    Copier(int scanner, int printer, int power)
        : PoweredDevice(power), // PoweredDevice is constructed here
             Scanner(scanner, power), Printer(printer, power)

    {
    }
};

class SuperCopier: public Copier
{
public:
    SuperCopier(int scanner, int printer, int power)
        :   PoweredDevice(power),//This is the line I want to get rid of
                Copier(scanner, printer, power)
    {
        std::cout<<"Super copier constructed\n";
    }
};

int main()
{
     Copier copier(1,2,3);
    SuperCopier superCopier(4, 5, 6);
}

Output:

PoweredDevice: 3
Scanner: 1
Printer: 2
PoweredDevice: 6
Scanner: 4
Printer: 5
Super copier constructed
5
  • Why do you want to get rid of that line? Commented Dec 13, 2016 at 12:51
  • I dont want to leave a complex class hierarchy further developer's those will inherit their new classes from Copier. Of course my classes are more detailed ... Commented Dec 13, 2016 at 12:54
  • 1
    You might use composition instead of inheritance... Commented Dec 13, 2016 at 12:54
  • @Jarod42 construction is not the only case. Classes have interface functions. Then people will fight with mapping interface functions to Copiers' Commented Dec 13, 2016 at 12:59
  • For that matter, you could use the decorator pattern as well... Commented Dec 13, 2016 at 13:00

1 Answer 1

1

With composition, you might do

class PoweredDevice
{
public:
    explicit PoweredDevice(int power)
    {
        std::cout << "PoweredDevice: " << power << '\n';
    }
};

class Scanner
{
public:
    Scanner(PoweredDevice& powerDevice, int scanner)
        : poweredDevice(powerDevice)
    {
        std::cout << "Scanner: " << scanner << '\n';
    }
private:
    PoweredDevice poweredDevice
};

class Printer
{
public:
    Printer(PoweredDevice& powerDevice, int printer)
        : poweredDevice(powerDevice)
    {
        std::cout << "Printer: " << printer << '\n';
    }
private:
    PoweredDevice poweredDevice
};

class Copier: public PoweredDevice, public Scanner, public Printer
{
public:
    Copier(int scanner, int printer, int power)
        : PoweredDevice(power),
          Scanner(*this, scanner),
          Printer(*this, printer)
    {
    }
};

class SuperCopier: public Copier
{
public:
    SuperCopier(int scanner, int printer, int power)
        : Copier(scanner, printer, power)
    {
        std::cout<<"Super copier constructed\n";
    }
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.