LHL'PTA 实验7. 继承(一)

编程入门 行业动态 更新时间:2024-10-28 19:19:53

<a href=https://www.elefans.com/category/jswz/34/1748296.html style=LHL'PTA 实验7. 继承(一)"/>

LHL'PTA 实验7. 继承(一)

实验7. 继承(一)
6-1 派生类使用基类的成员函数 (15分)
按要求完成下面的程序:
1、定义一个Animal类,成员包括:
(1)整数类型的私有数据成员m_nWeightBase,表示Animal的体重;
(2)整数类型的保护数据成员m_nAgeBase,表示Animal的年龄;
(3)公有函数成员set_weight,用指定形参初始化数据成员nWeightBase;
(4)公有成员函数get_weight,返回数据成员nWeightBase的值;
(5)公有函数成员set_age,用指定形参初始化数据成员m_nAgeBase;
2、定义一个Cat类,公有继承自Animal类,其成员包括:
(1)string类型的私有数据成员m_strName;
(2)带参数的构造函数,用指定形参对私有数据成员进行初始化;
(3)公有的成员函数print_age,功能是首先输出成员m_strName的值,然后输出“, age = ”,接着输出基类的数据成员m_nAgeBase的值。具体输出格式参见main函数和样例输出。

类和函数接口定义:
参见题目描述。

裁判测试程序样例:
#include
#include
using namespace std;

/* 请在这里填写答案 */

int main()
{
Cat cat(“Persian”); //定义派生类对象cat
cat.set_age(5); //派生类对象调用从基类继承的公有成员函数
cat.set_weight(6); //派生类对象调用从基类继承的公有成员函数
cat.print_age(); //派生类对象调用自己的公有函数
cout << "cat weight = " << cat.get_weight() << endl;
return 0;
}

输入样例:
本题无输入。

输出样例:
Persian, age = 5
cat weight = 6

