m0_72013766 2022-12-16 20:23 采纳率: 100%
浏览 48
已结题

如何编写一个三层类函数并调用

结合自身已经掌握的面向对象程序设计知识、C++算法知识、数学知识、物理知识、美术知识等,编写一个“MYWORLD”程序。
要求:(1)大致按照如下图的架构设计程序,其中类名只是个代号,大家需结合“见名知意”的原则从新命名;

(2)需满足纵向3层类,类An层至少需有3个类,类Bn层至少需有9个类的要求,让“MYWORLD”这个世界多姿多彩;
(3)需使用到算法,如:排序、求最大值、交换值等,可结合自身数学和物理知识,丰富程序的功能;
(4)尽可能的在程序中使用到如cout、cin、for、if、while、switch、构造函数、多态等所学到的知识。

  • 写回答

2条回答 默认 最新

  • |__WhoAmI__| 2022-12-16 23:17
    关注
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    
    using namespace std;
    
    // 类An层
    
    // 地球类
    class Earth {
    public:
      Earth(double radius, int population) : radius_(radius), population_(population) {}
      void SetPopulation(int population) { population_ = population; }
      void SetRadius(double radius) { radius_ = radius; }
      double GetRadius() { return radius_; }
      int GetPopulation() { return population_; }
    private:
      double radius_;
      int population_;
    };
    
    // 太阳系类
    class SolarSystem {
    public:
      SolarSystem(string name, int number_of_planets) : name_(name), number_of_planets_(number_of_planets) {}
      void SetName(string name) { name_ = name; }
      void SetNumberOfPlanets(int number_of_planets) { number_of_planets_ = number_of_planets; }
      string GetName() { return name_; }
      int GetNumberOfPlanets() { return number_of_planets_; }
    private:
      string name_;
      int number_of_planets_;
    };
    
    // 类Bn层
    
    // 动物类
    class Animal {
    public:
      Animal(string name, string habitat) : name_(name), habitat_(habitat) {}
      void SetName(string name) { name_ = name; }
      void SetHabitat(string habitat) { habitat_ = habitat; }
      string GetName() { return name_; }
      string GetHabitat() { return habitat_; }
      virtual void MakeSound() = 0; // 纯虚函数
    private:
      string name_;
      string habitat_;
    };
    
    // 人类类
    class Human : public Animal {
    public:
      Human(string name, string habitat, string language) : Animal(name, habitat), language_(language) {}
      void SetLanguage(string language) { language_ = language; }
      string GetLanguage() { return language_; }
      void MakeSound() { cout << "Hello, my name is " << GetName() << " and I speak " << language_ << endl; }
    private:
      string language_;
    };
    
    // 鱼类
    class Fish : public Animal {
    public:
      Fish(string name, string habitat, string color) : Animal(name, habitat), color_(color) {}
      void SetColor(string color) { color_ = color; }
      string GetColor() { return color_; }
      void MakeSound() { cout << "Blub, blub" << endl; }
    private:
      string color_;
    };
    
    // 其他类Bn
    
    // 植物类
    class Plant {
    public:
    Plant(string name, string habitat) : name_(name), habitat_(habitat) {}
    void SetName(string name) { name_ = name; }
    void SetHabitat(string habitat) { habitat_ = habitat; }
    string GetName() { return name_; }
    string GetHabitat() { return habitat_; }
    virtual void Grow() = 0; // 纯虚函数
    private:
    string name_;
    string habitat_;
    };
    
    // 花类
    class Flower : public Plant {
    public:
    Flower(string name, string habitat, string color) : Plant(name, habitat), color_(color) {}
    void SetColor(string color) { color_ = color; }
    string GetColor() { return color_; }
    void Grow() { cout << "I am a " << color_ << " flower and I am growing in the " << GetHabitat() << endl; }
    private:
    string color_;
    };
    
    // 树类
    class Tree : public Plant {
    public:
    Tree(string name, string habitat, int age) : Plant(name, habitat), age_(age) {}
    void SetAge(int age) { age_ = age; }
    int GetAge() { return age_; }
    void Grow() { cout << "I am a " << GetName() << " and I am " << age_ << " years old" << endl; }
    private:
    int age_;
    };
    
    // 类Cn层
    
    // 城市类
    class City {
    public:
    City(string name, int population) : name_(name), population_(population) {}
    void SetName(string name) { name_ = name; }
    void SetPopulation(int population) { population_ = population; }
    string GetName() { return name_; }
    int GetPopulation() { return population_; }
    private:
    string name_;
    int population_;
    };
    
    // 国家类
    class Country {
    public:
    Country(string name, int population, vector<City> cities) : name_(name), population_(population), cities_(cities) {}
    void SetName(string name) { name_ = name; }
    void SetPopulation(int population) { population_ = population; }
    void SetCities(vector<City> cities) { cities_ = cities; }
    string GetName() { return name_; }
    int GetPopulation() { return population_; }
    vector<City> GetCities() { return cities_; }
    void SortCitiesByPopulation() {
    sort(cities_.begin(), cities_.end(), [](const City& c1, const City& c2) { return c1.GetPopulation() < c2.GetPopulation(); });
    }
    City GetMostPopulatedCity() {
    City most_populated_city = cities_[0];
    for (const auto& city: cities_) {
    if (city.GetPopulation() > most_populated_city.GetPopulation()) {
    most_populated_city = city;
    }
    }
    return most_populated_city;
    }
    private:
    string name_;
    int population_;
    vector<City> cities_;
    };
    // 类Dn层
    
    // 太空站类
    class SpaceStation {
    public:
    SpaceStation(string name, int number_of_astronauts) : name_(name), number_of_astronauts_(number_of_astronauts) {}
    void SetName(string name) { name_ = name; }
    void SetNumberOfAstronauts(int number_of_astronauts) { number_of_astronauts_ = number_of_astronauts; }
    string GetName() { return name_; }
    int GetNumberOfAstronauts() { return number_of_astronauts_; }
    private:
    string name_;
    int number_of_astronauts_;
    };
    
    // 飞船类
    class Spaceship {
    public:
    Spaceship(string name, int number_of_astronauts) : name_(name), number_of_astronauts_(number_of_astronauts) {}
    void SetName(string name) { name_ = name; }
    void SetNumberOfAstronauts(int number_of_astronauts) { number_of_astronauts_ = number_of_astronauts; }
    string GetName() { return name_; }
    int GetNumberOfAstronauts() { return number_of_astronauts_; }
    private:
    string name_;
    int number_of_astronauts_;
    };
    
    // 主函数
    int main() {
    // 创建地球对象
    Earth earth(6371.0, 7000000000);
    cout << "The radius of Earth is " << earth.GetRadius() << " km and the population is " << earth.GetPopulation() << endl;
    earth.SetPopulation(7000000001);
    cout << "The radius of Earth is " << earth.GetRadius() << " km and the population is " << earth.GetPopulation() << endl;
    
    // 创建太阳系对象
    SolarSystem solar_system("Solar System", 8);
    cout << "The name of the solar system is " << solar_system.GetName() << " and it has " << solar_system.GetNumberOfPlanets() << " planets" << endl;
    solar_system.SetName("Milky Way");
    cout << "The name of the solar system is " << solar_system.GetName
    
    // 创建图片对象
    Image image("Sunset", 500, 500, "JPEG");
    cout << "The name of the image is " << image.GetName() << " and the format is " << image.GetFormat() << endl;
    image.SetName("Sunrise");
    image.SetFormat("PNG");
    cout << "The name of the image is " << image.GetName() << " and the format is " << image.GetFormat() << endl;
    
    // 创建视频对象
    Video video("Documentary", 3600, "MP4");
    cout << "The name of the video is " << video.GetName() << " and the duration is " << video.GetDuration() << " seconds" << endl;
    video.SetName("Movie");
    video.SetDuration(7200);
    cout << "The name of the video is " << video.GetName() << " and the duration is " << video.GetDuration() << " seconds" << endl;
    
    // 创建音乐对象
    Music music("Song", 180, "MP3");
    cout << "The name of the music is " << music.GetName() << " and the duration is " << music.GetDuration() << " seconds" << endl;
    music.SetName("Album");
    music.SetDuration(3600);
    cout << "The name of the music is " << music.GetName() << " and the duration is " << music.GetDuration() << " seconds" << endl;
    
    // 创建时钟对象
    Clock clock;
    clock.timer();
    return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月17日
  • 已采纳回答 12月17日
  • 创建了问题 12月16日

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上