类与对象练习题(二)---Rectangle/Triangle/Cylinder类

类与对象练习题(二)—Rectangle/Triangle/Cylinder类

在刚开始学习c++的时候刷了很多基础题,这些基础题比较适合初学C++的码友,所以在学完就立即进行了整理,一是为了让初学C++的码友有所参考,二也是为了复习一下所学过知识。
但因为当时在整理时,时间有点紧促,可能会出现一些小错误,于是利用五一假期对之前的文章进行检查,修改了一些小错误,可能有些错误我还没有发现,欢迎码友们对其指正。

以下六道题要用到类与对象中的封装友元函数等基础知识,适合初学类与对象这部分的码友进行练习。

Rectangle类01

定义一个长方形类Rectangle,私有数据成员为double型width、height(表示长方形的宽和高),成员函数包括构造函数Rectangle(用于实现对数据成员width、height的初始化)、成员函数GetArea(计算并返回长方形的面积)。

//main函数如下(不得修改main函数):
int main()
{
       double width,height;
       cin>>width>>height;
       Rectangle r1;
       cout<<r1.GetArea()<<endl;
       Rectangle r2(width,height);
       cout<<r2.GetArea()<<endl;
       return 0;
}

Sample Input
10.2 25.5
Sample Output
0
260.1

#include<iostream>
using namespace std;
class Rectangle
{
public:
    Rectangle(double width = 0, double height = 0);
    double GetArea();
private:
    double m_width;
    double m_height;
};
Rectangle::Rectangle(double width, double height)
{
    m_width = width;
    m_height = height;
}
double Rectangle::GetArea()
{
    return m_width * m_height;
}
int main()
{
    double width, height;
    cin >> width >> height;
    Rectangle r1;
    cout << r1.GetArea() << endl;
    Rectangle r2(width, height);
    cout << r2.GetArea() << endl;
    return 0;
}

Rectangle类02

定义一个长方形类Rectangle,私有数据成员为double型width、height(表示长方形的宽和高),成员函数包括构造函数Rectangle(用于实现对数据成员width、height的初始化,默认宽和高都为10)、成员函数(GetArea计算并返回长方形的面积)、成员函数Expand(用于实现对数据成员width、heigh的值膨胀整数n倍 )。

//main函数如下(不得修改main函数):
int main()
{
       double width,height;
       int n;
       cin>>width>>height;
       cin>>n;
       Rectangle r1;
       cout<<r1.GetArea()<<endl;
       Rectangle r2(width,height);
       cout<<r2.GetArea()<<endl;
       r2.Expand(n);
       cout<<r2.GetArea()<<endl;
       return 0;
}

Sample Input
10.2 25.5 3
Sample Output
100
260.1
2340.9

#include<iostream>
using namespace std;
class Rectangle
{
public:
    Rectangle(double width = 10, double height = 10);
    double GetArea();
    void Expand(int n);
private:
    double m_width;
    double m_height;

};
Rectangle::Rectangle(double width, double height)
{
    m_width = width;
    m_height = height;
}
double Rectangle::GetArea()
{
    return m_width * m_height;
}
void Rectangle::Expand(int n)
{
    m_width = m_width * n;
    m_height = m_height * n;
}
int main()
{
    double width, height;
    int n;
    cin >> width >> height;
    cin >> n;
    Rectangle r1;
    cout << r1.GetArea() << endl;
    Rectangle r2(width, height);
    cout << r2.GetArea() << endl;
    r2.Expand(n);
    cout << r2.GetArea() << endl;
    return 0;
}

Rectangle类03

定义一个长方形类Rectangle,私有数据成员为double型width、height表示长方形的宽和高),成员函数包括构造函数Rectangle(用于实现对数据成员width、height的初始化,默认宽和高都为10)、成员函数GetArea(计算并返回长方形的面积)。
main函数中输入两组宽和高,用于实例化两个类对象R1,R2,判断两个长方形R1、R2面积的大小。

程序执行时:
输入1.5 1.1 3.3 2输出1.65<6.6
输入 3 5 3 5输出15=15
输入3.5 2.0 1.8 1.1 则输出为7>1.98
#include<iostream>
using namespace std;
class Rectangle
{
public:
	Rectangle(double width = 10, double height = 10);
	double GetArea();
private:
	double m_width;
	double m_height;
};
Rectangle::Rectangle(double width, double height)
{
	m_width = width;
	m_height = height;
}
double Rectangle::GetArea()
{
	return m_width * m_height;
}
int main()
{
	double w1, h1, w2, h2;
	cin >> w1 >> h1 >> w2 >> h2;
	Rectangle R1(w1, h1);
	Rectangle R2(w2, h2);
	if (R1.GetArea() == R2.GetArea())
		cout << R1.GetArea() << "=" << R2.GetArea()<< endl;
	else if (R1.GetArea()>R2.GetArea())
		cout << R1.GetArea() << ">" << R2.GetArea() << endl;
	else
		cout << R1.GetArea() << "<" << R2.GetArea() << endl;
	return 0;
}

