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!