• C++第四次实验


    好久没做实验了,来做个实验吧~


    问题一

    1、检查下面的程序,找出其中的错误,并改正之。然后上机调试,使之能正常运行。

    错误代码

    #include 
        using namespace std;
        class Clock
        {   void set_clock(void);
        void show_clock(void);  
            int hour;
        int minute;
        int second;
    };
    Clock clock;
        int main()
    {
        set_clock();
       show_clock ();
         }
    int set_clock(void)
    {
        cin>>t.hour;
        cin>>t.month;
        cin>>t.second;
    }
        int show_clock(void)
    {  
    cout<<t.hour<<":"<<t.minute<<":"<<t.second<<endl;
      }
    

    修改后的代码

    #include
    #include
    #include
    #include
    using namespace std;
    
    class Clock
    {
    public:
        void set_clock(void);
        void show_clock(void);
        int hour;
        int minute;
        int second;
    };
    Clock t;
    int main()
    {
        t.set_clock();
        t.show_clock();
    }
    void Clock::set_clock(void)
    {
        cin >> t.hour;
        cin >> t.minute;
        cin >> t.second;
    }
    void Clock::show_clock(void)
    {
        cout << t.hour << ":" << t.minute << ":" << t.second << endl;
    }
    
    问题二

    建立一个名为Student的类,该类有以下几个私有成员变量:学生姓名、学号、性别、年龄。还有以下两个成员函数:
    (1)SetStu:用于初始化学生姓名、学号、性别和年龄的构造函数;
    (2)PrintStu:用于输出学生信息的函数。
    编写一个主函数,创建一个学生对象,然后调用成员函数输入学生对象数据,并将该学生对象的信息在屏幕上输出。

    
    #include
    #include
    #include
    #include
    using namespace std;
    
    class Student
    {
    public:
    	Student();
    
    	void PrintStu();
    
    private:
    	string student_name;//学生的名字
    	int student_id;//学生的学号
    	bool student_sex;//1表示男生,0表示女生
    	int student_age;//学生的年纪
    };
    
    void Student::PrintStu()
    {
    	cout << "The name of the student is:" << Student::student_name << endl;
    	cout << "The id of the student is:" << Student::student_id << endl;
    	if (Student::student_sex == 1)
    	{
    		cout << "The sex of the student is:" << "男" << endl;
    	}
    	else
    	{
    		cout << "The sex of the student is:" << "女" << endl;
    	}
    	
    	cout << "The age of the student is:" << Student::student_age << endl;
    }
    
    Student::Student()
    {
    	string a;
    	int id, sex, age;
    	cout << "学生的名字是:" ;
    	cin >> a;
    	Student::student_name = a;
    	cout << "学生的学号是:" ;
    	cin >> id;
    	Student::student_id = id;
    	cout << "学生的性别是:" ;
    	cin >> sex;
    	Student::student_sex = sex;
    	cout << "学生的年纪是:" ;
    	cin >> age;
    	Student::student_age = age;
    }
    int main()
    {
    	Student student;
    	student.PrintStu();
    	return 0;
    }
    
    
    

    效果如下

    在这里插入图片描述

    问题三

    定义一个名为Time 的时间类,包含时、分、秒等属性,对这些属性操作的方法有:
    (1)printMilitary():输出24小时制时间(如13:25:39);
    (2)printStandard():输出标准格式时间(如 1:25:39 PM)
    实现并测试这个类。

    
    //实现并测试这个类。
    #include
    #include
    #include
    #include
    using namespace std;
    class Time
    {
    public:
    //	Time();
    //	~Time();
    	void printMilitary();
    	void printStandard();
    
    private:
    	int second = 16;
    	int minute = 24;
    	int hour = 3;
    };
    
    void Time::printMilitary()
    {
    	cout << hour << ":" << minute << ":" << second << endl;
    
    }
    
    void Time::printStandard()
    {
    	if (hour > 12 && hour < 24)
    	{
    		cout << hour % 12 << ":" << minute << ":" << second << "PM" << endl;
    	}
    	else if (hour < 24)
    	{
    		cout << hour << ":" << minute << ":" << second << "AM" << endl;
    
    	}
    	
    }
    
    
    int main()
    {
    	Time time;
    	time.printMilitary();
    	time.printStandard();
    	return 0;
    }
    
    问题四

    (选做)设计一个职工类Employee。数据成员包括字符串型的name(姓名)、street(街道地址)、city(市)、province(省)、zip(邮政编码)。要求用成员函数实现以下功能:
    (1)可动态地设置职工对象信息。
    (2)在屏幕上打印职工信息。
    实现并测试这个类。

    #include
    #include
    #include
    #include
    using namespace std;
    
    class Employee
    {
    public:
    	string employee_name;
    	string chastreet;
    	string city;
    	string province;
    	int zip;
    	void PrintEmployee();
    	void ChangeEmployee();
    
    };
    
    void Employee::PrintEmployee()
    {
    	cout << "名字是:" << employee_name << endl;
    	cout << "街道是:" << chastreet << endl;
    	cout << "城市是:" << city << endl;
    	cout << "省份是:" << province << endl;
    	cout << "邮政编码:" << zip << endl;
    }
    
    void Employee::ChangeEmployee()
    {
    	cin >> employee_name >> chastreet >> city >> province >> zip;
    }
    Employee t;
    int main()
    {
    	t.ChangeEmployee();
    	t.PrintEmployee();
    	return 0;
    }
    
  • 相关阅读:
    【2022国赛模拟】[SDSC Day5] 毒药——自适应交互、DP
    如何对 Kubernetes 节点进行运维
    荧光素醋酸,CAS号: 96087-38-6
    通讯网关软件007——利用CommGate X2Mbt实现Modbus TCP访问MSSQL服务器
    Spring 定时任务如何到达某一指定时间点后,触发任务机制
    华为手机怎么开启生产模式 | 华为手机打开生产模式后有驱动没有安装好怎么办 | 华为荣耀9手机打开生产模式后有驱动有叹号怎么办
    小程序中的confirm-type设置键盘的确认按钮
    maxwell读取mysql的binlog并发送kafka
    Vue Router 源码分析
    Java类加载机制
  • 原文地址:https://blog.csdn.net/weixin_62529383/article/details/127032319