1

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
    Does this answer your question? [Undefined reference to vtable](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable) – Lukas-T Aug 26 '20 at 16:53
  • [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_an_ai Aug 26 '20 at 16:56
  • 2
    `getArea()` is not pure virtual nor is it defined. – Human-Compiler Aug 26 '20 at 16:59
  • 1
    dont try to translate java to c++ directly, despite looking similar they are very different languages. There are obivous differences (eg C++ has no `abstract` keyword) and more subtle differences, like initializing members should be done in the member initializer list not in the body of the constructor. There is lots more on the list... – 463035818_is_not_an_ai Aug 26 '20 at 17:00
  • fwiw, I dont quite agree with the duplicate because here the missing virtual destructor is not the problem (but that is the correct answer on the proposed dupe). An answer should mention the destructor but the actual problem is elsewhere – 463035818_is_not_an_ai Aug 26 '20 at 17:02
  • 1
    @stevesiddu no thats not how it works – 463035818_is_not_an_ai Aug 26 '20 at 17:03
  • Directly translating code from one language to another often results in the same thing you get when [directly translating human languages](https://en.wikipedia.org/wiki/All_your_base_are_belong_to_us). You need to re-implement behaviours, not code. – user4581301 Aug 26 '20 at 17:03
  • I'm new to cpp .. can someone help me in how do i make use variables that are declared in parent class – Steve Siddu Aug 26 '20 at 17:08
  • you are already using members of `Shape` in the derived classes. If you have another question you can open another one – 463035818_is_not_an_ai Aug 26 '20 at 17:12

2 Answers2

2

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(){};
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
2

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;
};
Human-Compiler
  • 11,022
  • 1
  • 32
  • 59