目录
第1关:点-线-面
任务描述
本关任务:把描述直角坐标系上的一个点的类作为基类,派生出描述一条直线的类和描述一个三角形的类。定义成员函数求出两点间的距离和三角形的面积。 提示:先定义描述点的类 Point;类 Point 的派生类 Line 为直线类,一直线有两个端点,所以它在点类的基础上新增一组点的坐标(x2,y2);三角形类 T 在直线的基础上再新增一组点的坐标(x3,y3),求出三角形的面积。具体要求如下: (1)定义点类 Point
- int x1,y1;//保护的数据成员(点坐标)
- 公有构造函数 Point(int a,int b): //初始化 x1、y1
(2)定义直线类 Line
- int x2,y2;//保护的数据成员(点坐标)。
- 公有构造函数 Line(int a,int b,int c,int d)://初始化 x2、y2,以及 x1、y1
(3)定义三角形类 Triangle
- int x3,y3; //私有的数据成员(点坐标)
- double area; //私有的数据成员(面积)
- 公有构造函数 Triangle(int a,int b,int c,int d,int e,int f)://初始化 x3、y3,以及x1、y1, x2、y2
- void Area(): //求三角形面积的功能函数,先求出三条边 x、y、z,然后用以下公式求面积:
s=(x+y+z)/2area=sqrt(s(s−x)(s−y)(s−z)) - void print(): //输出三个点的坐标和面积
测试说明
平台会对你编写的代码进行测试:
测试输入:
1 1 4 1 4 5
预期输出:
(1,1) (4,1) (4,5)area=6
代码
#include <iostream>
#include <cmath>
using namespace std;
// 点类Point
class Point {
protected:
int x1, y1;
public:
Point(int a, int b) : x1(a), y1(b) {}
};
// 直线类Line,继承自Point类
class Line : public Point {
protected:
int x2, y2;
public:
Line(int a, int b, int c, int d) : Point(a, b), x2(c), y2(d) {}
};
// 三角形类Triangle,继承自Line类
class Triangle : public Line {
private:
int x3, y3;
double area;
public:
Triangle(int a, int b, int c, int d, int e, int f) : Line(a, b, c, d), x3(e), y3(f) {}
void Area() {
// 计算三条边的长度
double x = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
double y = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
double z = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1));
double s = (x + y + z) / 2;
area = sqrt(s * (s - x) * (s - y) * (s - z));
}
void print() {
cout << "( " << x1 << "," << y1 << " ) ( " << x2 << "," << y2 << " ) ( " << x3 << "," << y3 << " )" << endl;
cout << "area=" << area << endl;
}
};
第2关:三角形-三角柱体
任务描述
本关任务:试定义类 TR1(三角形)及其派生类 COL(三角柱体)。其中三角形类可以计算三角形的面积和周长;三角柱体类可以计算柱体的体积和表面积。 具体要求如下: (1)TR1 的成员如下: 私有数据成员
- Double x,y,z; 分别表示三角形三条边的长度 公有成员函数
- TR1(double x1,double y1,double z1);构造函数,用 x1,y1,z1 分别初始化三角形的三条边。在初始化之前,必须先判断 x1、y1、z1 能否构成三角形,如果不能构成三角形,则给出提示并退出程序。判断三条边是否构成三角形的方法:任意两边之和大于第三边。
- virtual double area(); 虚函数,计算三角形的面积,计算公式为:
面积=sqrt(s(s−x)(s−y)(s−z))其中,x、y、z 分别为三角形三条边的长度,s=(x+y+z)/2。 - double peri(); 计算三角形的周长。 (2)类 COL 为类 TR1 的公有派生类,其成员如下: 私有数据成员
- double height; 表示三角柱体的高度。 公有成员函数
- COL(double x1,double y1,double z1,double h);构造函数,用 h 初始化 height,用 x1、y1、z1 分别初始化基类的成员 x、y、z。
- double volume();计算三角柱体的体积。计算公式:体积=底面积×高,其中底面积通过调用基类的成员函数 area()计算。
- double area();计算三角柱体的表面积。计算公式:表面积=2×底面积+底面周长×高度,其中底面积和底面周长分别通过调用基类的成员函数 area()和 prei()计算。
代码
#include <iostream>
#include <cmath>
using namespace std;
// 三角形类TR1
class TR1 {
private:
double x, y, z;
public:
TR1(double x1, double y1, double z1) {
// 判断能否构成三角形
if (x1 + y1 > z1 && x1 + z1 > y1 && y1 + z1 > x1) {
x = x1;
y = y1;
z = z1;
}
else {
cerr << "输入的边长不能构成三角形,请重新输入!" << endl;
exit(1);
}
}
virtual double area() {
double s = (x + y + z) / 2;
return sqrt(s * (s - x) * (s - y) * (s - z));
}
double peri() {
return x + y + z;
}
};
// 三角柱体类COL,公有派生自TR1类
class COL : public TR1 {
private:
double height;
public:
COL(double x1, double y1, double z1, double h) : TR1(x1, y1, z1), height(h) {}
double volume() {
return TR1::area() * height;
}
double area() {
return 2 * TR1::area() + TR1::peri() * height;
}
};
// int main() {
// double a, b, c, h;
// cout << "请输入三角形的底面边长和高:";
// cin >> a >> b >> c >> h;
// COL col(a, b, c, h);
// cout << "三角柱体的体积为:" << col.volume() << endl;
// cout << "三角柱体的表面积为:" << col.area() << endl;
// return 0;
// }
第3关:长方形-长方体
任务描述
本关任务:试定义 RECT 类(长方形)及其派生类 CUB(长方体),具体要求如下: (1)类 RECT 的成员如下:
(a)保护数据成员
- double x,y; 分别表示长方形的长和宽。
(b)公有成员函数
- RECT(double x1,double y1);构造函数,分别用 x1、y1 初始化 x 和 y。
- double area( ); 计算长方形的面积,计算公式:面积=长×宽。
- double peri( ); 计算长方形的周长。计算公式:周长=2×长+2×宽。
- int isSquare( ); 判断是否为正方形,如是,返回 1;否则返回 0。
(2) 类 CUB 为类 RECT 的公有派生类,其成员如下: (a) 私有数据成员
- double height; 表示长方体的高度。
(b) 公有成员函数
- CUB( ); 构造函数,用 h、x、y 分别初始化 height 及其基类成员 x 和 y。
- double volume( ); 计算长方体的体积。计算公式:体积=底面积×高,其中底面积通过调用基类成员函数 area( )计算。
- double area(); 计算长方体的表面积。计算公式:表面积=2×底面积+底面周长×高度。底面积和底面周长分别调用基类成员函数 area()和 peri()计算。
- int isSquare();判断是否为正方体,如是,返回 1,否则返回 0。在判断过程中,首先调用基类的成员函数 isSquare()判断底面是否为正方形。
代码
#include <iostream>
using namespace std;
// 长方形类RECT
class RECT {
protected:
double x, y;
public:
RECT(double x1, double y1) : x(x1), y(y1) {}
double area() {
return x * y;
}
double peri() {
return 2 * x + 2 * y;
}
int isSquare() {
return x == y? 1 : 0;
}
};
// 长方体类CUB,公有派生自RECT类
class CUB : public RECT {
private:
double height;
public:
CUB(double x1, double y1, double h) : RECT(x1, y1), height(h) {}
double volume() {
return RECT::area() * height;
}
double area() {
return 2 * RECT::area() + RECT::peri() * height;
}
int isSquare() {
return RECT::isSquare() && height == x;
}
};
// int main() {
// double length, width, height;
// cin >> length >> width >> height;
// CUB cub(length, width, height);
// cout << "长方体的体积为:" << cub.volume() << endl;
// cout << "长方体的表面积为:" << cub.area() << endl;
// if (cub.isSquare()) {
// cout << "该长方体是正方体" << endl;
// }
// else {
// cout << "该长方体不是正方体" << endl;
// }
// return 0;
// }
第4关:优秀学生/教师
任务描述
本关任务:设计评选优秀教师和优秀学生候选人的程序。如果学生的分数大于 90,则可评为优秀生;如果教师发表的论文数大于 3,则可评为优秀教师。 具体要求如下:
(1)定义基类Base: ①保护数据成员
- char name[8]; //存放姓名
- int num; //存放分数或论文数
②公有成员函数
- Base( ):构造函数,输入姓名;
- void print( ):功能函数,输出数据成员;
(2)由基类派生学生类Student,定义公有成员函数:
- Student ( ): 构造函数,输入分数;
- int Isgoodstudent( ):根据优秀学生的标准,满足条件返回1,否则返回0;
(3)由基类派生教师类Teacher,定义公有成员函数:
- Teacher ( ): 构造函数,输入论文数;
- int Isgoodteacher( ):根据优秀教师的标准,满足条件返回1,否则返回0;
代码
#include <iostream>
#include <cstring>
using namespace std;
// Base class
class Base {
protected:
char name[8]; // Store name
int num; // Store score or number of papers
public:
// Constructor to input name
Base() {
cout << "姓名:";
cin >> name;
}
// Function to print data members
void print() const {
cout << "姓名:" << name <<" " << num << endl;
}
};
// Derived class for Student
class Student : public Base {
public:
// Constructor to input score
Student() {
cout << "考试成绩:";
cin >> num;
}
// Function to determine if the student is excellent
int Isgoodstudent() const {
return num > 90 ? 1 : 0;
}
};
// Derived class for Teacher
class Teacher : public Base {
public:
// Constructor to input number of papers
Teacher() {
cout << "每年发表论文数:";
cin >> num;
}
// Function to determine if the teacher is excellent
int Isgoodteacher() const {
return num > 3 ? 1 : 0;
}
};
第5关:学生-教师
任务描述
本关任务:编写一个输出学生和教师数据的程序,学生数据有编号、姓名、年龄、班号和成绩;教师数据有编号、姓名、年龄、职称和部门。要求声明一个 person 类,并作为学生数据操作类 student 和教师数据操作类 teacher 的基类。
(1)定义Person类
int number1,age;//编号、年龄char name[10];//姓名Person();//默认构造函数Person(int i,int j,const char*p);//带参构造函数void disp();//数据输出
(2)定义Student类
int lesson;//班号char grade;//成绩Student(int i,int j,const char*p, int k,char a);//构造函数void disp();//数据输出
(3)定义Teacher类
char job[10],workplace[30];// 职称、部门Teacher(int i,int j,char*p,const char*a,const char*b);//构造函数void disp();//数据输出
代码
#include <iostream>
#include <cstring>
using namespace std;
// Base class Person
class Person {
protected:
int number1; // 编号
int age; // 年龄
char name[10]; // 姓名
public:
// Default constructor
Person() : number1(0), age(0) {
strcpy(name, ""); // Initialize name to an empty string
}
// Parameterized constructor
Person(int i, int j, const char* p) : number1(i), age(j) {
strncpy(name, p, sizeof(name) - 1);
name[sizeof(name) - 1] = '\0'; // Ensure null termination
}
// Display function
virtual void disp() const {
cout << "It's " << number1 << "," << name << "," << age;
}
};
// Derived class Student
class Student : public Person {
private:
int lesson; // 班号
char grade; // 成绩
public:
// Parameterized constructor
Student(int i, int j, const char* p, int k, char a) : Person(i, j, p), lesson(k), grade(a) {}
// Display function
void disp() const override {
Person::disp(); // Call base class display
cout << "," << lesson << ":" << grade << endl; // Output student-specific data
}
};
// Derived class Teacher
class Teacher : public Person {
private:
char job[10]; // 职称
char workplace[30]; // 部门
public:
// Parameterized constructor
Teacher(int i, int j, const char* p, const char* a, const char* b) : Person(i, j, p) {
strncpy(job, a, sizeof(job) - 1);
job[sizeof(job) - 1] = '\0'; // Ensure null termination
strncpy(workplace, b, sizeof(workplace) - 1);
workplace[sizeof(workplace) - 1] = '\0'; // Ensure null termination
}
// Display function
void disp() const override {
Person::disp(); // Call base class display
//cout << "," << job << ":" << workplace << endl; // Output
cout << "," << workplace << ":" << job << endl; // Output teacher-specific data
}
};
第6关:商品信息
任务描述
本关任务:假设某商店有如下几种货品:衬衣、帽子、立柜。每一种货物都有与其关联的说明信息。 衬衣:单价、产地、库存量、布料; 帽子:单价、产地、库存量、布料、样式(平顶或尖顶); 立柜:单价、产地、库存量、木料、颜色。 对这些商品的操作有:商品的进库(增加库存量),商品的出库(减少库存量),该类货品总价格的计算。要求自行设计数据结构,用类的继承与派生关系将上述的各种货品表示出来,并使用类的构造函数来初始化每一类对象的初始数据。 设立3 个不同的类来描述与处理3 种不同的货品。首先注意到上述3 种货品数据之间的相互关联关系,可使用C++基类及其派生类的定义方法,先抽象出如下每一货品都具有的 “公有”数据构成一个所谓的基类 base,而后再派生出所需的那3 个类。 ①Base(基)类:单价、产地、库存量,; ②由Base 作为基类,派生出Shirt(衬衣)类:增加“布料”数据; ③由Base 出发,派生出Wardrobe(立柜)类:增加“木料”与“颜色”数据; ④而后又由Shirt 类出发(作为基类),派生出Cap(帽子)类:增加“样式”数据。 (2).对应于要对各类数据所进行的操作,而设立出类的如下几个成员函数。 ①构造函数 通过传递来的实参数据,来构造出每一对象所具有的各数据成员。如基类Base 需要传递place、count 与price 三项数据,而派生类Shirt 则需在Base 数据的基础上增加第四项即布料数据material 等。 ②商品的进库(增加库存量) void in_something(int add_cnt); 将对象的库存量 count 增加一个数量 add_cnt。 ③商品的出库(减少库存量) void out_something(int del_cnt); 将对象的库存量count 减少一个数量del_cnt。 ④该类货品总价格的计算 double total_price(); 通过使用“price*count”计算并返回对象所代表货品的总价格。 ⑤对象数据的输出。 在屏幕上显示出对象所拥有的当前数据。
代码
#include<iostream>
#include<string>
using namespace std;
class Base
{
public:
Base(int Count,double Price,string Place)
{
count=Count; //在基类里面对所需要的数据进行定义
price=Price;
place=Place;
}
int in_something(int );
int out_something(int);
double total_price(double );
protected:
int count;
double price;
string place; //使用string来存储一段字符
};
class Shirt:public Base
{
public: //在shirt类里面调用base类中的元素,base类里面不需要写出来数据类型
Shirt(int count,double price,string place,string Material):Base( count, price, place)
{
material=Material;
}
void print1();
protected:
string material;
};
class Wardrobe:public Base
{
public: //在wardrobe类里面调用base类中的数据
Wardrobe(int count,double price,string place,string Colour,string Muliao):Base(count,price,place)
{
colour=Colour;
muliao=Muliao;
}
void print2();
protected:
string colour;
string muliao;
};
class Cap:public Shirt
{
public: //在cap类的里面,调用shirt类的数据,此时material无需再次在cap类里面进行声明
Cap(int count,double price,string place,string Material,string Yangshi):Shirt( count, price, place, Material)
{
yangshi=Yangshi;
}
void print3();
protected:
string yangshi;
};
int Base::out_something(int number)
{
count=count-number;
return count;
}
int Base::in_something(int number)
{
count=count+number;
return count;
}
double Base::total_price(double )
{
return count*price;
}
void Shirt::print1()
{
cout<<"衬衫数据:"<<endl;
cout<<"库存量:"<<count<<" 产地:"<<place<<" 单价:"<<price<<" 布料:"<<material<<" 总价格:"<<count*price<<endl;
}
void Wardrobe::print2()
{
cout<<"立柜数据:"<<endl;
cout<<"库存量:"<<count<<" 产地:"<<place<<" 单价:"<<price<<" 木料: "<<muliao<<" 颜色:"<<colour<<" 总价格:"<<count*price<<endl;
}
void Cap::print3()
{
cout<<"帽子数据:"<<endl;
cout<<"库存量:"<<count<<" 产地:"<<place<<" 单价:"<<price<<" 布料:"<<material<<" 样式:"<<yangshi<<" 总价格:"<<count*price<<endl;
}
第7关:汽车-小车-卡车
任务描述
本关任务:编写一个程序,其中有一个汽车类Vehicle,它具有一个需要传递参数的构造函数,类中的数据成员:车轮个数wheels和车重weight为保护属性;小车类Car是它的私有派生类,其中包含载人 passager_load;卡车类 Truck 是 Vehicle 的私有派生类,其中包含载人数 passager_load 和载重量 payload。每个类都有相关数据的输出方法。
代码
#include<iostream>
using namespace std;
class Vehicle { //声明汽车类
protected:
int wheel;//车轮
double weight;//车重
public:
Vehicle(int w,double i) { //构造函数
wheel = w;
weight = i;
}
void show() { //输出函数
cout<<"车轮个数:"<< wheel;
}
};
class Car:private Vehicle { //声明小车类,私有继承汽车类
private:
int passager_load;//载人数
public:
Car(int w,double i,int p):Vehicle(w,i) { //构造函数
passager_load = p;
}
void display() { //输出函数
cout<<"\n";
Vehicle::show();
cout<<"\t载人数:"<<passager_load<<endl;
}
};
class Truck:private Vehicle { //声明卡车类,私有继承汽车类
private:
int passager_load;//载人数
int payload;//载重量
public:
Truck(int w,double i,int p,int l):Vehicle(w,i) { //构造函数
passager_load = p;
payload = l;
}
void display1() { //输出函数
Vehicle::show();
cout<<"\t载人数:"<<passager_load<<endl;
}
};
9219

被折叠的 条评论
为什么被折叠?



