#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;
}