class Animal {
private:int m_nWeightBase;//LHL
protected:int m_nAgeBase;
public:void set_weight(int w) {m_nWeightBase = w;}int get_weight() { return m_nWeightBase; }void set_age(int a) {m_nAgeBase = a;}
};class Cat :private Animal {		//私有继承
private:string m_strName;//LHL
public:Cat(string name) {m_strName = name;}void set_print_age() {//LHLset_age(5);cout << m_strName <<", age = " << m_nAgeBase<<endl;}void set_print_weight() {set_weight(6);cout << m_strName << ", weight = " << get_weight() << endl;}
};

6-2 汽车类的继承 (15分)
根据给定的汽车类vehicle(包含的数据成员有车轮个数wheels和车重weight)声明,完成其中成员函数的定义,之后再定义其派生类并完成测试。
小车类car是它的派生类,其中包含载人数passenger_load。每个类都有相关数据的输出方法。

Vehicle类声明如下:
#include
using namespace std;
class Vehicle
{
protected:
int wheels;
float weight;
public:
Vehicle(int wheels,float weight);
int get_wheels();
float get_weight();
float wheel_load();
void show();
};

/* 请在这里填写答案 */

裁判测试程序样例:
int main ()
{
Vehicle v(4,1000);
v.show();
Car car1(4,2000,5);
car1.show ();
return 0;
}

输出样例:
在这里给出相应的输出。例如:

Type:Vehicle
Wheel:4
Weight:1000kg
Type:Car
Type:Vehicle
Wheel:4
Weight:2000kg
Load:5 persons

.html

-1 公有继承中派生类Student对基类Person成员的访问 (20分)
题目内容: 已知基类Person的定义如下:

class Person
{
string Name;
char Sex;
int Age;
public:
void Register(string name, int age, char sex)
{
Name=name;
Age=age;
Sex=sex;
}
void ShowMe() { cout<<Name<<" “<<Age<<” "<<Sex<<endl; }
};

请通过继承的方法建立一个派生类Student,其中

1.新增的数据成员有:

int Number; //学号

string ClassName; //班级

2.新增的成员函数有:

void RegisterStu(string classname, int number, string name, int age, char sex) //对数据成员赋值,并使用基类的Register

void ShowStu() //显示数据成员信息,并使用基类的ShowMe

在主程序中建立一个派生类对象,利用已有的成员函数分别显示派生类对象和基类对象的数据成员。

输入格式:
学生的班级、学号、姓名、年龄和性别

输出格式:
先输出派生类对象的数据成员,依次为 学号、班级、姓名、年龄和性别

再输出基类的数据成员,依次为姓名、年龄和性别

输入样例:
在这里给出一组输入。例如:

18软件班 180001 李木子 18 m

输出样例:
在这里给出相应的输出。例如:

180001 18软件班 李木子 18 m
李木子 18 m

#include<iostream>
#include <cstring>//LHL
using namespace std;class Person
{
private:char Name[20];char Sex;int Age;
public:void Register(char *name, int age, char sex){strcpy(Name, name);Age = age;Sex = sex;}void ShowMe(){//LHLcout<<Name<<" "<<Age<<" "<<Sex<<endl;}
};class Student:public Person
{
private:int Number;char ClassName[10];
public:void RegisterStu(char *classname, int number, char *name, int age, char sex){strcpy(ClassName, classname);Number = number;Register(name, age, sex);}void ShowStu(){cout<<Number<<" "<<ClassName<<" ";ShowMe();}};int main()
{Student s1;char classname[10],name[20],sex;int number,age;//LHLcin>>classname>>number>>name>>age>>sex;s1.RegisterStu(classname, number, name, age, sex);s1.ShowStu();s1.Person::Register(name, age, sex);s1.Person::ShowMe();return 0;
}

7-2 定义基类Point和派生类Circle,求圆的周长. (20分)
定义基类Point(点)和派生类Circle(圆),求圆的周长。Point类有两个私有的数据成员float x,y;Circle类新增一个私有的数据成员半径float r和一个公有的求周长的函数getCircumference();主函数已经给出,请编写Point和Circle类。

#include
#include
using namespace std;
//请编写你的代码
int main()
{
float x,y,r;//LHL
cin>>x>>y>>r;
Circle c(x,y,r);
cout<<fixed<<setprecision(2)<<c.getCircumference()<<endl;
return 0;
}

输入格式:
输入圆心和半径,x y r中间用空格分隔。

输出格式:
输出圆的周长,小数点后保留2位有效数字。

输入样例:
1 2 3

输出样例:
在这里给出相应的输出。例如:

Point constructor called
Circle constructor called
18.84
Circle destructor called
Point destructor called

#include<iostream>
#include<iomanip>
#define PI 3.14
using namespace std;
class Point {
protected://LHLfloat X, Y;
public:Point() {}Point(float x, float y) :X(x), Y(y) {cout << "Point constructor called" << endl;}~Point() {cout << "Point destructor called" << endl;}
};
class Circle :public Point {float R;
public:float c;Circle() {}Circle(float x, float y, float r) :Point(x, y), R(r) {cout << "Circle constructor called" << endl;};~Circle() {cout << "Circle destructor called" << endl;}float getCircumference();
};
float Circle::getCircumference() {c = PI * R * 2;return c;//LHL
}int main()
{float x, y, r;cin >> x >> y >> r;Circle c(x, y, r);cout << fixed << setprecision(2) << c.getCircumference() << endl;return 0;
}

7-3 点类 (30分)
给出下面的一个基类框架:

class Point_1D

{
protected:

float x;//1D 点的x坐标

public:

Point_1D(float p = 0.0);

float distance( );//计算当前点到原点的距离

}

以Point_1D为基类建立一个派生类Point_2D,增加一个保护数据成员:

float y;//2D平面上点的y坐标

以Point_2D为直接基类再建立一个派生类Point_3D,增加一个保护数据成员:

float z;//3D立体空间中点的z坐标

生成上述类并编写主函数,根据输入的点的基本信息,建立点对象,并能计算该点到原点的距离。

输入格式: 测试输入包含若干测试用例,每个测试用例占一行(点的类型(1表示1D点,2表示2D点,3表示3D点)点的坐标信息(与点的类型相关))。当读入0时表示结束。

输入样例:
在这里给出一组输入。例如:

1 -1
2 3 4
3 1 2 2
0

输出样例:
在这里给出相应的输出。例如:

Distance from Point -1 to original point is 1
Distance from Point (3,4) to original point is 5
Distance from Point (1,2,2) to original point is 3


#include<iostream>
#include<math.h>
using namespace std;//LHL
class Point_1D
{  protected:float x;//1D 点的x坐标public://Point_1D(float p = 0.0);void set_1D(){cin>>x;}float distance(const Point_1D & p2);
};
class Point_2D:public Point_1D
{protected:float y;//2D平面上点的y坐标public:void set_2D(){set_1D();cin>>y;}float distance(const Point_2D & p2);
};
class Point_3D:public Point_2D
{protected:float z;//3D立体空间中点的z坐标public:void set_3D(){set_2D();cin>>z;}float distance(const Point_3D & p2);
};
int main()
{int type;Point_1D a1,a2;Point_2D b1,b2;//LHLPoint_3D c1,c2;cin>>type;while(type){switch(type){case 1:a1.set_1D();a2.set_1D();a1.distance(a2);break;case 2:b1.set_2D();b2.set_2D();b1.distance(b2);break;case 3:c1.set_3D();c2.set_3D();c1.distance(c2);break;}cin>>type;}return 0;
}
float Point_1D::distance(const Point_1D & p2)
{float a;a=fabs(x-p2.x);//LHLcout<<"Distance from Point "<<x<<" to Point "<<p2.x<<" is "<<a<<endl;return a;
}
float Point_2D::distance(const Point_2D & p2)
{float a;//LHLa=sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y));cout<<"Distance from Point("<<x<<","<<y<<") to Point("<<p2.x<<","<<p2.y<<") is "<<a<<endl;return a;
}
float Point_3D::distance(const Point_3D & p2)
{float a;a=sqrt((x-p2.x)*(x-p2.x)+(y-p2.y)*(y-p2.y)+(z-p2.z)*(z-p2.z));cout<<"Distance from Point("<<x<<","<<y<<","<<z<<") to Point("<<p2.x<<","<<p2.y<<","<<p2.z<<") is "<<a<<endl;return a;
}

更多推荐

LHL'PTA 实验7. 继承(一)

本文发布于:2024-02-06 09:08:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1747863.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:LHL   PTA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!