How to implement an abstarct class's method in sub-class by getting variables of it's parents class using a constructor in c++

huangapple go评论91阅读模式
英文:

How to implement an abstarct class's method in sub-class by getting variables of it's parents class using a constructor in c++

问题

首先,有一个名为Shape的父类,它有两个构造函数,一个带一个参数,另一个带两个参数。有两个类继承自"Shape"类,它们分别是Rectangle和Circle。

我尝试了使用Java,并且得到了我想要的结果。

以下是Java的实现代码:

// Java 代码
// ...

我想在C++中实现完全相同的功能。

我尝试了以下方式:

// C++ 代码
// ...

但是我遇到了错误..

abstract.cpp:(.rdata$.refptr._ZTV5Shape[.refptr._ZTV5Shape]+0x0): 对“Shape的vtable”未定义的引用
英文:

First of all,there is a parent class called Shape and it has two constructors one with one parameter and another with two parameter.There are two classes which are inheriting properties from class "Shape". They are Rectangle and Circle .

I tried it using java and i got what i wanted.

Here is the java Implementation..


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();
    }
    
}

I want the exact thing to implement in C++..

I tried it as below...

#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();
	
}

But I'm getting an error..

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

答案1

得分: 2

因为Shape::getArea没有定义,也没有声明为纯虚函数,所以会出现错误:

virtual void getArea();

要使其成为纯虚函数,您需要:

virtual void getArea() = 0;

此外,您应该提供一个虚析构函数。何时需要虚析构函数,何时不需要超出了这个问题的范围。最简单的方法是只是提供它:

virtual ~Shape(){};
英文:

You get the error because there is no definition for Shape::getArea, nor is it declared as pure virtual:

virtual void getArea();

to make it pure virtual you need:

virtual void getArea() = 0;

Also you should provide a virtual destructor. When it is needed and when not is beyond the scope of this question. The easiest is to just provide it:

virtual ~Shape(){};

答案2

得分: 2

「未定义对 vtable 的引用」错误源于未为所有未定义为纯虚函数的 virtual 函数提供定义。

在您的示例中,您声明了 Shape::getArea,但您既没有声明它为纯 virtual 函数,也没有为它提供定义,这是您问题的来源:

class Shape{
        ...
        virtual void getArea(); // 已声明,但不是纯虚函数,也未定义
};

要使其成为纯虚函数,您需要在末尾添加 = 0

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

The "Undefined reference to vtable" error comes from not providing a definition to all virtual functions that are not defined pure virtual.

In your example, you declare Shape::getArea, but you do not declare it pure virtual or give it a definition, which is the source of your problem:

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

To make it pure virtual, you need = 0 at the end.

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

huangapple
  • 本文由 发表于 2020年8月27日 00:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63602229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定