如题

解决方案 »

  1.   

    #include <vector>
    #include<iostream>
    using namespace std;class Shape  
    {
      public:
    Shape()
    {
    type = 0;
    printf("hi, shape.type = %d\n", type);
    }
    int type;
    virtual ~Shape() { }
    };class Shape2 : public Shape
    {
    public:
    Shape2(int type) 
    {
    this->type = type;
    printf("hi, shape2.type = %d\n", this->type);
    }
    };int main()
    {
    Shape s;
    Shape2 s2(2); return 0;
    }
      

  2.   

    输出如下:hi, shape.type = 0
    hi, shape.type = 0
    hi, shape2.type = 2--------------------------
    第 1 行表示 Shape s;
    第 2, 3 行表示 Shape2 s2(2); 
    即, 它先调用父类 Shape 的构造函数, 再调用子类 Shape2 的构造函数.
      

  3.   


    如果 Shape(int type)
    {
    this->type = type;
    printf("hi, shape.type = %d\n", type);
    }
    int type;
    virtual ~Shape() { }
    Sharp(int type1 int type2)
    {
    }
      

  4.   

    Sharp(int type1 int type2)
    {
    }
      

  5.   

    Sharp2(int type1,int type2)
        :Sharp(type1,type2)
    {
    }
      

  6.   

    手动调一下即可, 为说明方便, 只在父类中提供一行打印, code 如下:
    #include<iostream>
    using namespace std;class Shape  
    {
    public:
    Shape() { type = 0; }
    Shape(int type) { type = 9; printf("hi, shape.type = %d\n", type);}
    int GetType(void) { return this->type; }
    virtual ~Shape() { } int type;};class Shape2 : public Shape
    {
    public:
    // 在这里手动调用
    Shape2(int type) { Shape::Shape(type); }
    };int main()
    {
    Shape2 s2(2); return 0;
    }输出为:
    hi, shape.type = 9