Triangle类01

定义一个三角形类Triangle,包括三个double型数据成员a、b和c表示三角形的三条边,成员函数包括构造函数、IsTriangle、GetArea。构造函数用于实现对数据成员的初始化;成员函数IsTriangle判断三条边是否构成三角形,成员函数GetArea返回三角形的面积。main函数中输入两组三角形的边长,用这两组边长实例化两个三角形类对象T1,T2,若T1、T2均能构成三角形,则输出两个三角形面积之和,否则输出failure。

输入:3 4 5 6 8 10 输出:30
输入:3 4 5 6 7 8  输出:26.3332
输入:1 2 0 3 4 5  输出:failure

Sample Input
3 4 5 6 7 8
Sample Output
26.3332

#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{
public:
	Triangle(double a, double b, double c);
	bool IsTriangle();
	double GetArea();
private:
	double m_a;
	double m_b;
	double m_c;
};
Triangle::Triangle(double a, double b, double c)
{
	m_a = a;
	m_b = b;
	m_c = c;
}
bool Triangle::IsTriangle()
{
	if (m_a + m_b > m_c && m_b + m_c > m_a && m_c + m_a > m_b)
		return true;
	else
		return false;
}
double Triangle::GetArea()
{
	double s;
	s = (m_a + m_b + m_c) / 2;
	return sqrt(s * (s - m_a) * (s - m_b) * (s - m_c));//海伦公式
}
int main()
{
	double a1,b1, c1, a2, b2, c2;
	cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2;
	Triangle T1(a1, b1, c1);
	Triangle T2(a2, b2, c2);
	if (T1.IsTriangle() && T2.IsTriangle())
		cout << T1.GetArea() + T2.GetArea() << endl;
	else
		cout << "failure" << endl;
	return 0;
}

Triangle类02

定义一个三角形类Triangle,包括三个double型数据成员a、b和c表示三角形的三条边,成员函数包括构造函数、IsTriangle,GetPerimeter。构造函数用于实现对数据成员的初始化;成员函数IsTriangle判断三条边是否构成三角形,成员函数GetPerimeter返回三角形的周长。main函数中输入两组三角形的边长,用这两组边长实例化两个三角形类对象T1,T2,若T1、T2均能构成三角形,则输出两个三角形周长之差,否则输出failure。

输入:3 4 5 6 8 10 输出: -12
输入:3 4 5 6 7 8  输出:-9
输入:1 2 0 3 4 5  输出:failure

Sample Input
3 4 5 6 7 7.5
Sample Output
-8.5

#include<iostream>
using namespace std;
class Triangle
{
public:
	Triangle(double a, double b, double c);
	bool IsTriangle();
	double GetPerimeter();
private:
	double m_a;
	double m_b;
	double m_c;
};
Triangle::Triangle(double a, double b, double c)
{
	m_a = a;
	m_b = b;
	m_c = c;
}
bool Triangle::IsTriangle()
{
	if (m_a + m_b > m_c && m_b + m_c > m_a && m_a + m_c > m_b)
		return true;
	else
		return false;
}
double Triangle::GetPerimeter()
{
	return m_a + m_b + m_c;
}
int main()
{
	double a1, b1, c1, a2, b2, c2;
	cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2;
	Triangle T1(a1, b1, c1);
	Triangle T2(a2, b2, c2);
	if (T1.IsTriangle() && T2.IsTriangle())
		cout << T1.GetPerimeter() - T2.GetPerimeter() << endl;
	else
		cout << "failure" << endl;
	return 0;
}

Cylinder类

定义一个圆柱类Cylinder,成员数据为底圆半径r和圆柱高h,成员函数为构造函数,GetArea计算圆柱体的表面积,GetVolume计算圆柱体的体积。(注意π值取3.14)
main函数输入圆柱的半径和高,定义一个类对象,计算并输出圆柱的表面积和体积。
Sample Input
1.5 1.5
Sample Output
28.26 10.5975

