标签:span 程序 eal logs 程序设计 space print using com
运算符重载---基本概念
C++程序设计
郭炜 刘家瑛
1 #include<iostream> 2 using namespace std; 3 class Complex{ 4 public: 5 double real; 6 double imaginary; 7 Complex(double a=0.0,double b=0.0) : real(a),imaginary(b) {}//初始化 8 ~Complex(){} 9 void print(); 10 }; 11 Complex operator+(Complex& a,Complex& b)//运算符重载函数(作为普通函数) 12 { 13 return Complex(a.real+b.real,a.imaginary+b.imaginary); 14 } 15 void Complex::print() 16 { 17 cout<<real<<"+"<<imaginary<<"i"<<endl; 18 } 19 int main() 20 { 21 Complex a(3,2),b(5,4),c; 22 c=a+b; 23 c.print(); 24 return 0; 25 }
注:重载为普通函数时,参数个数为运算符数目
标签:span 程序 eal logs 程序设计 space print using com
原文地址:http://www.cnblogs.com/dreamcoding/p/7236902.html