C++実験(総合)
5347 ワード
抽象ベースクラスShapeクラスを定義します.このクラスにはdouble型の保護データメンバーareaが含まれており、それぞれ次の機能が実装されます.
Shape.h
#include
using namespace std;
class Shape
{
public:
Shape(){area = 0;}
virtual ~Shape(){
cout << "Shape ~" << endl;
}
virtual double Area() = 0;
virtual void Show();
protected :
double area;
};
class Circle: public Shape
{
public :
double radious;
double x, y;
Circle(){}
Circle(double, double, double);
~Circle(){
cout << "Circle ~" << endl;
}
virtual double Area();
virtual void Show();
friend ostream &operator << (ostream &, Circle &);
friend istream &operator >> (istream &, Circle &);
double operator +(Circle);
};
class Rectangle:public Shape{
public:
Rectangle(){}
Rectangle(double, double);
~Rectangle(){
cout << "Rectangle ~" << endl;
}
virtual double Area();
virtual void Show();
friend ostream &operator << (ostream &, Rectangle &);
friend istream &operator >> (istream &, Rectangle &);
private :
double width;
double height;
};
Shape.cpp
#include
#include
#include
#include"Shape.h"
using namespace std;
const double PI = 3.14159;
void Shape::Show(){
cout << "area is " << area << endl;
}
Circle::Circle(double a, double b, double r)
{
x = a;
y = b;
radious = r;
}
double Circle::operator +(Circle b){
double len;
double x = x - b.x;
double y = y - b.y;
len = x * x + y * y;
len = sqrt(len);
return len;
}
void Circle::Show(){
cout << "The Circle's radious is : " << radious << endl;
cout << "The Circle's center is : " ;
cout << "( " << x << "," << y << " )"<< endl;
cout << "The Circle's area is : " << Area() << endl;
}
double Circle::Area(){
double area = PI * radious * radious;
return area;
}
istream& operator >>(istream &input, Circle& cir){
cout << "Please input the radious and " ;
cout << "Please input the center of Circle : ";
input >> cir.radious ;
input >> cir.x >> cir.y;
return input;
}
ostream& operator << (ostream & output, Circle& cir){
output << "The Circle's radious is : " << cir.radious << endl;
output << "The Circle's center is : " ;
output << "( " << cir.x << "," << cir.y << " )"<< endl;
output << "The Circle's area is : " << cir.Area() << endl;
return output;
}
Rectangle::Rectangle(double w, double h){
width = w;
height = h;
}
double Rectangle::Area() {
area = width * height;
return area;
}
void Rectangle::Show(){
cout << "The Rectangle's width = " << width << endl;
cout << "The Rectangle's height = " << height << endl;
cout << "The Rectangle's area = " << Area() << endl;
}
ostream& operator <>(istream& input, Rectangle& rec){
cout << "please input the rectangle's width and height : ";
input >> rec.width >> rec.height;
}
Main.cpp
#include
#include "Shape.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(){
Shape *pt = NULL;
Circle cir1(3, 5, 5), cir2(6, 2, 2);
double len;
Rectangle rec(5, 4);
len = cir1 + cir2;
cout << "Cir1's area = " << cir1.Area() << endl;
cout << "Cir2's area = " << cir2.Area() << endl;
cout << "cir1 cir2 : " << len << endl;
cout << endl;
pt = &cir1;
pt->Show();
cout << endl;
pt = &rec;
pt->Show();
cout << endl;
Rectangle *r = new Rectangle(3, 4);
r->Area();
r->Show();
delete r;
return 0;
}