4.23打卡
class Person { public: virtual void bellRing() {} virtual ~Person(){} }; class Student:public Person { public: void bellRing(){ cout << "I am a student learning in classroom." << endl; } ~Student (){ cout <<"A student object destroyed." << endl; } }; class Teacher:public Person { public: void bellRing(){ cout << "I am a teacher teaching in classroom." << endl; } ~Teacher (){ cout <<"A teacher object destroyed." << endl; } }; class Principal:public Person { public: void bellRing(){ cout << "I am the principal inspecting in campus." << endl; } ~Principal (){ cout <<"A principal object destroyed." << endl; } };
#include<bits/stdc++.h> using namespace std; #define PI 3.14159 class Shape { protected: double a, b, c; public: Shape(double a=0,double b=0,double c=0) { this->a = a; this->b = b; this->c = c; } virtual double getmianji() = 0; }; class Circle:public Shape//(圆形) { public: Circle(double a) :Shape(a) {} double getmianji() { return PI * a * a; } }; class Square :public Shape//(正方形) { public: Square(double a) :Shape(a) {} double getmianji() { return a * a; } }; class Rectangle :public Shape//(长方形) { public: Rectangle(double a,double b) :Shape(a,b) {} double getmianji() { return a*b; } }; class Trapezoid :public Shape//(梯形) { public: Trapezoid(double a,double b,double c) :Shape(a,b,c) {} double getmianji() { return (a + b) * c * 0.5; } }; class Triangle :public Shape//(三角形) { public: Triangle(double a,double b) :Shape(a,b) {} double getmianji() { return a*b*0.5; } }; int main() { double a, b, c; cin >> a; Circle C(a); cin >> a ; Square s(a); cin >> a >> b; Rectangle r(a, b); cin >> a >> b >> c; Trapezoid t1(a, b, c); cin >> a >> b; Triangle t2(a, b); Shape* sp[5] = { &C,&s,&r,&t1,&t2 }; double totalareas = 0; for (int i = 0; i < 5; i++) { totalareas += sp[i]->getmianji(); } printf("total of all areas = %0.3f", totalareas); system("pause"); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律