标签:
#include "iostream.h"
//1函数重载
int add(int a,int b){
return a+b;
}
float add(float a,float b,float c){
return a+b+c;
}
float add(float a,float b){
return a+b;
}
//2运算符重载
class Complex{//复数类
private:
float a,b;
public:
Complex(){
}
Complex(float a,float b){
this->a=a;
this->b=b;
}
float get_a();
float get_b();
//2运算符重载
Complex operator +(const Complex &y)const;
};
Complex Complex::operator+(const Complex &y)const{
return Complex(a+y.a,b+y.b);//显示调用构造函数
}
float Complex::get_a(){
return a;
}
float Complex::get_b(){
return b;
}
void main(){
int m;
float n;
cin>>m>>n;//标准输入流对象
//标准输出流对象
cout<<"m*10="<<m*10<<"\nn*10="<<n*10<<endl;//endl换行符
cout<<add(1,2)<<endl; //cout<<add(1,3)<<" "<<add(1.2,1.3)<<" "<<add(1.2,2.3,4.3)<<endl会出错原因是对流的符合太多
cout<<add(1.2,3)<<endl;
cout<<add(1,23,4)<<endl;
Complex c1(4,3),c2(3,2),c3;//c++中有抽象函数也可以创造对象
c3=c1+c2;
cout<<c3.get_a()<<endl;
cout<<c3.get_b()<<endl;
//动态内存分配
int* point =new int(4);
cout<<"point="<<*point<<endl;
delete point;
}
标签:
原文地址:http://www.cnblogs.com/wangshuile/p/5595495.html