Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include <vector>
- #include <cmath>
- #include <ctime>
- #include <cctype>
- using namespace std;
- class Shape {
- public:
- virtual double getArea() = 0;
- };
- class Square : public Shape {
- private:
- double a;
- public:
- Square(double a){
- this->a = a;
- }
- virtual double getArea(){
- return a*a;
- }
- };
- class Circle : public Shape {
- private:
- double r;
- public:
- Circle(double r){
- this->r = r;
- }
- virtual double getArea(){
- return 3.14159265358979323846*r*r;
- }
- };
- int main(){
- cout << "Let's compute area." << endl;
- Shape *s = NULL;
- cout << "1 for square, 0 for circle?" << endl;
- int x;
- cin >> x;
- if(x == 1){
- cout << "Great. Please enter edge length: " << endl;
- double edge;
- cin >> edge;
- s = new Square(edge);
- } else if(x == 0){
- cout << "Great. Please enter radius length: " << endl;
- double radius;
- cin >> radius;
- s = new Circle(radius);
- }
- if (s != NULL){
- cout << "We will compute the area" << endl;
- cout << "The area is " << s->getArea() << endl;
- delete s;
- }
- cout << "Good bye" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement