当前位置:   article > 正文

【sy5_继承的应用_1_单继承_Cylinder】_(1)声明 point(点)类,由point 类派生出 circle(圆)类,再由 circle 类

(1)声明 point(点)类,由point 类派生出 circle(圆)类,再由 circle 类派生出cylind
sy5_继承的应用_1_单继承_Cylinder

(1) 声明点类Point,包含成员变量的输入和输出功能,由类Point派生出Circle(圆形)类,实现求圆面积的功能,由Circle(圆形)类派生出Cylinder(圆柱体)类,具有求圆柱体体积和表面积的功能,最后编写main函数使用这些类,重载运算符”<<”和”>>”,使之能用于输出和输入以上类对象。

整段代码:

Point.h

#pragma once
#ifndef POINT_H
#define POINT_H
#include <iostream>
using namespace std;
class Point
{
protected:
	float x, y;
public:
	Point();
	Point(float x, float y);
	friend istream& operator>>(istream& input, Point& P);
	friend ostream& operator<<(ostream & output, const Point& P);

};
#endif // !POINT_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Circle.h

#pragma once
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include "Point.h"
using namespace std;
class Circle:public Point
{
protected:
	float r;
public:
	Circle();
	void shapeName();
	Circle(float x, float y,float r);
	float area()const;
	friend istream& operator>>(istream& input,  Circle& C);
	friend ostream& operator<<(ostream& output, const Circle& C);
};
#endif // !CIRCLE_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Cylinder.h

#pragma once
#ifndef CYLINDER_H
#define CYLINDER_H
#include<iostream>
#include "Circle.h"
using namespace std;
class Cylinder:public Circle
{
protected:
	float h;
public:
	Cylinder();
	Cylinder(float x, float y, float r, float h);
	float area()const;
	float volume()const;
	friend istream& operator>>(istream& input, Cylinder& Cy);
	friend ostream& operator<<(ostream& output, const Cylinder& Cy);
};
#endif // !CYLINDER_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Point.cpp

#include "Point.h"
#include <iostream>
using namespace std;
Point::Point()
{
	this->x = 0;
	this->y = 0;
}
Point::Point(float x, float y)
{
	this->x = x;
	this->y = y;
}
istream& operator>>(istream& input,Point& P)
{
	cout << "请输入点(x,y)的坐标值:";
	input >> P.x >> P.y;
	return input;
}
ostream& operator<<(ostream& output,const Point& P)
{
	output << "坐标点为:"<<"(" << P.x << "," << P.y << ")" << endl;
	return output;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

Circle.cpp

#include"Circle.h"
using namespace std;
Circle::Circle()
{
	this->x = 0;
	this->y = 0;
	this->r = 0;
}
Circle::Circle(float x, float y, float r):Point(x,y)
{
	this->r = r;
}
float Circle::area()const
{
	return 3.14 * r * r;
}
istream& operator>>(istream& input, Circle& C)
{
	cout << "请输入点坐标(x,y):";
	input >> C.x >> C.y ;
	cout << "请输入圆的半径r:";
	input >> C.r;
	return input;
}
ostream& operator<<(ostream& output, const Circle& C)
{
	output << "圆的圆心坐标为:" <<"(" << C.x <<"," << C.y << ")" << endl;
	output << "圆的半径为:" << C.r << endl;
	output << "圆的面积为:" << C.area() << endl;
	return output;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

Cylinder.cpp

#include "Cylinder.h"
Cylinder::Cylinder()
{
	this->x = x;
	this->y = y;
	this->r = r;
	this->h = h;
}
Cylinder::Cylinder(float x, float y, float r, float h)
{
	this->h = h;
}
float Cylinder::area()const
{
	return 2 * Circle::area() + 2 * 3.14 * r * h;
}
float Cylinder::volume()const
{
	return Circle::area() * h;
}
istream& operator>>(istream& input, Cylinder& Cy)
{
	cout << "请输入点坐标(x,y):";
	input >> Cy.x >> Cy.y ;
	cout << "请输入圆的半径r:";
	input >> Cy.r;
	cout << "请输入圆柱体的高h:";
	input >> Cy.h;
	return input;
}
ostream& operator<<(ostream& output, const Cylinder& Cy)
{
	output << "圆柱体的表面积为:" << Cy.area() << endl;
	output << "圆柱体的体积为:" << Cy.volume() << endl;
	return output;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

main.cpp

#include <iostream>
#include "Point.h"
#include "Circle.h"
#include "Cylinder.h"
using namespace std;
int main()
{
	Point P;
	cin >> P;
	cout << P;
	cout << "-------------------------------" << endl;
	Circle C;
	cin >> C;
	cout << C;
	cout << "-------------------------------" << endl;
	Cylinder Cy;
	cin>>Cy;
	cout << Cy;
	return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
运行结果:

在这里插入图片描述

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号