鱼C论坛

 找回密码
 立即注册
查看: 955|回复: 4

[已解决]萌新求教

[复制链接]
发表于 2020-3-1 22:01:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
“编写一个完整的城市类,并利用该类定义自己家乡为一个具体对象,并在主函数中展示城市的具体信息”
萌新想问下这个程序哪里错了呀,不知道该怎么改,谢谢大佬们


#include<iostream>
#include<string.h>
using namespace std;
class City{
        public:
                char m_name[100],m_location[100],m_weather[100];
                City(char*name,char*location,char*weather){
                        strcpy((char*)m_name,(char*)name);
                        strcpy((char*)m_location,(char*)location);
                        strcpy((char*)m_weather,(char*)weather);
                }
            void getname(){
                    cout<<m_name;
                }
                void getlocation(){
                        cout<<m_location;
                }
                void getweather(){
                        cout<<m_weather;
                }
};
int main()
{
        City name("Yixing");
        name.getname();
        City location("It locates in the south of Jiangsu province.");
        location.getlocation();
        City weather("wet");
        weather.getweather();
        system("pause");
        return 0;
}
最佳答案
2020-3-2 00:27:49
给你写了一个C++风格的:

  1. #include <iostream>
  2. //#include<String.h>
  3. #include <string> //C++的string 类,比C语言的字符数组表示的字符串好用
  4. using namespace std;
  5. class City
  6. {
  7. public:
  8.     // 构造函数
  9.     City(){};  //默认构造函数,可以不写,也可以在函数体中给成员变量默认值
  10.     City(const string name, const string location, const string weather) : m_name(name), m_location(location), m_weather(weather) // 初始化列表形式的成员变量初始化。当然你也可以采用下面注释掉的赋值方式。
  11.     {
  12.         // this->m_name = name;
  13.         // this->m_location = location;
  14.         // this->m_weather = weather;
  15.     }

  16.     //成员函数
  17.     // 设置名称
  18.     void setname(const string name)
  19.     {
  20.         this->m_name = name;  //C++ string类中有=运算符,以及一些非常方便的字符串操作函数,应用起来更加方便
  21.     }
  22.     // 设置位置
  23.     void setlocation(const string loc)
  24.     {
  25.         this->m_location = loc;
  26.     }
  27.     // 设置气候
  28.     void setweather(const string weather)
  29.     {
  30.         this->m_weather = weather;
  31.     }
  32.     // 成员变量读取函数
  33.     string getname()
  34.     {
  35.         return this->m_name;
  36.     }
  37.     string getlocation()
  38.     {
  39.         return this->m_location;
  40.     }
  41.     string  getweather()
  42.     {
  43.         return this->m_weather;
  44.     }

  45.     void printInfo() // 打印城市信息。
  46.     {
  47.         cout << "Name: "<< this->m_name << endl;
  48.         cout << "Location: " << this->m_location << endl;
  49.         cout << "Weather: " << this->m_weather << endl;
  50.     }
  51. //成员变量声明
  52. private: // 为了数据安全,一般情况下成员变量以私有变量形式出现。成员变量的访问只能通过定义好的成员函数进行。
  53.     string m_name;
  54.     string m_location;
  55.     string m_weather;
  56. };
  57. int main()
  58. {
  59.     // 声明一个City类的对象并初始化化
  60.     City myCity("Yixing", "It lcates in the south of Jiangsu province", "Wet");
  61.     // 打印对象myCity的城市信息
  62.     myCity.printInfo(); //调用成员函数
  63.     cout << myCity.getname() << myCity.getlocation() << myCity.getweather() << endl; //采用cout,调用成员变量读取函数
  64.    
  65.     //更新城市信息。
  66.     myCity.setname("Xi'an");
  67.     myCity.setlocation("Locates in the central part of Shaanxi province");
  68.     myCity.setweather("Moderate");
  69.     // 打印城市信息
  70.     myCity.printInfo();

  71.     City anotherCity; //这里会调用默认构造函数,由于我写的默认构造函数体是空的,所以成员变量没有赋值
  72.     anotherCity.printInfo();// 由于成员变量没有初始化,所以打印出来应该是所有变量都是空字符串
  73.     anotherCity.setname("Beijing"); //设置对象anotherCity的m_name;
  74.     anotherCity.printInfo(); //打印城市信息,可以看到名字已经被更新
  75.     system("pause");
  76.     return 0;
  77. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-1 23:41:34 | 显示全部楼层
既然写C++,怎么写的代码还是C语言的风格啊。
问题太多了,我帮你改的能用了,但是你这代码写的真的只是一个用了C++的类定义的C语言代码。
待会儿帮你重新写一个。
  1. #include<iostream>
  2. #include<string.h>
  3. using namespace std;
  4. class City{
  5.   public:
  6.     char m_name[100],m_location[100],m_weather[100];
  7.     City(char*name,char*location,char*weather){
  8.       strcpy((char*)m_name,(char*)name);
  9.       strcpy((char*)m_location,(char*)location);
  10.       strcpy((char*)m_weather,(char*)weather);
  11.       }
  12.     void getname(){
  13.       cout<<m_name;
  14.       }
  15.     void getlocation(){
  16.       cout<<m_location;
  17.     }
  18.     void getweather(){
  19.       cout<<m_weather;
  20.     }
  21. };
  22. int main(){
  23.   //City name("Yixing");                                          
  24.   //name.getname();                                                
  25.   //City location("It locates in the south of Jiangsu province.");
  26.   //location.getlocation();                                             
  27.   //City weather("wet");
  28.   //首先,你City类中定义的构造函数只有一个, 这个函数需要一次给三个字符串作为参数。
  29.   //所以声明一个City类的对象并初始化对象成员的唯一方式如下:  
  30.   City myCity("Yixing", "It lcates in the south of Jiangsu province", "Wet") ;
  31.   myCity.getname();
  32.   myCity.getlocation();
  33.   myCity.getweather();                                                                                    
  34.   system("pause");                                                     
  35.   return 0;                                                            
  36. }
  37.    
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-2 00:27:49 | 显示全部楼层    本楼为最佳答案   
给你写了一个C++风格的:

  1. #include <iostream>
  2. //#include<String.h>
  3. #include <string> //C++的string 类,比C语言的字符数组表示的字符串好用
  4. using namespace std;
  5. class City
  6. {
  7. public:
  8.     // 构造函数
  9.     City(){};  //默认构造函数,可以不写,也可以在函数体中给成员变量默认值
  10.     City(const string name, const string location, const string weather) : m_name(name), m_location(location), m_weather(weather) // 初始化列表形式的成员变量初始化。当然你也可以采用下面注释掉的赋值方式。
  11.     {
  12.         // this->m_name = name;
  13.         // this->m_location = location;
  14.         // this->m_weather = weather;
  15.     }

  16.     //成员函数
  17.     // 设置名称
  18.     void setname(const string name)
  19.     {
  20.         this->m_name = name;  //C++ string类中有=运算符,以及一些非常方便的字符串操作函数,应用起来更加方便
  21.     }
  22.     // 设置位置
  23.     void setlocation(const string loc)
  24.     {
  25.         this->m_location = loc;
  26.     }
  27.     // 设置气候
  28.     void setweather(const string weather)
  29.     {
  30.         this->m_weather = weather;
  31.     }
  32.     // 成员变量读取函数
  33.     string getname()
  34.     {
  35.         return this->m_name;
  36.     }
  37.     string getlocation()
  38.     {
  39.         return this->m_location;
  40.     }
  41.     string  getweather()
  42.     {
  43.         return this->m_weather;
  44.     }

  45.     void printInfo() // 打印城市信息。
  46.     {
  47.         cout << "Name: "<< this->m_name << endl;
  48.         cout << "Location: " << this->m_location << endl;
  49.         cout << "Weather: " << this->m_weather << endl;
  50.     }
  51. //成员变量声明
  52. private: // 为了数据安全,一般情况下成员变量以私有变量形式出现。成员变量的访问只能通过定义好的成员函数进行。
  53.     string m_name;
  54.     string m_location;
  55.     string m_weather;
  56. };
  57. int main()
  58. {
  59.     // 声明一个City类的对象并初始化化
  60.     City myCity("Yixing", "It lcates in the south of Jiangsu province", "Wet");
  61.     // 打印对象myCity的城市信息
  62.     myCity.printInfo(); //调用成员函数
  63.     cout << myCity.getname() << myCity.getlocation() << myCity.getweather() << endl; //采用cout,调用成员变量读取函数
  64.    
  65.     //更新城市信息。
  66.     myCity.setname("Xi'an");
  67.     myCity.setlocation("Locates in the central part of Shaanxi province");
  68.     myCity.setweather("Moderate");
  69.     // 打印城市信息
  70.     myCity.printInfo();

  71.     City anotherCity; //这里会调用默认构造函数,由于我写的默认构造函数体是空的,所以成员变量没有赋值
  72.     anotherCity.printInfo();// 由于成员变量没有初始化,所以打印出来应该是所有变量都是空字符串
  73.     anotherCity.setname("Beijing"); //设置对象anotherCity的m_name;
  74.     anotherCity.printInfo(); //打印城市信息,可以看到名字已经被更新
  75.     system("pause");
  76.     return 0;
  77. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-2 17:25:49 | 显示全部楼层
major_lyu 发表于 2020-3-2 00:27
给你写了一个C++风格的:

十分感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-2 17:27:33 | 显示全部楼层
major_lyu 发表于 2020-3-2 00:27
给你写了一个C++风格的:

解释得很详细,谢谢大佬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-10 14:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表