#include<iostream>
using namespace std;
const double PI = 3.14;
class Cylinder
{
public:
	Cylinder(double r, double h);
	double GetArea();
	double GetVolume();
private:
	double m_r;
	double m_h;
};
Cylinder::Cylinder(double r, double h)
{
	m_r = r;
	m_h = h;
}
double Cylinder::GetArea()
{
	return 2 * PI * m_r * m_r + 2 * PI * m_r * m_h;
}
double Cylinder::GetVolume()
{
	return  PI * m_r * m_r * m_h;
}
int main()
{
	double r, h;
	cin >> r >> h;
	Cylinder C1(r, h);
	cout << C1.GetArea() << " " << C1.GetVolume() << endl;
	return 0;
}

大家好,我是Lucky_追梦仔。一个正在学习编程的小白,希望我的博文,可以帮助到您学习,或者解决您遇到的问题。

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是实现要求的完整代码: ```cpp #include <iostream> #include <vector> #include <sstream> #include <fstream> #include <cmath> using namespace std; class Shape { public: virtual double area() = 0; virtual double perimeter() = 0; virtual void serialize(ostream& os) = 0; virtual void deserialize(istream& is) = 0; virtual ~Shape() {} }; class Triangle : public Shape { public: Triangle(double a, double b, double c) { this->a = a; this->b = b; this->c = c; } double area() override { double p = (a + b + c) / 2; return sqrt(p * (p - a) * (p - b) * (p - c)); } double perimeter() override { return a + b + c; } void serialize(ostream& os) override { os << "0 " << a << " " << b << " " << c << endl; } void deserialize(istream& is) override { is >> a >> b >> c; } private: double a, b, c; }; class Rectangle : public Shape { public: Rectangle(double w, double h) { this->w = w; this->h = h; } double area() override { return w * h; } double perimeter() override { return 2 * (w + h); } void serialize(ostream& os) override { os << "1 " << w << " " << h << endl; } void deserialize(istream& is) override { is >> w >> h; } private: double w, h; }; class Square : public Rectangle { public: Square(double a) : Rectangle(a, a) {} void serialize(ostream& os) override { os << "2 " << w << endl; } void deserialize(istream& is) override { is >> w; h = w; } }; class Circle : public Shape { public: Circle(double r) { this->r = r; } double area() override { return M_PI * r * r; } double perimeter() override { return 2 * M_PI * r; } void serialize(ostream& os) override { os << "3 " << r << endl; } void deserialize(istream& is) override { is >> r; } private: double r; }; class Ellipse : public Shape { public: Ellipse(double a, double b) { this->a = a; this->b = b; } double area() override { return M_PI * a * b; } double perimeter() override { return 2 * M_PI * sqrt((a * a + b * b) / 2); } void serialize(ostream& os) override { os << "4 " << a << " " << b << endl; } void deserialize(istream& is) override { is >> a >> b; } private: double a, b; }; vector<Shape*> readShapesFromFile(const char* filename) { vector<Shape*> shapes; ifstream infile(filename); string line; while (getline(infile, line)) { stringstream ss(line); int type; ss >> type; Shape* shape; if (type == 0) { double a, b, c; ss >> a >> b >> c; shape = new Triangle(a, b, c); } else if (type == 1) { double w, h; ss >> w >> h; shape = new Rectangle(w, h); } else if (type == 2) { double a; ss >> a; shape = new Square(a); } else if (type == 3) { double r; ss >> r; shape = new Circle(r); } else if (type == 4) { double a, b; ss >> a >> b; shape = new Ellipse(a, b); } else { continue; } shapes.push_back(shape); } return shapes; } int main() { vector<Shape*> shapes = readShapesFromFile("shapes.txt"); for (Shape* shape : shapes) { cout << "Area: " << shape->area() << endl; cout << "Perimeter: " << shape->perimeter() << endl; shape->serialize(cout); delete shape; } return 0; } ``` 其中,`Shape` 是所有形状的父类,包含了三个纯虚函数分别计算面积、周长和序列化输出。`Triangle`、`Rectangle`、`Square`、`Circle` 和 `Ellipse` 是不同形状的子类,实现了对应的计算和序列化方法。`Rectangle` 和 `Ellipse` 分别有 `Square` 和 `Circle` 的子类。`readShapesFromFile` 函数用来从文件中读取形状数据,创建对应的对象并返回。在 `main` 函数中,我们通过 `readShapesFromFile` 函数读取形状数据,计算并输出对应的面积、周长和序列化结果,最后销毁对象

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值