E27 [第9章:类成员,类成员的构造(冒号语法)]下面的一段程序中的Cline类中缺少了部分成员函数,该程序的运行结果如下:
Point 1 is:(0,0) Point 2 is:(0,0) Length=0 Point 1 is:(1,1) Point 2 is:(5,5) Length=5.65685
请为Cline函数补充必要的成员函数与实现代码,使得程序正确运行。
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
int m_x; //点的X坐标
int m_y; //点的Y坐标º
public:
CPoint()
{
m_x=0;
m_y=0;
}
CPoint (int x,int y)
{
m_x=x;
m_y=y;
}
int getx()
{
return m_x;
}
int gety()
{
return m_y;
}
void showPoint()
{
cout<<"("<<this->m_x<<","<<this->m_y<<")"<<endl;
}
};
class CLine
{
CPoint m_point1;
CPoint m_point2;
};
void main()
{
CLine line1;
line1.ShowLine();
cout<<"Length="<<line1.distance()<<endl;
CLine line2(1,1,5,5);
line2.ShowLine();
cout<<"Length="<<line2.distance()<<endl;
}
以下是答案,超级简单
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
int m_x; //点的X坐标
int m_y; //点的Y坐标?
public:
CPoint()
{
m_x=0;
m_y=0;
}
CPoint (int x,int y)
{
m_x=x;
m_y=y;
}
int getx()
{
return m_x;
}
int gety()
{
return m_y;
}
void showPoint()
{
cout<<"("<<this->m_x<<","<<this->m_y<<")"<<endl;
}
};
class CLine
{
private:
CPoint m_point1;
CPoint m_point2;
public:
void ShowLine()
{
cout<<"Point1 is :";m_point1.showPoint();
cout<<"Point2 is :";m_point2.showPoint();
}
double distance()
{
return sqrt((m_point1.getx()-m_point2.getx())*(m_point1.getx()-m_point2.getx())+(m_point1.gety()-m_point2.gety())*(m_point1.gety()-m_point2.gety()));
}
CLine(int a=0,int b=0,int c=0,int d=0):m_point1(a,b),m_point2(c,d)
{
}
};
int main()
{
CLine line1;
line1.ShowLine();
cout<<"Length="<<line1.distance()<<endl;
CLine line2(1,1,5,5);
line2.ShowLine();
cout<<"Length="<<line2.distance()<<endl;
return 0;
}
E28 [第9章:类成员,类成员的构造(冒号语法)]设有类CTime和CDate分别用于描述时间和日期,另外有CDateTime类描日期和时间,请为三个类给出具体的实现代码,并在main函数中测试。
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
//原来题目里面基本只有定义,就是一个比较简单的填充过程
class CTime
{
int m_hour,m_minute,m_second;
public:
CTime(int hour=0,int minute=0,int second=0)
//如果时间合法,则赋值,否则赋值0时0分0秒
{
if(hour<0||hour>23||minute>=60||minute<0||second<0||second>=60)
{
hour=0;
minute=0;
second=0;
}
m_hour=hour;
m_minute=minute;
m_second=second;
}
int SetTime(int hour=0,int minute=0,int second=0)
//如果时间合法,则赋值并返回1,否则不赋值,并返回0
{
if(hour<0||hour>23||minute>=60||minute<0||second<0||second>=60)
{
hour=0;
minute=0;
second=0;
return 0;
}
m_hour=hour;
m_minute=minute;
m_second=second;
return 1;
}
int getHour()
{
return m_hour;
}
int getMinute()
{
return m_minute;
}
int getSecond()
{
return m_second;
}
void ShowTime(bool flag)
//flag为True则以24小时制显示时间,否则以 AM PM的方式显示
{
if(flag)
{
cout<<setw(2)<<m_hour<<":"
<<setw(2)<<m_minute<<":"
<<setw(2)<<m_second<<endl;
}
else
{
if(m_hour>12)
{
cout<<setw(2)<<setfill('0')<<m_hour-12<<":"
<<setw(2)<<setfill('0')<<m_minute<<":"
<<setw(2)<<setfill('0')<<m_second<<".PM"<<endl;
}
else
{
cout<<setw(2)<<setfill('0')<<m_hour<<":"
<<setw(2)<<setfill('0')<<m_minute<<":"
<<setw(2)<<setfill('0')<<m_second<<".AM"<<endl;
}
}
}
//自己考虑是否需要添加其他成员函数
};
class CDate
{
int m_year,m_month,m_day;
public:
CDate(int year=2000,int month=1,int day=1)
//如果日期合法,则赋值,否则赋值2000年1月1日
{
int temp[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(month>12)
{
year=2000;
month=1;
day=1;
}
if(year%400==0||(year%4==0&&year%100!=0))
{
temp[1]++;
}
if(day>temp[month-1])
{
year=2000;
month=1;
day=1;
}
m_year=year;
m_month=month;
m_day=day;
}
int SetDate(int year=2000,int month=1,int day=1)
//如果日期合法,则赋值并返回1,否则不赋值,并返回0
{
int temp[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(month>12)
{
year=2000;
month=1;
day=1;
return 0;
}
if(year%400==0||(year%4==0&&year%100!=0))
{
temp[1]++;
}
if(day>temp[month-1])
{
year=2000;
month=1;
day=1;
return 0;
}
m_year=year;
m_month=month;
m_day=day;
return 1 ;
}
int GetYear()
{
return m_year;
}
int GetMonth()
{
return m_month;
}
int GetDay()
{
return m_day;
}
void ShowDate(bool flag)
//flag为TRUE,则以中文的方式显示日期,否则以MM/DD/YYYY的方式显示日期
{
if(flag)
{
cout<<m_year<<"年"<<m_month<<"月"<<m_day<<"日"<<endl;
}
else
{
cout<<setw(2)<<setfill('0')<<m_month<<"/"<<setw(2)<<setfill('0')<<m_day<<"/"<<setw(4)<<setfill('0')<<m_year<<endl;
}
}
//自己考虑是否需要添加其他成员函数
};
class CDateTime
{
CTime m_time;
CDate m_date;
public:
//添加必要的构造函数
CDateTime(int a=2000,int b=1,int c=1,int d=0,int e=0,int f=0):m_time(d,e,f),m_date(a,b,c)
{
}
int SetDateTime(int a,int b,int c,int d,int e,int f)
//自己设计参数,考虑该函数的返回值加以表示全部正确、日期不对、时间不对等情况
{
if(m_date.SetDate(a,b,c)==1&&m_time.SetTime(d,e,f)==1)
{
return 1;
}
if(m_date.SetDate(a,b,c)==0&&m_time.SetTime(d,e,f)==1)
{
return -1;
}
if(m_date.SetDate(a,b,c)==1&&m_time.SetTime(d,e,f)==0)
{
return -1;
}
}
void ShowDateTime(bool a,bool b)
// 自己设计参数,考虑可以中文日期和西文日期格式,以及24小时和AM PM都可以控制
{
m_date.ShowDate(a);
m_time.ShowTime(b);
}
//添加自己认为必要的其他成员函数
int GetMonth()
{
return m_date.GetMonth();
}
int GetMinute()
{
return m_time.getMinute();
}
};
int main()
{
CDateTime d1(2014, 5, 2, 27, 12, 5);
d1.ShowDateTime(false, true); //第1个参数表示以英文方式显示日期, 第2个参数表示以24小时制方式显示时间
CDateTime d2;
d2.ShowDateTime(true, false); //第1个参数表示以中文方式显示日期, 第2个参数表示以AM PM的方式显示时间
int iStatus;
iStatus=d2.SetDateTime(2014, 5, 2, 21, 55, 5);
switch(iStatus)//吐槽一下,万一两个都不正确呢~233
{
case 1:
cout<<"日期和时间数据设置正确!"<<endl;
break;
case -1:
cout<<"日期数据设置不正确!"<<endl;
break;
case -2:
cout<<"时间数据设置不正确!"<<endl;
break;
}
d2.ShowDateTime(false, false); //第1个参数表示以英文方式显示日期, 第2个参数表示以AM PM的方式显示时间
cout<<"月="<<d2.GetMonth()<<endl; //认真考虑一下如何实现此操作?
cout<<"分钟="<<d2.GetMinute()<<endl; //认真考虑一下如何实现此操作?
return 0;
}
E29有一个person类定义如下:
class CPerson
{
private:
string m_name; // 姓名
int m_age; // 年龄
char m_sex; // 性别
public:
CPerson(const string& name, int age, char sex)
{
m_name = name;
m_age = age;
m_sex = sex;
}
CPerson()
{
m_name = "无名";
m_age = 0;
m_sex = 'M';
}
void print() const
{
cout<<"姓名:"<<m_name<<"\n年龄:"<<m_age<<"\n";
if (m_sex == 'M')
cout<<"性别:男"<<endl;
else
cout<<"性别:女"<<endl;
}
};
请以CPerson类为基类定义一个派生类CStudent,要求该类具有以下属性成员和成员函数:
学生所属大学名称,string类型表示
学生所在年级,int类型表示
CStudent(); // 以{”无名”、18岁、男性、“苏州大学”、1年级}为默认值的无参构造函数;
CStudent(string name, int , char, string collageName, int grade); // 带参数的构造函数
void print(); // 显示学生类对象的所有信息
编写对CStudent类的测试程序,要求如下:
(1) 定义一个学生类student1,属性值为默认属性值,并输出其信息
(2) 定义一个学生类student2,其属性为:
姓名:“Liming”
年龄:21
性别:男
大学:苏州大学
年级:1
测试程序如下:
int main()
{
CStudent st1,st2("Liming",21,'M',"苏州大学",1);
cout<<"学生1的信息:"<<endl;
st1.print();
cout<<"\n学生2的信息:"<<endl;
st2.print();
return 0;
}答案
//author CN.TTDragon
#include<bits/stdc++.h>
typedef long long ll;
const ll mod=1e9+7;
const ll maxn=1e5+7;
const double pi=acos(-1);
using namespace std;
class CPerson
{
private:
string m_name; // 姓名
int m_age; // 年龄
char m_sex; // 性别
public:
CPerson(const string& name, int age, char sex)
{
m_name = name;
m_age = age;
m_sex = sex;
}
CPerson()
{
m_name = "无名";
m_age = 0;
m_sex = 'M';
}
void print() const
{
cout<<"姓名:"<<m_name<<"\n年龄:"<<m_age<<"\n";
if (m_sex == 'M')
cout<<"性别:男"<<endl;
else
cout<<"性别:女"<<endl;
}
};
class CStudent : public CPerson
{
private:
string cname;
int grade;
public:
CStudent(const string &name = "无名",int age=0,char sex='M',const string &cna="苏州大学",int g=1):CPerson(name,age,sex)
{
cname=cna;
grade=g;
}
void print()
{
CPerson::print();
cout<<"大学:"<<cname<<endl;
cout<<"年级:"<<grade<<endl;
}
};
int main()
{
CStudent st1,st2("Liming",21,'M',"苏州大学",1);
cout<<"学生1的信息:"<<endl;
st1.print();
cout<<"\n学生2的信息:"<<endl;
st2.print();
return 0;
}
写在最后:
完结撒花!有个文件操作的实验五,鸽了鸽了,有机会再说吧(说鸽就鸽,也是一种不鸽!)
TT珑,考试加油!

京公网安备 11010502036488号