为什么在用C ++编写Virtual函数的代码时需要将所有类的构造函数的成员初始化为'0' [英] Why need to initiallise to '0' of the members of constructor of all the classes while writing a code for Virtual function in C++

查看:165
本文介绍了为什么在用C ++编写Virtual函数的代码时需要将所有类的构造函数的成员初始化为'0'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码.

这段代码是为了理解虚函数的概念,因此在构造函数中编写时像shape(int a = 0,int b = 0)一样,并且对于派生类的所有其他构造函数都一样,完全可以正常工作.
但是当不将成员初始化为0时,会显示错误,例如没有匹配的函数调用Shape :: Shape()".

Here is the code.

This code is to understand the concept of virtual function so in the constructor when write like shape(int a=0, int b=0 ) and same for all other constructor of derived classes, working absolutely fine.
But when not initializing the members to 0 it''s showing error like " no matching function for call to Shape::Shape()".

#include< iostream >
using namespace std;
class Shape {
protected:
    int width, height;
    int mem;
public:
    Shape( int a, int b)
    {
        width = a;
        height = b;
    }
    virtual int area()
    {
        cout << "Parent class area :" <<endl;
        return 0;
    }
    virtual int dis()
    {
        mem=0;
    }
};
class Rectangle: public Shape{
public:
    Rectangle(int a, int b)
    {
        Shape(a, b);
    }
    int area ()
    {
        cout << "Rectangle class area :" <<endl;
        return (width * height);
    }
    int dis()
    {
        cout<<"mem is : "<<mem<<endl;
    }
};
class Triangle: public Shape{
public:
    Triangle(int a, int b)
    {
        Shape(a, b);
    }
    int area ()
    {
        cout << "Triangle class area :" <<endl;
        return (width * height / 2);
    }
};
// Main function for the program
int main( )
{
    Shape *shape;
    Rectangle rec(10,7);
    Triangle  tri(10,5);
    // store the address of Rectangle
    shape = &rec;
    // call rectangle area.
    shape->area();
    shape->dis();
    // store the address of Triangle
    shape = &tri;
    // call triangle area.
    shape->area();

    return 0;
}

推荐答案

错误#1:
您只能从构造函数的初始化程序列表中调用成员变量或基类的构造函数.
Bug #1:
You are allowed to call the constructor of a member variable or base class only from the initializer list of your constructor.
Rectangle(int a, int b)
    : Shape(a, b)
{
}



错误#2:
没有匹配的函数可以调用Shape :: Shape()"
如果您没有从派生类中显式调用Shape的构造函数,则编译器会自动调用基类的默认(无参数)构造函数.问题在于您的Shape类没有默认构造函数,因为仅当您不提供任何其他构造函数时,编译器才会自动生成默认构造函数.如果定义自己的构造函数,则只有为该类编写一个构造函数,该类才会具有默认构造函数.



Bug #2:
"no matching function for call to Shape::Shape()"
If you don''t explicitly call the constructor of Shape from your derived class then the compiler automatically calls the default (parameterless) constructor of the base class. The problem with this is that your Shape class doesn''t have a default constructor because the compiler auto-generates a default constructor only if you don''t provide any other constructors. If you define your own constructors then the class will have a default constructor only if you write one for it.


这篇关于为什么在用C ++编写Virtual函数的代码时需要将所有类的构造函数的成员初始化为'0'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