Search code examples
javac++oopinheritanceabstraction

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


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'

Solution

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