rectangle.hpp
#ifndef rectangle_hpp
#define rectangle_hpp
#include "shape.h"
class Rectangle:public Shape{
double width{1.0};
double height{1.0};
public:
Rectangle()=default;
Rectangle(double w,double h);
double getWidth() const;
void setWidth(double w);
double getHeight() const;
void setHeight(double h);
double getArea() const;
};
#endif
rectangle.cpp
#include "rectangle.hpp"
Rectangle::Rectangle(double w,double h):width{w},height{h}{}
double Rectangle::getWidth() const{return width};
void Rectangle::setWidth(double w){width=w;}
double Rectangle::getHeight()const{return height;}
void Rectangle::setHeight(double n){height=n;}
double Rectangle::getArea() const{
return width*height;
}
circle.cpp
#include<iostream>
#include "circle.hpp"
Circle::Circle(double radius_){
radius=radius_;
}
double Circle::getArea(){
return (3.14*radius*radius);
}
double Circle::getRadius() const{
return radius;
}
void Circle::setRaius(double radius){
this->radius=radius;
}
circle.hpp
#ifndef circle_hpp
#define circle_hpp
#include "shape.h"
class Circle:public Shape{
double radius;
public:
Circle();
Circle(double radius_);
double getArea();
double getRadius() const;
void setRaius(double radius);
};
#endif
shape.h
ifndef shape_h
#define shape_h
#include<iostream>
#include<array>
#include<string>
using std::string;
using namespace std::string_literals;
enum class Color {
white,black,red,green,blue,yellow,
};
class Shape{
private:
Color color{Color::black};
bool filled{false};
public:
Shape()=default;
Shape(Color color_,bool filled_){
color=color_;
filled=filled_;
}
Color getColor(){return color;}
void setColor(Color color_){color=color_;}
bool isFilled(){return filled;}
void setFilled(bool filled_){filled=filled_;}
string toString(){
std::array<string,6>c{"white"s,"black"s,"red"s,"green"s,"blue"s,"yellow"s};
return "Shape:"+c[static_cast<int>(color)]+" "+
(filled ?"filled"s:"not filled"s);
}
};
#endif
mai.cpp
#include <iostream>
#include <string>
#include "shape.h"
#include "circle.hpp"
#include "rectangle.hpp"
int main() {
Shape s1{Color::blue,false};
Circle c1{3.9};
Rectangle r1{4.0,1.0};
std::cout<<s1.toString()<<std::endl;
std::cout<<c1.toString()<<std::endl;
std::cout<<r1.toString()<<std::endl;
return 0;
}