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'