• 华清 c++ day5 9月12


    1. #ifndef _HOMEWORK_H_
    2. #define _HOMEWORK_H_
    3. #include
    4. #include
    5. using namespace std;
    6. //定义一个图形类
    7. class Shap
    8. {
    9. protected:
    10. //周长
    11. double perimeter;
    12. //面积
    13. double area;
    14. public:
    15. //无参构造
    16. Shap();
    17. //析构函数
    18. ~Shap();
    19. };
    20. //圆类
    21. class Circle :public Shap
    22. {
    23. private:
    24. double radius;
    25. public:
    26. //无参构造
    27. Circle();
    28. //有参构造
    29. Circle(double r);
    30. //拷贝构造
    31. Circle(const Circle& other);
    32. //获取周长
    33. double getPerimeter()const;
    34. //获取面积
    35. double getArea()const;
    36. };
    37. //矩形类
    38. class Rect :public Shap
    39. {
    40. private:
    41. double width;
    42. double length;
    43. public:
    44. //无参构造
    45. Rect();
    46. //有参构造
    47. Rect(double l, double w);
    48. //拷贝构造
    49. Rect(const Rect& other);
    50. //获取周长
    51. double getPerimeter()const;
    52. //获取面积
    53. double getArea()const;
    54. };
    55. #endif // !_HOMEWORK_H_
    1. #include "homework.h"
    2. Shap::Shap():perimeter(0),area(0)
    3. {
    4. cout << "Shap::无参构造" << endl;
    5. }
    6. Shap::~Shap()
    7. {
    8. cout << "Shap::析构函数" << endl;
    9. }
    10. Circle::Circle():radius(0)
    11. {
    12. cout << "Cricle::无参构造" << endl;
    13. }
    14. Circle::Circle(double r):radius(r)
    15. {
    16. cout << "Circle::有参构造" << endl;
    17. perimeter = 2 * 3.1415926 * radius;
    18. area = 3.1415926 * radius * radius;
    19. }
    20. Circle::Circle(const Circle& other):radius(other.radius)
    21. {
    22. cout << "Circle::拷贝构造函数" << endl;
    23. perimeter = 2 * 3.1415926 * radius;
    24. area = 3.1415926 * radius * radius;
    25. }
    26. double Circle::getPerimeter() const
    27. {
    28. return perimeter;
    29. }
    30. double Circle::getArea() const
    31. {
    32. return area;
    33. }
    34. Rect::Rect():width(0),length(0)
    35. {
    36. cout << "Rect::无参构造" << endl;
    37. }
    38. Rect::Rect(double l, double w):length(l),width(w)
    39. {
    40. cout << "Rect::有参构造" << endl;
    41. perimeter = 2 * (length + width);
    42. area = length * width;
    43. }
    44. Rect::Rect(const Rect& other):width(other.width),length(other.length)
    45. {
    46. cout << "Rect::拷贝构造函数" << endl;
    47. perimeter = 2 * (length + width);
    48. area = length * width;
    49. }
    50. double Rect::getPerimeter() const
    51. {
    52. return perimeter;
    53. }
    54. double Rect::getArea() const
    55. {
    56. return area;
    57. }
    1. #include "homework.h"
    2. int main()
    3. {
    4. Circle c1(5);
    5. cout << "c1: Perimeter = " << c1.getPerimeter() << ", Area = " << c1.getArea() << endl;
    6. Rect r1(4, 6);
    7. cout << "r1: Perimeter = " << r1.getPerimeter() << ", Area = " << r1.getArea() << endl;
    8. Circle c2(c1);
    9. cout << "c2: Perimeter = " << c2.getPerimeter() << ", Area = " << c2.getArea() << endl;
    10. Rect r2(r1);
    11. cout << "r2: Perimeter = " << r2.getPerimeter() << ", Area = " << r2.getArea() << endl;
    12. return 0;
    13. }

    面试题 - GitMind

    C++ - day4 - GitMind

    在线思维导图 - GitMind

  • 相关阅读:
    全文搜索引擎对比:RedisSearch 和 Elasticsearch 的优劣分析
    risc-v 栈分析
    十、缓存与数据库一致性
    ABB机器人10106故障报警(维修时间提醒)的处理方法
    Mybatis核心配置文件中的标签介绍
    C++基础语法(类于对象下)
    第二十四章 源代码文件 REST API 参考(六)
    HTML二识
    JDK 动态代理
    Redis原理(二):Redis数据结构(下)
  • 原文地址:https://blog.csdn.net/Huxiao1220/article/details/132840547