C++流操作符重载,多文件实现

血红的双手。 提交于 2020-02-07 07:48:31

主文件:main.cpp

#include<iostream>
#include<cstring>
#include"Point.h"
#include"Circle.h"
#include"Cylinder.h"
void main(void) {
	Cylinder cy1(3.5, 6.4, 5.2, 10);
	std::cout << "\noriginal cylinder:\nx=" << cy1.getX() << ",y=" << cy1.getY() << ",r=" <<
		cy1.getRadius() << ",h=" << cy1.getHeight() << "\narea=" << cy1.area()
		<< ",volume=" << cy1.volume() << std::endl;
	cy1.setHeight(15);
	cy1.setRadius(7.5);
	cy1.setPoint(5, 5);
	std::cout << "\nnew cylinder:\n" << cy1 << std::endl;
	Point& pRef = cy1;
	std::cout << "\npRef as a point:" << pRef;
	Circle& cRef = cy1;
	std::cout << "\ncRef as a circle:" << cRef;
	
}

Point头文件:Point.h

#ifndef POINT_H
#define POINT_H
#include<iostream>
class Point
{
public:
	Point(float a, float b);
	void setPoint(float a, float b);
	float getX() const { return x; }
	float  getY()const { return y; }
	friend std::ostream& operator<<(std:: ostream&, Point&);
protected:
	float x, y;
};
#endif // !POINT_H

Point源文件:Point.cpp

#include "Point.h"
#include<iostream>
Point::Point(float a, float b) :x(a), y(b) {}
void Point::setPoint(float a, float b) {
	x = a;
	y = b;
}
std::ostream& operator<<(std::ostream& out, Point& p) {
	out << "[" << p.x << "," << p.y << "]" << std::endl;
	return out;
};

Circle头文件:Circle.h

#pragma once
#include "Point.h"
#include<iostream>
class Circle :
	public Point
{
public:
	Circle(float x, float y, float r);
	void setRadius(float);
	float getRadius() const;
	float area() const;
	friend std::ostream& operator<<(std::ostream&, Circle&);
protected:
	float radius;
};

Circle源文件:Circle.cpp

#include "Circle.h"
#include<iostream>
Circle::Circle(float a,float b,float r):Point(a,b),radius(r){}
float Circle::getRadius ()const { return radius; }
void Circle::setRadius(float r) { radius = r; }
std::ostream& operator<<(std::ostream& out, Circle& c) {

	out << "Center=[" << c.x << "," << c.y << "],r=" << c.radius << ",area=" << c.area() << std::endl;
	return out;
}
float Circle::area()const{
	return 3.14159 * radius * radius;
}

Cylinder头文件:Cylinder.h

#pragma once
#include "Circle.h"
#include<iostream>
class Cylinder :
	public Circle
{
public:
	Cylinder(float x, float y, float r, float h);
	void setHeight(float);
	float getHeight()const;
	float area()const;
	float volume()const;
	friend std::ostream& operator<<(std::ostream&, const Cylinder&);
protected:
	float height;
};

Cylinder源文件:Cylinder.cpp

#include "Cylinder.h"
#include<iostream>
Cylinder::Cylinder(float a,float b,float r,float h):Circle(a,b,r),height(h){}
void Cylinder::setHeight(float h) { height = h; };
float Cylinder::getHeight()const { return height; }
float Cylinder::area()const { return 2 * Circle::area() + 2 * 3.14159 * radius * height; }
float Cylinder::volume()const { return Circle::area() * height; }
std::ostream& operator<<(std::ostream& out, const Cylinder& cy) {

	out << "Center=[" << cy.x << "," << cy.y << "],r=" << cy.radius << ",h=" << cy.height <<
		"\narea=" << cy.area() << ",volume=" << cy.volume() << std::endl;
	return out;
}

Output

original cylinder:
x=3.5,y=6.4,r=5.2,h=10
area=496.623,volume=849.486

new cylinder:
Center=[5,5],r=7.5,h=15
area=1060.29,volume=2650.72


pRef as a point:[5,5]

cRef as a circle:Center=[5,5],r=7.5,area=176.714
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!