C++面向对象(二) Complex对象

一、Header(头文件)防卫式声明

complex.h

 ———————————————————————————————————————————
|	#ifndef __COMPLEX__						|
|	#define __COMPLEX__						|
|	 ———————————————————					|
|	|		            |					|
|	|		            |					|
|	| ...               |					|
|	|		            |					|
|	|		            |					|
|	|		            |					|
|	 ———————————————————					|
|	#endif									|
 ———————————————————————————————————————————

上面头文件内部要写入的内容,便是用来处理下面主程序main()函数部分的。

complex-text.cpp

#include <iostream>
#include "complex.h"
using namespace std;

int main(){
   
	complex c1(2,1);
	complex c2;
	cout << c1 << endl;
	cout << c2 << endl;
	
	c2 = c1 + 5;
	c2 = 7 + C1;
	c2 = c1 + c2;
	c2 += c1;
	c2 += 3;
	c2 = -c1;
	
	cout << (c1 == c2) << endl;
	cout << (c1 != c2) << endl;
	cout << conj(c1) << endl;
	return 0;
}

二、Header(头文件)的布局

complex.h

	———————————————————————————————————————————————————————————————————————————————
	|	#ifndef __COMPLEX__		        										  |
	|	#define __COMPLEX__		      											  |
	———————————————————————————————————————————————————————————————————————————————
0	|	#include <cmath>		          			--------------------------	  |
	|												|  forward declarations  |    |
	|	class ostream;             					|	   (前置声明 )	     |	  |
	|	class complex;		              		    --------------------------	  |
	|																			  |
	|   complex& __doapl (complex* ths, const complex& r); 		           		  |
	———————————————————————————————————————————————————————————————————————————————
1	|	class complex{
   	          					------------------------	  |
	|		...										|  class declarations  |      |
	|	};            								|	   (类-声明 )	   |      |
	|			              		   				------------------------	  |
	———————————————————————————————————————————————————————————————————————————————
2	|	complex::function ...	          		    ----------------------		  |
	|												|  class definition  |        |
	|	           									|	  (类-定义 )	 |    	  |
	|			              		   				----------------------		  |
	———————————————————————————————————————————————————————————————————————————————
	|	#endif	          					 									  |
	———————————————————————————————————————————————————————————————————————————————

主要部分是头文件的类声明类定义部分,而前置声明部分主要是为了给类声明类定义部分提供一些前置变量声明的准备。

class declaration & definition

class complex{
   														(class head)
———————————————————————————————————————————————————————————————————————————————
public:																(class body)
	complex(double r = 0, double i = 0): re(r), im(i) {
   }
	complex& operator += (const complex&);
	double real() const{
   
		return re;
	
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值