代码正在打印所有构造函数.我读到当我们从另一个类派生一个类时,构造函数不会被继承.那么为什么创建c是从b和调用构造函数a    
class A
{
public:
  A()  { cout << "A's constructor called" << endl; }
};
class B
{
public:
  B()  { cout << "B's constructor called" << endl; }
};
class C: public B, public A  // Note the order
{
public:
  C()  { cout << "C's constructor called" << endl; }
};
int main()
{
    C c;
    return 0;
}
我们怎样才能调用祖母的构造函数.
例如:B继承自A并C继承自B.
我需要调用Ain 的构造函数C.是否可以不创建实例B?
如果我需要这个如何在Java中实现它.
为什么C++的创建者决定使用构造函数初始化列表来初始化基类?为什么他没有选择使用类似下面代码中的第二个注释行的语法?
class A{
public:
  A() { }
};
class B : A{
public:
  B() : A() { }   // Why decide to use this one, using constructor initializer, to initialize base class?
  B() { A(); }    // Why not choose this one? It's easy for me to understand if it was possible.
};
int main(int argc, char *argv[]){
  /* do nothing */
}
我创建了三个类:( Shape基类),Rectangle和Square.我试图Shape从Rectangle's和Square's构造函数调用构造函数,但编译器显示错误.
这是代码:
class Shape{
public:
    double x;
    double y;
    Shape(double xx, double yy) {x=xx; y=yy;}
    virtual void disply_area(){
        cout<<"The area of the shape is: "<<x*y<<endl;
    }
};
class Square:public Shape{
public:
    Square(double xx){ Shape(xx,xx);}
    void disply_area(){
        cout<<"The area of the square is: "<<x*x<<endl;
    }
};
class Rectnagel:public Shape{
    public:
        Rectnagel(double xx, double yy){ Shape(xx,yy);}
    void disply_area(){
        cout<<"The area of the eectnagel is: "<<x*y<<endl;
    }
};
int main() { …鉴于此代码
class Address
{
  private:
    char * streetName;
    int houseNumber;
  public:
    Address(char* strName, int houseNumber)
    {....}
 }
class Person
{
   protected:
       char *name, * phoneNumber;
       Address addr;
   public:
       Person(char* n, char* pN, char* stN, char* hsN): addr(stN,hsN)
       {
           //...... assign variable for person
       }      
};
class Officer: public Person
{
    private:
        double salary;
    public:
        // How to write the constructor??
        Officer(char* _name, char*_phoneNumber, char* _streetName, int _streetNumber, double _salary): .... ????
}
如何编写具有五个输入变量的派生类Officer的构造函数,其中_streetName和_streetNumber将被提供给基类Person中包含的成员对象addr?
我正在交叉编译从MSVC2010到GCC 4.7的项目.每个我调用基类构造函数的地方都是这样的:
 FPSCamera::FPSCamera(CameraType camType, float fov, int viewportW, int viewportH, float nearPlane, float farPlane)
{
    Camera3D::Camera3D( camType,  fov,  viewportW,  viewportH,  nearPlane,  farPlane);
}
我进入海湾合作委员会:
不能直接打电话给施工人员
MSVC没有抱怨......这样错误是固定的:
FPSCamera::FPSCamera(CameraType camType, float fov, int viewportW, int viewportH, float nearPlane, float farPlane)
       :Camera3D( camType,  fov,  viewportW,  viewportH,  nearPlane,  farPlane);
{
}
为什么?
我写了一个测试程序:
#include <iostream>
class base
{
        public:
        base()
        {
                std::cout << "base 1" << std::endl;
        }
        base(int a)
        {
                std::cout << "base a=" << a << std::endl;
        }
};
class child : public base
{
        public:
        child()
        {
                std::cout << "child" << std::endl;
        }
};
int main(int argc, char* argv[])
{
        child c;
        return 0;
}
该计划输出以下内容:
$ ./a.out
base 1
child
有没有可能的方法将一个整数参数发送到基类,从而调用基本构造函数的另一种形式?
在base只有一个构造函数的情况下,我得到一个编译错误:candidate expects 1 argument, 0 provided,正如您所料.但是,如果我给该构造函数提供一个默认参数,例如:
base(int a = 10)
然后我的程序编译并运行,输出:base a=10.(有趣?)
有没有办法传入变量值 …
这不是家庭作业,只是我的C++课程的一些训练练习,以便习惯于继承和东西.所以练习的第一部分要求我们创建一个程序,它有一个类名Rectangle,我们应该做构造函数getter和setter,找到区域和周长.这部分工作正常.练习的第二部分说要创建一个新的类名Square,它扩展Rectangle,它有一个构造函数,它将作为参数的正方形宽度.然后程序应该打印区域和周边.
#include <iostream>
using namespace std;
class Rectangular {
    private:
        int width;
        int height;
    public:
        Rectangular () {
            width = 5;
            height = 5;
        }
        Rectangular (int w, int h) {
            width = w;
            height = h;
        }
        void setWidth (int w) {
            width = w;
        }
        void setHeight (int h) {
            height = h;
        }
        int getWidth () {
            return width;
        }
        int getHeight () {
            return height;
        }
        int getArea () {
            return width*height;
        }
        int getPerimeter () …可能重复:
C++超类构造函数调用规则
你如何将工作委托给超类的构造函数?例如
class Super {
public:
    int x, y;
    Super() {x = y = 100;}
};
class Sub : public Super {
public:
    float z;
    Sub() {z = 2.5;}
};
我怎么Sub::Sub()叫Super::Super(),这样我就不必设置x并y在两个构造?
来自Java,我对调用基类构造函数(或super())有点困惑。我有 2 个类:Player(抽象类)和HumanPlayer ,它是 Player 的子类。现在,我有一个 Player 的构造函数,其声明如下:
Player(string name, list<Point> points);
现在我正在尝试实现这样的东西:
HumanPlayer() {
    string name = get name from user...
    list<Point> points = get points from user...
    ...
    ...
    ...
    super(name, points);
}
对于c++来说真的很陌生,很难理解它的语法。问候,
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
    A() { i=1; j=2;};
    A (A &obj) { i= obj.i+100; j= obj.j+100;};
     int i;
     int j;
};
class B:public A
{
public:
    B():A() {i=10; j=20; k=30;};
    B(A &obj) {  A::A(obj); k=10000; };//
    int k;
};
int main()
{ 
    A dog;
    B mouse(dog);
    cout<<mouse.i<<endl;
    cout<<mouse.k<<endl;
    return 0;
}
我尝试为派生类编写一个复制构造函数,它利用了基类的复制构造函数.我希望它mouse.i应该是101,但实际上编译结果是1.值为mouse.k10000,这是预期的.我想知道我的代码有什么问题.
我有一个类,Tile它有一个带有Color对象参数的构造函数:
class Tile
{
public:
    static const int size = 32;
    Tile();
    Tile(Color &color);
    void render(int x, int y);
    Color getColor();
    ~Tile();
private:
    Color _color;
};
然后我有一个子类,TileGrass继承Tile:
class TileGrass : public Tile
{
public:
    TileGrass();
    ~TileGrass();
};
现在的问题是TileGrass需要Tile使用Color参数继承构造函数.但是,TileGrass对象已经知道它需要给超类提供什么颜色,所以我不想Color在创建新TileGrass对象时传入对象.在Java中,我可以这样做:
public class TileGrass extends Tile {
public TileGrass() {
    super(color object);
}
}
我怎样才能在C++中做这样的事情?