简体   繁体   English

如何使用具有纯虚函数的父类访问 C++ 中的父类的构造函数

[英]How to access Constructor of Parent class in C++ with parent class having a pure virtual function

I am trying to create Circle and Rectangle from Class Shape.我正在尝试从类形状创建圆形和矩形。 I want y to be assigned pi if I call Shape() constructor with a parameter (from circle class).如果我使用参数(来自圆类)调用 Shape() 构造函数,我希望 y 被分配 pi 。 Since Shape has a purely virtual function the compiler is showing error.由于 Shape 具有纯虚函数,因此编译器显示错误。 How can I overcome this error.我怎样才能克服这个错误。 And why is default parameter running correctly then?为什么默认参数运行正确呢? Also I tried this->Shape(0) from Circle class.我也从 Circle 类中尝试了 this->Shape(0) 。 Compiler is saying "Invalid use of this"编译器说“Invalid use of this”

#include<iostream>
using namespace std;

class Shape
{public:
double x,y;

   Shape()
   {x=0;y=0;}

   Shape(int p,int t=3.14159)
   {x=p;y=t;}

   virtual void display_area()=0;
   virtual void get_data()=0;
};



class Circle: public Shape
{public:

    Circle()
    {Shape(0);}    //ERROR HERE

    void get_data()
    {cout<<"\nRadius: ";cin>>x;}

    void display_area()
    {cout<<"\nArea: "<<y*x*x;}
};

To call the base constructor you need to use the member initialization list .要调用基本构造函数,您需要使用member initialization list

Change:改变:

Circle()
{
    Shape(0);
}    //ERROR HERE

To

Circle() : Shape(0)
{

}

base classes are always initialized before the constructor's block runs, so you do it in the constructor's member initialization list ..基类总是在构造函数块运行之前初始化,所以你在构造函数的成员初始化列表中进行初始化..

I also fixed another bug in your code.... You are doing some narrowing conversions, which wouldn't work as you want...我还修复了您代码中的另一个错误....您正在进行一些缩小转换,这不会如您所愿...

#include<iostream>
using namespace std;

class Shape
{
public:
    double x,y;

Shape()
{  
     x=0;
     y=0;
}

Shape(double p, double t=3.14159)   //changed from Shape(int p, int t=3.14159)
{  
     x=p;
     y=t;
}

virtual void display_area()=0;

virtual void get_data()=0;
};

class Circle: public Shape
{
public:
    Circle() : Shape(0)
{  /*Shape(0); */ }    //Not HERE

void get_data()
{   
     cout<<"\nRadius: ";
     cin>>x;
}

void display_area()
{
     cout<<"\nArea: "<<y*x*x;}
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用C ++中的父类实现覆盖纯虚拟方法 - Override pure virtual method with parent class's implementation in C++ 在C ++中,Child是否有办法重用GrandParent中定义的纯虚函数的Parent类实现 - In C++ Is there a way for the Child to reuse the the Parent class implementation of the pure virtual function defined in GrandParent 如何使纯虚拟 function 在父 class function 中工作 - How to make a pure virtual function work in parent class function 从父类调用纯虚函数 - Call pure virtual function from parent class 在c ++中限制对纯虚函数的派生类的访问 - Restricting access to derived class for a pure virtual function in c++ 如何使用另一个父函数来满足父类纯虚函数的定义 - How can a base class satisfy the definition of a parent's pure virtual function using another parent's function 具有纯虚函数和指针数组对象类型的父类的指针数组 - pointer array of parent class having a pure virtual function and object type of pointer array 如何在 c++ 中访问父 class 的 function 并将变量保留在父 ZA2F2ED4F8EBC2CBB61DC21A29DC40 中? - how to access function of parent class in c++ and keep the variable in parent class? 如何在C ++中覆盖模板类中的纯虚函数? - How to override a pure virtual function in template class in C++? c++ - 如何在 class 模板的纯虚拟 function 中使用迭代器? - c++ - How to use iterators in a pure virtual function of a class template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM