You are here:

C++/CORRECTTNESS PLZ

Advertisement


Question
Hi vijiyan i made a program of abstract class shape in which rectangulabox and sphere are inherited with point3d then point3d is with point.

circle and rectangle is inherit with only point not point3d.

kindly check this and make it correct thanks

here is my code


# include <iostream>
# include <string.h>
# include <vector>
# include <typeinfo>

using namespace std;
class shape
{
     private :
         string name;
     public :
         shape(string n=0){name = n;}
         virtual void print() = 0;
         virtual double get_area() = 0;
         virtual double get_volume() = 0;
         virtual string get_name()
         {return name;}
};

class point : public shape
{
     
     private :
         int x , y ;
     public :
         point(){}
         point(string n,int a=0 , int b=0):shape(n)
         {x=a;y=b;}
         
         double get_area(){return 0;}
         double get_volume(){return 0;}
         void print()
         {cout << "Points(X/Y) :" << x  << " " << y << endl;}
         
};

class circle : public point
{
     protected :
         double radius ;
     public :
         circle(){}
         circle(string n,int x,int y,double r):point(n,x,y)
         {radius = r;}
         
         double get_area()
         {return  3.141 * radius * radius;}
         
         double get_volume()
         {return 0;}
         
         void print()
         {
         point::print();
         cout << "Area of CIRCLE : " << get_area() <<endl;
         }
};

class Rectangle : public point
{
  protected:
  double width,height;
public:
  Rectangle(){}
  Rectangle(string n,int x,int y, double w, double h):point(n,x,y){width=w, height=h;}
  double get_area(){return height*width;}
  //double get_volume(){return ()}

  void print()
  {
  point:print();
  cout<<"Area of RECTANGLE"<<get_area()<<endl;
  
  cout<<"Volume of RECTANGLE"<<get_volume()<<endl;}
  
};
class point3d : public point
{
private:
  int z;
public:
  point3d(){}
  point3d(string n,int c=0 ):shape(n)
         {c=z;}
         
         double get_sarea(){return 0;}
         double get_volume(){return 0;}
         void print()
         {cout << "Points(X/Y/Z) :" << x  << " " << y <<" "<<z<< endl;}
};

class RectangularBox : public point3d
{
private:
  double  width,height,depth;
public:
  RectanglarBox(){}
  RectanglarBox(string n,int x,int y, int z,double w, double h, double d):point3d(n,x,y,z)

{width=w, height=h, depth=d;}
  double get_sarea(){return 2*(d*h) + 2*(d*w) + 2*(w*h);}
  

  void print()
  {
  point3d:print();
  cout<<"Area of RECTANGLULARBOX"<<get_sarea()<<endl;
  s
  
};
class sphere :public point3d
{
protected :
         double radius ;
     public :
         sphere(){}
         sphere(string n,int x,int y,int y, double r):point(n,x,y)
         {radius = r;}
         
         double get_sarea()
         {return  4 * 3.141 * radius * radius;}
         
         
         void print()
         {
         point3d::print();
         cout << "Area of sphere : " << get_sarea() <<endl;
         }
};

int main()
{
   vector <shape *> shape(6);
   
   shape[0] = new point("p1",5,2) ;
   shape[1] = new circle("c1",5,2,4.2) ;
   shape[2] = new Rectangle("R",2,3,1.5,2.5);
  shape[3] = new point3d("p3",5,2,4);
  shape[4] = new RectangularBox("RB",5,3,4,1.2,2.2,3.1);
 shape[5] = new sphere("s",2,3,4,3);
   
 
   for(int i=0 ; i<5 ;i++)
   {
         shape[i]->print();
   }
   system("pause");
   return 0;
}

Answer
Inheritance in C++ implies an IS-A or CAN-BE-SUBSTITUED-FOR relationship.

So I think that point3d inheriting from point  is fine, point inheriting from shape, circle inheriting from point or sphere inheriting from point3d etc. are not.

A circle IS-A shape, but a circle is not a point. Circle should contain a point (its center) and a radius.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.