编程打卡:面向对象程序设计

#include <iostream>
#include <iomanip>
using namespace std;
const float PI = 3.14159;
class Shape {
public:
    virtual float area() = 0;
};
class Circle : public Shape {
private:
    float radius;
public:
    Circle(float r) : radius(r) {}
    float area() {
        return PI * radius * radius;
    }
};
class Rectangle : public Shape {
private:
    float length, breadth;
public:
    Rectangle(float l, float b) : length(l), breadth(b) {}
    float area() {
        return length * breadth;
    }
};
class Square : public Shape {
private:
    float side;
public:
    Square(float s) : side(s) {}
    float area() {
        return side * side;
    }
};
class Trapezoid : public Shape {
private:
    float a, b, h;
public:
    Trapezoid(float a, float b, float h) : a(a), b(b), h(h) {}
    float area() {
        return (a + b) * h / 2;
    }
};
class Triangle : public Shape {
private:
    float base, height;
public:
    Triangle(float b, float h) : base(b), height(h) {}
    float area() {
        return base * height / 2;
    }
};
int main() {
    float a[9];
    for (int i = 0; i < 9; i++) {
        cin >> a[i];
    }
    Circle c(a[0]);
    Square s(a[1]);
    Rectangle r(a[2], a[3]);
    Trapezoid t(a[4], a[5], a[6]);
    Triangle tr(a[7], a[8]);
    float sum = c.area() + s.area() + r.area() + t.area() + tr.area();
    cout << fixed << setprecision(3) << sum << endl;
}
#include <iostream>
using namespace std;
class Horse {
    public:
    Horse() {
        cout << "Horse 申请了空间...\n";
    }
    virtual ~Horse() {
        cout << "Horse 释放了空间...\n";
    }
    virtual void Fly() {
        cout << "Just a horse.\n";
    }
};
class Pegasus : public Horse {
    public:
    Pegasus() {
        cout << "Pegasus 申请了空间...\n";
    }
    ~Pegasus() {
        cout << "Pegasus 释放了空间...\n";
    }
    void Fly() {
        cout << "I can fly!\n";
    }
};
int main()
{
    Horse *p1 = new Horse; //输出:Horse 申请了空间...
    Horse *p2 = new Pegasus; /*  输出两行:
                                 Horse 申请了空间...
                                 Pegasus 申请了空间...   
                     */    
    cout << endl; 
    
    p1->Fly(); //输出:Just a horse.
    p2->Fly(); //输出:I can fly!
    cout << endl; 
    
    delete p1; //输出:Horse 释放了空间...
    delete p2;  /* 输出两行:
                   Pegasus 释放了空间... 
                   Horse 释放了空间... 
            */
    return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
class Point {
public:
    double x, y, z;
    friend double operator-(const Point& a, const Point& b) {
        return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
    }
};
template <class T>
double dist(T a, T b) {
    return abs(a-b);
}
int main() {
    int n;
    while (cin >> n) {
        if (n == 0) break;
        if (n == 1) {
            int a,b;
            cin >> a >> b;
            cout << dist<int>(a,b) << endl;
        }
        else if (n == 2) {
            double a,b;
            cin >> a >> b;
            cout << dist<double>(a,b) << endl;
        }
        else if (n == 3) {
            Point a,b;
            cin >> a.x >> a.y >> a.z >> b.x >> b.y >> b.z;
            cout << dist<Point>(a,b) << endl;
        }
    }
}

posted @ 2023-05-23 19:08  satou_matsuzaka  阅读(3)  评论(0编辑  收藏  举报

This is a Test

メイドノココロハ アヤツリドール