第9周编程作业
1设计Person类(20分)
题目内容:
设计一个Person类,包含name、age、sex属性以及对这些属性操作的方法。实现并测试这个类。
根据类的封装性要求,把name、age、sex声明为私有的数据成员,声明公有的成员函数Register()、ShowMe()来访问这些属性,在Register()函数中对数据成员进行初始化。person1通过cin来得到信息,person2通过Register(“Zhang3”,
19, ‘m’) 来得到信息。
输入格式:
person1的信息
输出格式:
person1和person2的信息
输入样例:
Li4 18 f
输出样例:
Li4 18 f
Zhang3 19 m
//Person类
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
char name[20];
char sex;
int age;
public:
void Register(const char nn[], int aa,char ss );
void ShowMe();
};
void Person::Register(const char nn[], int aa, char ss)
{
strcpy_s(name, nn);//strcpy_s()和strcpy()在这里是一样的
sex = ss;
age = aa;
}
void Person::ShowMe()
{
cout << name << " " << age << " " << sex << endl;
}
int main()
{
char Name[20];
char Sex;
int Age;
cin >> Name >> Age >> Sex;
Person a, b;
a.Register(Name, Age, Sex);
b.Register("Zhang3", 19,'m');
a.ShowMe();
b.ShowMe();
return 0;
}
2设计Dog类(20分)
题目内容:
设计一个Dog类,包含name、age、sex和weight等属性以及对这些属性操作的方法。实现并测试这个类。
根据类的封装性要求,把name、age、sex和weight声明为私有的数据成员,编写公有成员函数setdata()对数据进行初始化,GetName()、GetAge()、GetSex()和GetWeight()获取相应属性。初始化数据由用户输入。
输入格式:
Dog类对象的初始化数据
输出格式:
根据Dog类对象的初始化数据输出一句话,请严格按照格式输出,句末有点号。
输入样例:
ahuang 3 m 2.4
输出样例:
It is my dog.
Its name is ahuang.
It is 3 years old.
It is male.
It is 2.4 kg.
//设计dog类
//故意写复杂的
#include<iostream>
#include<cstring>
using namespace std;
class Person
{
private:
char name[20];
char sex;
int age;
public:
void Register(const char nn[], int aa, char ss);
char* Getname() { return name; }
char Getsex() { return sex; }
int Getage() { return age; }
};
void Person::Register(const char nn[], int aa, char ss)
{
strcpy_s(name, nn);
sex = ss;
age = aa;
}
class Dog //由之前所写的Person类补充Dog类
{
private:
Person person;
double weight;
public:
void setdata(Person pp,double ww);
char* GetName();
char GetSex();
int GetAge();
double Getweight();
};
char* Dog::GetName()
{
return person.Getname();
}
char Dog::GetSex()
{
return person.Getsex();
}
int Dog::GetAge()
{
return person.Getage();
}
double Dog::Getweight()
{
return weight;
}
void Dog::setdata(Person pp,double ww)
{
person = pp;
weight = ww;
}
int main()
{
Person pp;
Dog dd;
char nn[20];
int aa;
char ss;
double ww;
cin >> nn >> aa >> ss >> ww;
pp.Register(nn, aa, ss);
dd.setdata(pp, ww);
cout << "It is my dog." << endl;
cout << "Its name is " << dd.GetName() << "." << endl;
cout << "It is " << dd.GetAge() << " years old." << endl;
if (dd.GetSex() - 'm' == 0)
cout << "It is male." << endl;
else
cout << "It is female." << endl;
cout << "It is " << dd.Getweight() << " kg." << endl;
return 0;
}
3设计并测试Trapezium类(20分)
题目内容:
设计并测试一个名为Trapezium的梯形类,其属性为梯形的四个顶点的坐标。该梯形上边和下边均和x轴平行。
根据类的封装性要求,在类的声明中用8个私有的整型变量表示4个点的坐标值,声明成员函数initial(int,int,int,int,int,int,int,int)初始化数据成员,函数GetPosition(int&,int&,int&,int&,int&,int&,int&,int&)读取坐标值,函数Area()计算面积。
输入格式:
梯形四个顶点的坐标,
输出格式:
梯形的面积,依次为左上(x1,y1)、右上(x2,y2)、左下(x3,y3)和右下(x4,y4)角的顶点。
输入样例:
3 2 5 2 1 -4 7 -4
输出样例:
24
//名为Trapezium的梯形类
#include<iostream>
using namespace std;
class Trapezium
{
private:
int x1, x2, x3, x4;
int y1, y2, y3, y4;
public:
void initial(int, int, int, int, int, int, int, int);
void GetPosition(int&, int&, int&, int&, int&, int&, int&, int&);
double Area();
};
void Trapezium::initial(int xx1, int yy1, int xx2, int yy2, int xx3, int yy3,int xx4, int yy4)
{
x1 = xx1;
y1 = yy1;
x2 = xx2;
y2 = yy2;
x3 = xx3;
y3 = yy3;
x4 = xx4;
y4 = yy4;
}
void Trapezium::GetPosition(int& xx1, int& yy1, int& xx2, int& yy2, int& xx3, int& yy3, int& xx4, int& yy4)
{
cout << xx1 << ' ' << yy1 << ' ' << xx2 << ' ' << yy2 << ' ' << xx3 << ' ' << yy3;
cout << ' ' << xx4 << ' ' << yy4;
}
double Trapezium::Area()
{
return 1.0 * (x2 - x1 + x4 - x3) * (y1 - y3) / 2;
}
int main()
{
int a, b, c, d, e, f, g, h;
cin >> a >> b >> c >> d >> e >> f >> g >> h;
Trapezium t;
t.initial(a, b, c, d, e, f, g, h);
cout << t.Area();
return 0;
}