c++学习2011-03-07
来源:互联网 发布:最小二乘法矩阵推导 编辑:程序博客网 时间:2024/06/06 00:50
背景:
对象初始化
源代码:
#include <iostream>
using namespace std;
class complex //定义一个复数
{
private:
float real; //实部
float imag; //虚部
public:
complex(float r ,float i )
{
real = r;
imag = i;
}
complex operator + (complex c2); //重载
complex operator - (complex c2); //重载
void display();
};
complex complex::operator + (complex c2)
{
return complex(real + c2.real,imag + c2.imag);
}
complex complex::operator - (complex c2)
{
return complex(real - c2.real,imag - c2.imag);
}
void complex::display()
{
cout << real << "," << imag << endl;
}
int main()
{
complex c1(5.0,4.0),c2(2.0,10.0),c3; //c3没有初始化
//正确初始化complex c1(5.0,4.0),c2(2.0,10.0),c3(0.0,0.0);
c3 = c1 - c2;
c3.display();
c3 = c1 + c2;
c3.display();
}
错误提示:
error: no matching function for call to 'complex::complex()'
错误分析:
没有初始化话对象
研究结果(知识扩展):
构造函数的作用就是在对象被创建时利用特定的值构造对象,将对象初始化为一个特定的状态
- C语言学习03
- C语言学习日记07
- C学习
- C++学习
- c ++ 学习
- 学习C
- c学习
- 学习C#!
- 学习C
- c学习
- c学习
- 学习C
- 学习C
- 学习C++
- C学习
- c学习
- 【C/C++】学习导论
- C/C++____vector___学习
- 请求转发与重定向的区别
- [二级C语言]上机题库、模拟软件、Visual C++ 6.0、复习资料
- 关于lua和python的垃圾回收的看法
- ARM Linux异常处理之data abort(一)
- 【STM32 .Net MF开发板学习-28】中文显示(WPF方式)
- c++学习2011-03-07
- 怎样让CFileDialog打开的对话框只显示并返回目录
- HTML中Meta之详解
- 1007 DNA Sorting
- java获取网卡物理地址
- Google网站架构分析
- INPUT SYSTEM
- KB-Oracle 9i NVarchar求生守則 之Delphi 解決方法
- GNOME 3 要去掉窗口最小化按钮