定义CMyRectangle类描述平面中的一个矩形,矩形由左上角坐标(x、y),宽度和长度来描述。要求可以设置和得到矩形的这些信息,并能通过PrintInfo函数输出矩形的相关信息,通过GetArea函数得到矩形的面积,通过GetCircumference函数得到矩形的周长。在main函数里定义一个CMyRectangle对象,为这个对象设置相关信息,调用GetArea函数输出矩形的面积,调用GetCircumference输出矩形的周长。要求创建两个对象,一个用正常形式,另一个采用指针形式。分别调用进行输出
代码如下
头文件(juxing.h)如下:
#pragma once
class CMYRectangle {
private:
int x, y;
int wide; //矩形的宽
int length; //矩形的长
public:
void setx();//输入x的值
int getx();//得到x的值
void sety();//输入y的值
int gety();//得到y的值
void setwide(); //输入矩形的宽
int getwide();//得到矩形的宽
void setlength();//输入矩形的长
int getlength();//得打矩形的长
int GetArea(int wide, int length); //求矩形的面积
int GetCircumference(int x, int y); //求矩形的周长
void PrintInfo(); //输出矩形的信息
};
main文件如下:
#include "juxing.h"
#include<iostream>
using namespace std;
class CMYRectangle c;
class CMYRectangle* p = &c;
int main()
{
cout << "正常形式"<<endl;
c.setx();
c.sety();
c.setwide();
c.setlength();
c.GetArea(c.getwide(), c.getlength());
c.GetCircumference(c.getwide(), c.getlength());
c.PrintInfo();
cout << "指针形式"<<endl;
p->setx();
p->sety();
p->setwide();
p->setlength();
p->GetArea(p->getwide(), p->getlength());
p->GetCircumference(p->getwide(), p->getlength());
p->PrintInfo();
return 0;
}
void CMYRectangle::setx()
{
cout << "输入矩形左上角x轴坐标: ";
cin >> x;
}
int CMYRectangle::getx()
{
return x;
}
void CMYRectangle::sety()
{
cout << "输入矩形左上角y轴坐标: ";
cin >> y;
}
int CMYRectangle::gety()
{
return y;
}
void CMYRectangle::setwide()
{
cout << "矩形的宽: ";
cin >> wide;
}
int CMYRectangle::getwide()
{
return wide;
}
void CMYRectangle::setlength()
{
cout << "矩形的长: ";
cin >> length;
}
int CMYRectangle::getlength()
{
return length;
}
int CMYRectangle::GetArea(int wide, int length)
{
return wide * length;
}
int CMYRectangle::GetCircumference(int wide, int length)
{
return 2 * (wide + length);
}
void CMYRectangle::PrintInfo()
{
cout << "矩形的左上角坐标为: " << "(" << x << "," << y << ")" << endl;
cout << "矩形的宽和长为: " << c.getwide() << "," << c.getlength() << endl;
cout << "矩形的面积为: " << c.GetArea(c.getwide(), c.getlength()) << endl;
cout << "矩形的周长为: " << c.GetCircumference(c.getwide(), c.getlength()) << endl;
}
运行截图如下:
定义一个满足下列要求的类CDate:
1)有三个私有成员数据:年、月、日;
2)有设置日期的公有成员函数。(最好在设置日期的成员函数里加入数据合法检查。)
3)有用格式“年/月/日”输出日期的公有成员函数
4)有对当前日期加一天的公有成员函数。
5)有判断是否为闰年的私有成员函数。
设计主函数,在主函数中创建对象;调用设置日期的成员函数,设置当天的日期;调用输出日期的成员函数输出当天的日期;调用对当前日期加一天的成员函数,然后再调用输出日期的成员函数,输出第二天的日期。
注意:闰年的2月的天数为29天,其它年份2月28天,闰年是指:年份能被4且不能被100整除,或者年份能被400整除。
代码如下:
头文件(time.h)如下:
#pragma once
class CDate {
private:
int year, month, day;//年,月,日
int leapyear(int year);//判断是否为润年
public:
int getyear();//得到year
int getmonth();//得到month
int getday();//得到day
void settime();//设置日期
void gettime();//输出日期
int judge(int year)
{
return leapyear(year);
}
int Can_Plus();
int nextday();//加一天
};
main文件如下:
#include "time.h"
#include<iostream>
using namespace std;
int main()
{
CDate T;
T.settime();
T.Can_Plus();
T.nextday();
T.gettime();
return 0;
}
int CDate::getyear()
{
return year;
}
int CDate::getmonth()
{
return month;
}
int CDate::getday()
{
return day;
}
int CDate::Can_Plus()
{
int flag;
int days;
flag=judge(year);
switch (getmonth())
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
{
days = 31; break;
}
case 2:
{
days = (flag == 1) ? 29 : 28;
}
default:
{
days = 30;
break;
}
}
return days;
}
int CDate::nextday()
{
if (day < Can_Plus())
day += 1;
else if (day == Can_Plus())
{
day = 1;
if (month <= 11)
month += 1;
else
{
month = 1;
year += 1;
}
}
return 1;
}
int CDate::leapyear(int year)
{
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
return 1;
else
return 0;
}
void CDate::settime()
{
cin >> year >> month >> day;
}
void CDate::gettime()
{
cout << getyear()<< '/' << getmonth()<< '/' << getday();
}
运行结果如下:
根据下面的类图,编写成绩这个类。
建立一个新的win32控制台应用程序,命名demo,添加一个新的类score。将该类的定义写在score.h中,类的实现写入score.cpp中。编写一个程序,要求输入10个学生成绩后,最后输出他们的平均分和不及格人数
代码如下:
头文件(score.h)如下:
#pragma once
class Score {
private:
int score[10];//成绩数组
int avgscore;//平均分
int filepeople;//不及格人数
public:
void import();//输入成绩
void avg(); //计算平均分
void file();//统计不及格人数
};
main文件如下:
#include "score.h"
#include<iostream>
using namespace std;
void main()
{
Score S;
S.import();
S.avg();
S.file();
}
void Score::import()
{
cout << "请输入十个人的分数: " << endl;
for (int i = 0; i < 10; i++)
{
cin >> score[i];
}
}
void Score::avg( )
{
int sum = 0;
for (int i = 0; i < 10; i++)
{
sum += score[i];
}
avgscore = sum / 10;
cout << "平均分是: " << avgscore<<endl;
}
void Score::file()
{
int p = 0;
for (int i = 0; i < 10; i++)
{
if (score[i]< 60)
p++;
}
filepeople = p;
cout << "不及格人数是: "<<filepeople;
}
运行截图如下: