赞
踩
(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
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
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
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;
}
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;
}
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;
}
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;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。