#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
class Box
{
protected:
double length;
double width;
double height;
public:
// Constructors
Box(double lv, double wv, double hv) : length {lv}, width {wv}, height {hv}
{ std::cout << "Box(double, double, double) called.\n"; }
Box(double side) : Box {side, side, side}
{ std::cout << "Box(double) called.\n"; }
Box() { std::cout << "Box() called.\n"; }
double volume() const
{ return length * width * height; }
double getLength() const { return length; }
double getWidth() const { return width; }
double getHeight() const { return height; }
~Box()
{ cout << "box destructor" << endl; }
};
class Carton : public Box
{
private:
string material {"Cardboard"};
public:
Carton(double lv, double wv, double hv, const string desc) : Box {lv, wv, hv}, material {desc}
{ std::cout << "Carton(double,double,double,string) called.\n"; }
Carton(const string desc) : material {desc}
{ std::cout << "Carton(string) called.\n"; }
Carton(double side, const string desc) : Box {side},material {desc}
{ std::cout << "Carton(double,string) called.\n"; }
Carton() { std::cout << "Carton() called.\n"; }
~Carton()
{ cout << "carton destructor" << endl; }
};
int main()
{
Carton carton3 {4.0, "Plastic"};
}
В этом коде я ожидаю, как выходнаследования выходной конструктор
Box(double) called.
Carton(double,string) called.
cartoon destructor
box destructor
Но это показывает, как выход
Box(double, double, double) called.
Box(double) called.
Carton(double,string) called.
cartoon destructor
box destructor
Я не понимаю, что как Box(double, double, double) called.
отображается на экране. Я проследил код шаг за шагом, но он не должен быть на выходе. Может кто-нибудь объяснить проблему? Благодарю.
Как вы думаете, что это делает? 'Коробка (двойная сторона): Коробка {сторона, сторона, сторона}'? – juanchopanza
Вы используете [делегирующий конструктор] (http://en.cppreference.com/w/cpp/language/initializer_list#Delegating_constructor). Это правильное поведение. – NathanOliver
@askque Не могли бы вы отметить ответ, как принято, если вы сочтете это полезным, конечно. –