VS 多文件避免重定义 ostream不允许在数据声明中使用friend 文件连接 程序设计第三版的十二章第一题 12.1题

系列文章目录



前言

这篇文章是C++程序设计第三版的十二章第一题。之前写程序都是在一个cpp文件里写。导致将一个程序分成几个文件写就行不通了,各种报错。现将本题的完整解答放在优快云上。

一、文件之间衔接的简单讲解

1 文件形式

为要导入的文件分别写成.h .cpp形式,.h里放函数的声明(不要放函数定义),在.cpp里放函数的定义.

2 避免重定义的操作

避免重定义的操作:
#ifndef DUOTAI_400_1_CYLINDER_H
#define DUOTAI_400_1_CYLINDER_H
写在.h文件的前面
文件(大写)H
全部大写

然后在该.h文件的结尾写上#endif

#ifndef  _DUOTAI_400_1_CYLINDER_H_
#define  _DUOTAI_400_1_CYLINDER_H_
#endif

3 导入文件方法

如果要用到其它文件的东西
导入它的.h文件
例:#include"duoTai_400_1_Circle.h"

4 友元函数中ostream报错的解决办法

std::ostream& operator <<(std::ostream& output, Cylinder & c)
在ostream前加std::

二、代码

1.所有文件

代码如下(示例):

在这里插入图片描述

2, Point类

(1)duoTai_400_1_Point.h文件

代码如下:


#include <ostream>
#ifndef  _DUOTAI_400_1_POINT_H_
#define  _DUOTAI_400_1_POINT_H_
class Point;

 class Point {
public:
	Point(float a = 0, float b = 0);
	void setPoint(float, float);
	float getX() const { return x; }
	float getY() const { return y; }
	friend std::ostream& operator <<(std::ostream&, Point&);

protected:
	float x;
	float y;
};


#endif

(2) duoTai_400_1_Point.cpp文件


#include<iostream>
using namespace std;
#include"duoTai_400_1_Point.h"
//using std::cin; 
//using std::cout;
//using std::ostream;
//using std::istream; 
//using std::endl;



Point::Point(float a, float b) {
	x = a;
	y = b;
}

void Point:: setPoint(float a, float b) {
	x = a;
	y = b;
}

ostream& operator <<(ostream& output, Point& p) {

	output << "[" << p.x << "," << p.y << "]" << endl;

	return output;
}

3,Circle类

(1)duoTai_400_1_Point.h文件


#ifndef  _DUOTAI_400_1_CIRCLE_H_
#define  _DUOTAI_400_1_CIRCLE_H_


#include"duoTai_400_1_Point.h"
 class Circle :public Point {

public:
	Circle(float a, float b, float r);
	void setradius(float);

	float getradius() const;
	float area()const;
	friend std::ostream& operator <<(std::ostream&, Circle&);

protected:
	float radius;
};

#endif

(2)duoTai_400_1_Point.cpp文件

#include"duoTai_400_1_Point.h"


#include"duoTai_400_1_Circle.h"

Circle::Circle(float a, float b, float r) :Point(a, b), radius(r) {}


void Circle::setradius(float p) {
		radius=p;
}


float Circle::getradius() const {
	return radius;
}


float Circle:: area()const {
		return (3.14159 * radius * radius);
}

std::ostream& operator << (std::ostream& output, Circle& p) {
	output << "[" << p.x << "," << p.y << "]" << " " << "readius=" << p.radius << " area:" << p.area() << std::endl;
	return output;
}


3, Cylinder类

(1) duoTai_400_1_Cylinder.h文件


#ifndef  _DUOTAI_400_1_CIRCLE_H_
#define  _DUOTAI_400_1_CIRCLE_H_


#include"duoTai_400_1_Point.h"
 class Circle :public Point {

public:
	Circle(float a, float b, float r);
	void setradius(float);

	float getradius() const;
	float area()const;
	friend std::ostream& operator <<(std::ostream&, Circle&);

protected:
	float radius;
};

#endif

(2) duoTai_400_1_Cylinder.cpp文件


#include<iostream>
#include"duoTai_400_1_Point.h"
#include"duoTai_400_1_Circle.h"
#include"duoTai_400_1_Cylinder.h"

using namespace std;

	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() {
		return height;
	}
	float Cylinder::area() {
		return (2 * Circle::area() + 2 * 3.14 * radius);
	}
	float Cylinder:: volume() {
		return (Circle::area() * height);
	}


std::ostream& operator <<(std::ostream& output, Cylinder & c) {
	output << "[" << c.x<< "," << c.y << "]" << " " << "readius=" << c.radius << " area:" << c.area() << "  height:" << c.height << endl;
	return output;
}

4,main()函数

(1)duoTai_400_1文件


#include<iostream>

using namespace std;
#include"duoTai_400_1_Circle.h"
#include"duoTai_400_1_Cylinder.h"
#include"duoTai_400_1_Point.h"

int main() {

	return 0;
}

该处使用的url网络请求的数据。


总结

发现新的问题再更新。对你有用的话,点个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值