Skip to content

Instantly share code, notes, and snippets.

@HSley13
Created September 5, 2023 11:21
Show Gist options
  • Save HSley13/0c916121975927daa161dfbf08f89139 to your computer and use it in GitHub Desktop.
Save HSley13/0c916121975927daa161dfbf08f89139 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstring>
using namespace std;
class twoDShape
{
double width, height;
char name[30];
public:
twoDShape()
{
height=width=0;
strcpy(name, "Unknown");
}
twoDShape(double i,char *n)
{
height=width=i;
strcpy(name, n);
}
twoDShape(double h,double w,char *n)
{
height=h;
width=w;
strcpy(name, n);
}
void showdim()
{
cout<<"The width is: "<<width<<" and the Height is: "<<height<<endl;
}
virtual double area()=0; // Area() is a pure virtual function
/* Because twoDshape contains One pure virtual function it has became an
Abstract class which can't be instantiated */
double getheight()
{
return height;
}
double getwidth()
{
return width;
}
char *getname()
{
return name;
}
void setheight(double h)
{
height=h;
}
void setwidth(double w)
{
width=w;
}
};
class triangle: public twoDShape
{
char style[30];
public:
triangle()
{
strcpy(style,"Unknown");
}
triangle(char *str, double i): twoDShape(i,"triangle")
{
strcpy(style, str);
}
triangle(char *str, double h, double w): twoDShape(h,w,"triangle")
{
strcpy(style, str);
}
void showstyle()
{
cout<<"The Triangle is "<<style<<"\n";
}
double area()
{
return getheight()*getwidth()/2;
}
};
class rectangle: public twoDShape
{
public:
rectangle(double i): twoDShape(i,"rectangle")
{
}
rectangle(double h, double w): twoDShape(h,w,"rectangle")
{
}
bool isSquare()
{
if(getheight()==getwidth())return true;
return false;
}
double area()
{
return getheight()*getwidth();
}
};
int main()
{
twoDShape *shapes[4];
double e,r;
cout<<"Enter 2 numbers for the second Shape dimension"<<endl;
cin>>e;
cin>>r;
char ab[30];
cout<<"Enter the Style of the Second Shape"<<endl;
cin>>ab;
triangle b(ab,e,r);
double t;
cout<<"Enter One number for the third Shape dimension"<<endl;
cin>>t;
char ad[30];
cout<<"Enter the Style of the third Shape"<<endl;
cin>>ad;
triangle c(ad,t);
double y,u;
cout<<"Enter 2 numbers for the fourth Shape dimension"<<enl;
cin>>y;
cin>>u;
rectangle d(y,u);
double v;
cout<<"Enter One number for the fifth Shape dimension"<<endl;
cin>>v;
rectangle l(v);
shapes[0]= &b;
shapes[1]= &c;
shapes[2]= &d;
shapes[3]= &l;
for(int i=1; i<5; i++)
{
cout<<"Object is: " <<shapes[i]->getname()<<endl;
shapes[i]->showdim();
cout<<"Area is: "<<shapes[i]->area()<<endl;
cout<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment