C++编程考题:
Date: November 3, 2022
- Concise summary:期末复习的一些C++编程题
第四章:
第二次上机第一题
设计一个长方体类Box,它能计算并输出长方体的体积和表面积。
(1) 定义构造函数完成长方体的初始化
(2) 求长方体的体积;
(3) 求长方体的表面积;
(4) 输出长方体的相关信息。
请写出完整的程序(可以复制粘贴整个程序作为答案,也可以上传截图和运行结果)。
Code:完成
#include<iostream>
using namespace std;
class Box {
public:
//注意:这里如果有参构造函数包含了默认值,就不需要再补默认构造函数了,否则会造成多个默认构造函数的情况,会报错
Box(int l = 1, int w = 1, int h = 1) {
this->l = l;
this->w = w;
this->h = h;
}
double v() {
double v;
v = l * w * h;
return v;
}
double s() {
double s;
s = 2 * (1 * w + 1 * h + w * h);
return s;
}
void inf() {
cout << "长方体的长宽高分别是: " << l << ", " << w << ", " << h << endl;
cout << "长方体的体积和表面积分别是:" << v() << ", " << s() << endl;
}
private:
double l, w, h;
};
int main() {
Box b(1, 2, 3);
b.inf();
return 0;
}
第二次上机第二题
需要求3个三角形的面积,请编写一个面向对象的程序。数据成员包括三角形的三边长a、b、 c。要求用成员函数实现以下功能:
(1) 定义构造函数完成三角形的初始化;
(2) 求三角形的周长;
(3) 求三角形的面积;
(4) 输出三角形信息。
Code:
#include<iostream>
#include<math.h>
using namespace std;
class triangle {
public:
triangle(double a = 3, double b = 4, double c = 5) {
this->a = a;
this->b = b;
this->c = c;
}
double perimeter() {
double p = a + b + c;
return p;
}
double area() {
double p1 = 0.5 * (a + b + c);
double s = sqrt(p1 * (p1 - a) * (p1 - b) * (p1 - c));
return s;
}
void inf() {
cout << "三角形的三条边长分别为:" << a << ", "<< b << ", "<< c << endl;
cout << "三角形的周长和面积分别为:" << perimeter() << " ," << area() << endl;
}
private:
double a, b, c;
};
int main() {
triangle t0, t1(6, 8, 10), t2(5, 12, 13);
t0.inf();
t1.inf();
t2.inf();
return 0;
}
第六章:
第五次上机作业一:
先定义“高度”类Hight和“圆”类Circle,再由Hight和Circle多重派生出“圆柱体”类Cylinder。在主函数中定义一个圆柱体对象,调用成员函数求出圆柱体的体积和表面积。
(类中的数据成员不能定义为public。)
int main( ){
Cylinder s(5,3); //5是圆柱体底圆半径,3是圆柱体高度
s.tiji(); //此行输出圆柱体体积
s.biaomianji(); //此行输出圆柱体表面积
return 0;
}
请写出完整的程序。
Code:
#include<iostream>
using namespace std;
class Height {
public:
Height(double h = 1) {
this->h = h;
}
void setH(double h) {
this->h = h;
}
double getH() {
return h;
}
private:
double h;
};
class Circle{
public:
Circle(double r = 1) {
this->r = r;
}
void setR(double r) {
this->r = r;
}
double getR(){
return r;
}
private:
double r;
};
class Cylinder :public Height, public Circle {
public:
Cylinder(double r, double h): Circle(r), Height(h){}
double tiji() {
double V;
V = 3.14 * getR() * getR() * getH();
cout << "圆柱体的体积是:" << V << endl;
return V;
}
double biaomianji() {
double S;
S = 2 * 3.14 * getR() * getR() + 2 * 3.14 * getR() * getH();
cout << "圆柱体的表面积是:" << S << endl;
return S;
}
};
int main(){
Cylinder s(5, 3); //5是圆柱体底圆半径,3是圆柱体高度
s.tiji(); //此行输出圆柱体体积
s.biaomianji(); //此行输出圆柱体表面积
return 0;
}
第五次上机作业二:
用先定义“高度”类Hight和“长方形”类Rectangle,再由Hight和Rectangle多重派生出“长方体”类Cuboid。在主函数中定义一个长方体对象,调用成员函数求出长方体的体积和表面积。
(类中的数据成员不能定义为public。)
int main(){
Cuboid c(2,5,3); //2、5、3分别表示长方体的长、宽和高
c.tiji( ); //此行输出长方体体体积
c.biaomianji( ); //此行输出长方体表面积
return 0;
}
请写出完整的程序。
Code:
#include<iostream>
using namespace std;
class Height {
public:
Height(float h = 1) {
this->h = h;
}
float getH() {
return h;
}
private:
float h;
};
class Rectangle {
public:
Rectangle(float l = 1, float w = 1) {
this->l = l;
this->w = w;
}
float getL() {
return l;
}
float getW() {
return w;
}
private:
float l;
float w;
};
class Cuboid :public Height, public Rectangle {
public:
Cuboid(float h, float l, float w) :Height(h), Rectangle(l, w) {}
void tiji() {
float L, W, H, V;
L = getL();
W = getW();
H = getH();
V = L * W * H;
cout << "体积为" << V << endl;
}
void biaomianji() {
float L, W, H, S;
L = getL();
W = getW();
H = getH();
S = 2 * (L * W + L * H + W * H);
cout << "表面积为" << S << endl;
}
};
int main() {
Cuboid c(2, 5, 3); //2、5、3分别表示长方体的长、宽和高
c.tiji(); //此行输出长方体体体积
c.biaomianji(); //此行输出长方体表面积
return 0;
}
第五次上机作业三:
先定义的“高度”类Hight和“圆”类Circle,再由Hight和Circle多重派生出“圆锥体”类Cone。在主函数中定义一个圆锥体类的对象,调用成员函数求出圆锥体的的体积和底圆面积。
(类中的数据成员不能定义为public。)
int main( ){
Cone s(5,3);
s.tiji( );//此行输出圆锥体的体积
s.diyuanmianji( );//此行输出圆锥体的底圆面积
return 0;
}
请写出完整的程序。
Code:
#include<iostream>
using namespace std;
class Height {
public:
Height(double h = 1) {
this->h = h;
}
double getH() {
return h;
}
private:
double h;
};
class Circle {
public:
Circle(double r = 1) {
this->r = r;
}
double getR() {
return r;
}
private:
double r;
};
class Cone :public Height, public Circle {
public:
Cone(double x, double y) :Height(x), Circle(y) {}
void tiji() {
double R, H, V;
R = getR();
H = getH();
V = (3.14 * R * R * H) / 3;
cout << "体积为" << V << endl;
}
void biaomianji() {
double R, S;
R = getR();
S = 0.5 * (2 * 3.14 * R) * R;
cout << "表面积为" << S << endl;
}
};
int main() {
Cone c(1, 1);
c.tiji(); //此行输圆锥体体积
c.biaomianji(); //此行输出底圆面积
return 0;
}
第五次上机作业四:
先定义一个点类Point,Point类有两个数据成员代表横坐标和纵坐标,再由点类Point派生出线段类Line,在主函数中定义一个线段类对象,调用成员函数求出改线段的长度和其中点坐标。
(类中的数据成员不能定义为public。)
int main(){
Line s(0,0,0,4); //两个点坐标分别是(0,0)(0,4)
s.distance( ); //此行输出线段s的长度。
s.ZDdata( ); //此行输出线段s的中点坐标。
return 0;
}
请写出完整的程序。
Code:
#include<iostream>
#include<math.h>
using namespace std;
class Point {
public:
Point(double x = 0, double y = 0) {
this->x = x;
this->y = y;
}
double getX() {
return x;
}
double getY() {
return y;
}
private:
double x;
double y;
};
class Line :public Point {
public:
Line(double x1, double y1, double x2, double y2) :p1(x1, y1), p2(x2, y2) {}
void distance() {
double l = sqrt((p1.getX() - p2.getX()) * (p1.getX() - p2.getX()) + (p1.getY() - p2.getY()) * (p1.getY() - p2.getY()));
cout << "线段长度为:" << l << endl;
}
void ZDdata() {
double x3 = 0.5 * (p1.getX() + p2.getX());
double y3 = 0.5 * (p1.getY() + p2.getY());
cout << "线段中点为:(" << x3 << ", " << y3 << ")" << endl;
}
private:
Point p1, p2;
};
int main() {
Line s(0, 0, 0, 4); //两个点坐标分别是(0,0)(0,4)
s.distance(); //此行输出线段s的长度。
s.ZDdata(); //此行输出线段s的中点坐标。
return 0;
}