【问题标题】:How to implement an abstarct class's method in sub-class by getting variables of it's parents class using a constructor in c++如何通过使用c ++中的构造函数获取父类的变量来在子类中实现抽象类方法
【发布时间】:2020-12-15 12:36:16
【问题描述】:

首先,有一个名为Shape的父类,它有两个构造函数,一个有一个参数,另一个有两个参数。有两个类从“Shape”类继承属性。它们是矩形和圆形。

我用java试过了,我得到了我想要的。

这里是java实现..


package javaapplication6;
import java.io.*; 

abstract class Shape{
    protected int radius,length,width;
    
    public Shape(int n){
        radius=n;
    }
    
    public Shape(int x,int y){
        length=x;
        width=y;
    }
    
    abstract public void getArea();
}

class Rectangle extends Shape{
    
    public Rectangle(int x,int y){
        super(x,y);
    }
    
    public void getData(){
        System.out.println(length+" "+width);
    }
    
    public void getArea(){
        System.out.println("Area of Reactangle is : "+width*length);
    }
}

class Circle extends Shape{
    
    public Circle(int x){
        super(x);
    }
    
    public void getData(){
        System.out.println(radius);
    }
    
    public void getArea(){
        System.out.println("Area of Reactangle is : "+2*radius*3.14);
    }
}

public class JavaApplication6 {
    
    public static void main(String[] args) {
        
        Rectangle r=new Rectangle(3,4);
        r.getData();
        r.getArea();
        
        System.out.println();
        
        Circle c=new Circle(3);
        c.getData();
        c.getArea();
    }
    
}

我想要在 C++ 中实现确切的东西..

我试过如下...

#include<bits/stdc++.h>
using namespace std;

class Shape{
    public:
        int r,x,y;
        
        Shape(int rad){
            r=rad;
        }
        
        Shape(int height,int width){
            x=height;
            y=width;
        }
        
        void getClass(){
            cout<<"Ur in class shape"<<endl;
        }
        
        virtual void getArea();
};

class Rectangle : public Shape{
    public:
        
        Rectangle(int x,int y):Shape(x,y){}
        
        void getArea(){
            cout<< "Area of rectangle : "<<x * y<<endl;
        }
        
        void getClass(){
            cout<<"Ur in class Rectangle"<<endl;
        }
};

class Circle : public Shape{
    public:
        
        Circle(int r):Shape(r){}
        
        vooid getArea(){
            cout<< "Area of Circle : "<<2* 3.14 * r<<endl;
        }
        
        void getClass(){
            cout<<"Ur in class Circle"<<endl;
        }
};

int main(){
    Circle c(5);
    c.getClass();
    c.getArea();
    
    Rectangle r(3,4);
    r.getClass();
    r.getArea();
    
}

但我遇到了一个错误..

abstract.cpp:(.rdata$.refptr._ZTV5Shape[.refptr._ZTV5Shape]+0x0): undefined reference to `vtable for Shape'

【问题讨论】:

标签: java c++ oop inheritance abstraction


【解决方案1】:

您收到错误是因为Shape::getArea 没有定义,也没有声明为纯虚拟:

virtual void getArea();

要使其成为纯虚拟,您需要:

virtual void getArea() = 0;

您还应该提供一个虚拟析构函数。什么时候需要,什么时候不需要,超出了这个问题的范围。最简单的就是提供它:

virtual ~Shape(){};

【讨论】:

    【解决方案2】:

    “未定义对 vtable 的引用”错误来自未为所有未定义纯虚拟的 virtual 函数提供定义。

    在您的示例中,您声明了Shape::getArea,但您没有声明它是纯virtual 或给它一个定义,这是您问题的根源:

    class Shape{
            ...
            virtual void getArea(); // declared, but not pure virtual or defined
    };
    

    要使其成为纯虚拟,最后需要= 0

    class Shape{
            ...
            virtual void getArea() = 0;
    };
    

    【讨论】:

      猜你喜欢
      • 2014-12-16
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多