C++的第四个实验

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


问题一

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

错误代码

#include <iostream>
    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<cstdio>
#include<iostream>
#include<cmath>
#include<string.h>
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<cstdio>
#include<iostream>
#include<cmath>
#include<string.h>
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<cstdio>
#include<iostream>
#include<cmath>
#include<string.h>
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<cstdio>
#include<iostream>
#include<cmath>
#include<string.h>
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;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值