Thread: Check this code please and FAST!! I need it to b chked!!

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    9

    Exclamation Check this code please and FAST!! I need it to b chked!!

    This program deals with inheritance!!
    Here's the code:

    Code:
     /*A program with point class as the base class. Cube  
    class is  inherited from Square class and Square class is the sub class of point.
    */
    
    #include <iostream.h>
    #include <conio.h>
    
         /* ****** Point class : BASE CLASS ***** */
          class Point{
         protected:
    	int x,y,z;
         public:
    	Point( int=0,int=0, int=0);  //Default constructor.
    	void setpoint(int,int,int); //sets coordinates(x,y)
    	int get_x() const { return x; } //gets x
    	int get_y() const { return y; } //gets y
    	int get_z() const { return z; } //gets z
    	friend ostream &operator<<( ostream &, const Point &);
         };
    
         //Constructor for class point.
         Point::Point(int a,int b,int c){
    	  setpoint(a,b,c);
         }
         //Sets the coordinates of x and y.
         void Point::setpoint(int a, int b, int c){
    	 if( a >=0 && b >=0 && c >=0){
    	    x=a;
    	    y=b;
    	    z=c;
    	  }
         }
         //Outputs the point.
         ostream &operator<<(ostream &output, const Point &p){
    	  output<<" [ " <<p.x<<" , "<<p.y<<" , "<<p.z<<" ] "<<endl;
    	  return output;
         }
         /*
       int main(){
        clrscr();
        Point p(56,45);
        cout<<" X coordinate is : "<<p.get_x()<<"\n Y coordinate is :"<<p.get_y();
        p.setpoint(10,10);
        cout<<"\n The new location of p is: "<<p<<endl;
        getch();
        return 0;
        }*/
    
        /* ****** Square class: SUBCLASS of point ****** */
        class Square : public Point
        {
        protected:
         double side;
        public:
          Square(double=0.0,int=0,int=0,int=0 ); //default constructor.
          void setside( double);  //sets the side.
          double getside() const; //returns the side.
          double area() const; //calculates the area.
        friend ostream &operator<<( ostream &, const Square &);
        };
    
        //Constructor of class Square.
        Square::Square( double sde, int a,int b, int c): Point(a,b,c)
    	{
          setside(sde);
    	}
        //Sets the appropriate side of the sq
        void Square::setside( double sde){
    	side= (sde >=0 ? sde : 0);
    	}
    
        //returns side.
        double Square::getside() const {
           return side;
         }
    
        //Calculate area of the square.
         double Square::area() const{
           return side*side;
         }
    
        //Output the square in the following format:
        // Coordinates =(x,y,z); Side = #.#;
        ostream &operator<<(ostream &output, const Square &s){
          output<<"\t Coordinates of the square = "<<" [ "<<s.get_x()<<" , "<<s.get_y()<<" , "<<s.get_z()<<" ] "<<endl;
          output<<"\t Side of the square = "<<s.side<<endl;
          return output;
        }
    
       /*
       int main(){
        clrscr();
        Square s(12,56,45);
        cout<<"\t X coordinate is : "<<s.get_x()<<"\n\t Y coordinate is :"<<s.get_y()<<"\n\t Z coordinate is :"<<s.get_z();
        cout<<" \n\t Side of the square is : "<<s.getside();
        s.setside(11);
        cout<<"\n\n\t The new location and  side of c are:\n"<<s<<"\n\t Area = " <<s.area()<<"\n";
        getch();
        return 0;
        } */
    
        /* ****** Cube class: SUBCLASS of square ****** */
        class Cube: public Square {
        friend ostream &operator<<( ostream &, const Cube &);
        public:
           //default constructor.
           Cube( double=0.0,int=0,int=0,int=0);
           double volume() const;  //calculate and return volume.
           };
    
        //Cube constructor calls Square constructor.
        Cube::Cube(double sde,int x,int y,int z): Square(sde,x,y,z)
        {
         setside(sde);
        }
        //Calculates the volume of the cube
        double Cube::volume() const {
    	   return side*side*side;
         }
        //Output the Cube dimensions
        ostream &operator<<(ostream &output, const Cube &c){
          output<<"\t Coordinates of the cube = "<<" [ "<<c.get_x()<<" , "<<c.get_y()<<" , "<<c.get_z()<<" ] "<<endl;
          output<<"\t Side of the cube = "<<c.side<<endl;
          return output;
        }
    
    
       int main(){
        clrscr();
        Cube c(12,56,45,67);
        cout<<"\t X coordinate is : "<<c.get_x()<<"\n\t Y coordinate is :"<<c.get_y()<<"\n\t Z coordinate is :"<<c.get_z();
    
        cout<<" \n\t Side of the cube is : "<<c.getside();
    
        //use set functions to change the Cube's attributes
        c.setpoint(3,4,5);
        c.setside(11);
    
        cout<<"\n\n\t The new location,point and  side of c are:\n"<<c<<"\n\t Volume = " <<c.volume()<<"\n";
    
        //display the Cube as a point
        Point &pref= c;   //pref thinks it is a point
        cout<<"\n\t Cube printed as a point : "<<pref<<endl;
    
        //displays the Cube as the square
        Square &sqRef= c;   //sqRef thinks it is a square
        cout<<"\n\t Cube printed as a Square :\n "<<sqRef<<"\n\t Area : "<< sqRef.area()<<endl;
        getch();
        return 0;
        }
    Please check this ASAP!And another thing u might be wondering why I took (x,y,z) as the coordinates for sq..for one thing,cube was being inherited from square!
    If the logic is wrong(something's telling me it is *lol*) ........neywayz,pLease PUHLEASE tell me what the errors are!!
    One more qs...when a base class-member has the same name as a derived class member..as in overriding..does the "is-a" relationship still hold between the two!?

    Appreciate it!!

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Does it compile ok? If not, why not? What errors do you get?

    Anyway, the logic for points, squares and cubes isn't what I would've chosen but it looks like it should work. The inherited point is the centre of the square, and the same applies to cube. I probably would've have had a container approach to this, with a cube composed of 8 points and a square of 4 - but thats just me. It depends how you like to think about it really

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    The program works just fine..no errors.. I just wanted someone to check the logic..if it was ok!!
    Hmmm....I'll try the container approach as well!

    Thankz!

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Just my opinion remember, but if you want to practice using inheritance your method is good.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    9


    Could u answer my other qs that concerns overriding?
    If the base- class member has the same name as the derived class-member..would the 'is-a' relationship hold? and Y!!

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    9

    Exclamation Another code to b checked pronto! :)

    NEEDs to b fixed!

    Code:
    #include <iostream.h>
    #include <math.h>
    #include <conio.h>
    
    
     /* ****** SHAPE CLASS : BASE CLASS ****** */
     class Shape{
      private:
         int x,y; //gives the centre of the shapes.
      public:
         Shape(int=0,int=0); //default constructor
         void setshape(int,int);
         int get_X() const { return x; }
         int get_Y() const { return y; }
         //pure virtual functions overridden in derived classes
         virtual void printshapename() const =0;
         };
    
      //default constructor.
      Shape::Shape(int a,int b){
    	setshape(a,b);
         }
    
       //sets the shape i.e coordinates
       void Shape::setshape(int a,int b){
           x=a; y=b;
        }
    
       /* ***** 2dshape class : subclass of shape ****** */
       class twodshape :public Shape{
       public:
          twodshape(int s,int w):Shape(s,w){}
          virtual double area() const
          { return 0.0; }
          virtual double perimeter () const
          { return 0.0; }
          virtual void printshapename() const { cout<<" 2dshape : ";}
          virtual void print() const;
       };
    
       void twodshape::print() const
         {
           cout<<" ( "<<Shape::get_X()<<" , "<<Shape::get_Y()<<" ) "<<endl;
        }
    
       /* ***** 3dShape class : subclass of shape ****** */
       class thshape : public Shape{
       public:
          thshape(int a,int b) : Shape(a,b) {}
          virtual double area() const
          { return 0.0;}
          virtual double volume () const
          { return 0.0;}
          virtual void printshapename() const { cout<<" 3d shape : ";}
          virtual void print() const;
          };
    
       void thshape::print() const
         {
           cout<<" ( "<<Shape::get_X()<<" , "<<Shape::get_Y()<<" ) "<<endl;
        }
    
       /* ***** Square class :  subclass of 2dshape ****** */
       class Square : public twodshape {
       private:
          double side;
       public:
          Square(double=0.0,int=0,int=0 ); //default constructor.
          void setside( double);  //sets the side.
          double getside() const; //returns the side.
          virtual double area() const; //calculates the area.
          virtual double perimeter() const; //calculates the perimeter
          virtual void printshapename() const { cout<<"\n\t Square : ";}
          virtual void print() const;
          };
    
         //Constructor of class Square.
        Square::Square( double sde, int a,int b):twodshape(a,b)
    	{
          setside(sde);
    	}
        //Sets the appropriate side of the sq
        void Square::setside( double sde){
    	side= (sde >=0 ? sde : 0);
           }
    
        //returns side.
        double Square::getside() const {
           return side;
         }
    
        //Calculate area of the square.
         double Square::area() const{
           return side*side;
         }
    
        //Calculates perimeter of the square
        double Square::perimeter() const{
           return 2*(side+side);
         }
    
        //Output the square in the following format:
        void Square::print() const
        {
          twodshape::print();
          cout<<"\t Side of the square = "<<side<<endl;
          cout<<"\t Area of the square = "<<area()<<endl;
          cout<<"\t Perimeter of the square = "<<perimeter()<<endl;
         }
    
        /* ****** Rectangle class :Sub class of 2dshape ****** */
        class Rectangle : public twodshape{
        private:
         double length,breadth;
        public:
          Rectangle(double=0.0,double=0.0,int=0,int=0 ); //default constructor.
          void setlenb( double,double);  //sets the length and width.
          double getlen() const; //returns the length.
          double getbdt() const; //returns the width.
          virtual double area() const; //calculates the area.
          virtual double perimeter() const; //calculates the perimeter.
          virtual void printshapename() const { cout<<"\n\t Rectangle : ";}
          virtual void print() const;
          };
    
        //default constructor
        Rectangle::Rectangle( double len, double bdt,int a,int b) : twodshape(a,b)
        {
          setlenb(len,bdt);
        }
    
        void Rectangle::setlenb(double len, double bdt){
           length=(len >=0 ? len : 0 );
           breadth=(bdt >=0 ? bdt : 0);
           }
    
        //Gets the length
         double Rectangle::getlen() const{
          return length;
         }
    
         //Gets the breadth
         double Rectangle::getbdt() const{
           return breadth;
         }
    
         //Gets the area of the rectangle
         double Rectangle::area() const {
           double area;
           area=length*breadth;
           return area;
        }
    
        //Gets the perimeter of the rectangle
        double Rectangle::perimeter() const {
         double perim;
         perim=2*(length + breadth);
         return perim;
         }
    
        //Output the rectangle in the following format:
        void Rectangle::print() const
        {
          twodshape::print();
          cout<<"\t Length of the rectangle = "<<length<<endl;
          cout<<"\t Breadth of the rectangle = "<<breadth<<endl;
          cout<<"\t Area of the rectangle  = "<<area()<<endl;
          cout<<"\t Perimeter of the rectangle = "<<perimeter()<<endl;
          }
    
        /* ****** Circle class : subclass of 2dshape ****** */
        class Circle : public twodshape{
        private:
         double radius;
        public:
         Circle( double=0.0,int=0,int=0); //default constructor
         void setradius( double );
         double getradius() const;
         virtual double area() const;
         virtual double perimeter() const;
         virtual void printshapename() const { cout<<" Circle : ";}
         virtual void print() const;
         };
    
         //default constructor
         Circle::Circle(double rad, int a,int b): twodshape(a,b)
         {
          setradius( rad);
          }
    
          //sets the radius
          void Circle::setradius(double rad ){
    	 radius =( rad >= 0 ? rad : 0 );
    	 }
    
          //gets the radius
          double Circle::getradius() const { return radius; }
    
          //Area of the circle
          double Circle::area() const{
    	return 3.14 * radius * radius; }
    
          //Perimeter of the circle
          double Circle::perimeter() const{
    	  return 2 * 3.14 * radius; }
    
          //Outputs the circle
          void Circle::print() const
          {
    	 twodshape::print();
    	 cout<<"\t Radius : "<<radius<<endl;
    	 cout<<"\t Area of the circle : "<<area()<<endl;
    	 cout<<"\t Perimeter of the circle : "<<perimeter()<<endl;
          }
    
        /* ****** Triangle class : subclass of 2dshape class ****** */
        class Triangle : public twodshape{
        private:
          double base,height;
        public:
         Triangle( double=0.0,double=0.0,int=0,int=0); //default constructor
         void setbh( double,double );
         double getheight() const;
         double getbase() const;
         virtual double area() const;
         virtual void printshapename() const { cout<<" Triangle : ";}
         virtual void print() const;
         };
    
         //default constructor
         Triangle::Triangle(double hg,double bs, int a,int b): twodshape(a,b)
         {
          setbh(hg,bs);
          }
    
          //sets the base and height
          void Triangle::setbh(double hg,double bs ){
    	 height =(hg >= 0 ? hg : 0 );
    	 base = (bs >= 0 ? bs : 0);
    	 }
    
           //gets the height
          double Triangle::getheight() const { return height; }
    
          //gets the base
          double Triangle::getbase() const { return base; }
    
          //Area of the triangle
          double Triangle::area() const{
    	return ( height * base)/2; }
    
          //Outputs the triangle
          void Triangle::print() const
          {
    	 twodshape::print();
    	 cout<<"\t Height : "<<height<<endl;
    	 cout<<"\t Base : "<<base<<endl;
    	 cout<<"\t Area of the triangle : "<<area()<<endl;
          }
    
         /* ******* Cylinder class : Subclass of 3dshape ******* */
         class Cylinder : public thshape , public Circle{
         private:
          double height;
         public:
          Cylinder(double=0.0,double=0.0,int=0,int=0); //default constructor
          void setheight( double );
          double getheight()const ;
          virtual double area() const;
          virtual double volume() const;
          virtual void printshapename() const { cout<<" Cylinder : "; }
          virtual void print() const;
         };
    
         //default constructor
         Cylinder::Cylinder(double h,double r,int a, int b) : thshape(a,b) , Circle(r)
         {
           setheight( h);
         }
    
         //sets the height
         void Cylinder::setheight( double h){
          height =( h >= 0 ? h : 0);
          }
    
          //gets the height
          double Cylinder::getheight() const
          { return height; }
    
          //Area of the cylinder
          double Cylinder::area() const{
    	 //surface area
    	 return 2 * Circle::area() + 2 * 3.14 * getradius() * height;
    	 }
    
          //Perimeter of the cylinder
          double Cylinder::volume() const{
    	return Circle::area() * height ; }
    
         //outputs the cylinder
         void Cylinder::print() const{
          Circle::print();
          cout<<"\t Height = "<<height;
          cout<<"\t Area of the cylinder = "<<area()<<endl;
          cout<<"\t Volume of the cylinder = "<<volume()<<endl;
          }
    
         /* ******** Cube class: Subclass of threedshape ******* */
         class Cube : public thshape,public Square{
         private:
          double height;
         public:
          Cube(double=0.0,double=0.0,int=0,int=0); //default constructor
          void sethght( double );
          double gethght()const ;
          virtual double area() const;
          virtual double volume() const;
          virtual void printshapename() const { cout<<" Cube : "; }
          virtual void print() const;
         };
    
         //default constructor
         Cube::Cube(double h,double bd,int a, int b) : thshape(a,b), Square(bd)
          {
           sethght( h);
         }
    
         //sets the height
         void Cube::sethght( double h){
          height =( h >= 0 ? h : 0);
          }
    
          //gets the height
          double Cube::gethght() const
          { return height; }
    
          //Area of the cube
          double Cube::area() const{
    	 //area
    	 return Square::area() * height ;
    	 }
    
          //Perimeter of the cube
          double Cube::volume() const{
    	return 2 * (pow(height,2) + pow(height,2) + pow(height,2)) ; }
    
         //outputs the cube
         void Cube::print() const{
          Square::print();
          cout<<"\t Height = "<<height;
          cout<<"\t Area of the cube : "<<area()<<endl;
          cout<<"\t Volume of the cube : "<<volume()<<endl;
           }
    
         /* ****** Prism class : subclass of 3dshape ******* */
         class Prism: public Triangle,public thshape{
         private:
           double height;
         public:
          Prism(double=0.0,double=0.0,double=0.0,int=0,int=0); //default constructor
          void sethght( double );
          double gethght()const ;
          virtual double volume() const;
          virtual void printshapename() const { cout<<" Prism : "; }
          virtual void print() const;
         };
    
         //default constructor
         Prism::Prism(double h,double hg,double bs,int a, int b) : thshape(a,b), Triangle(hg,bs)
    
          {
           sethght( h);
         }
    
         //sets the height
         void Prism::sethght( double h){
          height =( h >= 0 ? h : 0);
          }
    
          //gets the height
          double Prism::gethght() const
          { return height; }
    
          //Area of the prism
          double Prism::volume() const{
           //area
           return Triangle::area() * height ;
    	 }
    
          //outputs the prism
         void Prism::print() const{
          Triangle::print();
          cout<<"\t Height = "<<height<<endl;
          cout<<"\t Volume of the prism = "<<volume()<<endl;
           }
    
    
         //Main driver program
     int main(){
      clrscr();
      twodshape tds(7,11);  //create 2dshape
      Square sq(4.0, 22 , 8); //creates a square
      Rectangle rec(7.0,6.0,45,5); //creates a rectangle
      Circle cir(4.5,45,4); //creates a circle
      Triangle tri(3.0,4.0,44,3); //creates a triangle
    
      //3d shapes
      thshape thds(3,4); //creates a 3dshape
      Cylinder cyd(3.0,4.5,7,56); //creates a cylinder
      Cube cub(5.0,4.5,34,45);//creates a cube
      Prism prism(4.9,5.6,8.0,45,45); //creates a prism
         */
      tds.printshapename();
      tds.print();
      cout<<"\n";
    
      sq.printshapename();
      sq.print();
      cout<<"\n";
    
      rec.printshapename();
      rec.print();
      cout<<"\n";
    
      cir.printshapename();
      cir.print();
      cout<<"\n";
    
      tri.printshapename();
      tri.print();
      cout<<"\n";
      getch();
    
      thds.printshapename();
      cout<<"\n";
    
      cyd.printshapename();
      cyd.print();
      cout<<"\n";
    
      cub.printshapename();
      cub.print();
      cout<<"\n";
    
      prism.printshapename();
      prism.print();
      cout<<"\n";
    
    
      getch();
      return 0;
      }
    Works fine but it has some logical errors. plz help me Fix 'em!
    errr...sorry about the length! *grin*

Popular pages Recent additions subscribe to a feed