![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++整理06
崖高人远
这个作者很懒,什么都没留下…
展开
-
运算符的重载,赋值重载,下标重载、对带默认参数的构造函数、析构函数、拷贝构造函数、重载赋值的赋值
//未完成 //运算符的重载,赋值重载,下标重载 //对带默认参数的构造函数、析构函数、拷贝构造函数、重载赋值的赋值 #include using namespace std; //数组类 //以后T类型就是int类型 //这样定义的好处 //如果以后int改成其他数据类型就之用修改这一个地方 //但是typedef的用法却不怎么熟悉,需要复习 typedef int T; class Ar转载 2012-09-27 23:11:06 · 511 阅读 · 0 评论 -
对象类型转换,explicit,显式和隐式
//对象类型转换 //请在运行 #include using namespace std; class A{ }; class B{ public: //重载类型转换 operator A() { cout A" << endl; } B() { } //构造函数进行类型转换 //explicit //默认情况下,当需要类型转换时,构造函数会被隐式调用而实现类型转换,转载 2012-10-07 13:57:49 · 530 阅读 · 0 评论 -
重载+,=,+=
//运算符重载掌握得不够 #include using namespace std; class F { int n; int d; public: F(int n = 0, int d = 1):n(n),d(d) {} friend ostream& operator<<(ostream& o,const F& f) { return o<< f.n << '/' << f.转载 2012-10-07 09:40:56 · 5990 阅读 · 0 评论 -
重载[],explicit
#include using namespace std; //数组类 typedef int T; class Array { T* data; //指向堆中的数据 int sz; //表示数据的个数 int max; //堆中的空间大小 //扩容 void expand() { max = 2 * max; T* t = new T[max]; for(int转载 2012-10-07 09:17:29 · 361 阅读 · 0 评论 -
重载类型转换运算符double,int,string
#include #include using namespace std; class F { int n; int d; public: F(int n = 0, int d = 1):n(n),d(d) { } //不要返回值 //类型转换运算符函数,将当前对象转成double //double operator double() operator转载 2012-10-07 13:40:36 · 1400 阅读 · 0 评论 -
基本数据类型的运算符不能重载,不能创造运算符
// //基本数据类型的运算符不允许被重载 #include using namesapce std; /* int operator+(int,int) { return a - b; } */ //不允许创造运算符 class Integer { int x; public: Integer(int x= 0):x(x) { //不能创造运算符转载 2012-10-07 13:31:01 · 543 阅读 · 0 评论 -
重载*
//重载指针运算符* #include using namespace std; class IntPtr { int * ptr; public: IntPtr(int* ptr = NULL):ptr(ptr) {} int& operator*() { return *ptr; } }; int main() { int x = 100; //以前很少见的直接给对象赋转载 2012-10-07 13:28:49 · 379 阅读 · 0 评论 -
重载赋值运算符详细
#include using namespace std; class Date { int y,m,d; public: //构造 Date(int y =1900,int m = 1,int d =1):y(y),m(m),d(d) { cout << "Date(i,i,i) Constructor," << "this:" << this << endl;转载 2012-10-07 09:47:53 · 423 阅读 · 0 评论 -
重载*,重载 ->
#include using namespace std; struct Date { int y; int m; int d; }; class DatePtr { Date* ptr; public: //如果创建DataPtr对象时使用参数,那么传进去的指针也就成为了DataPtr的成员ptr的值了 DatePtr(Date* ptr = NULL):ptr(ptr)转载 2012-10-07 13:24:37 · 4225 阅读 · 1 评论 -
重载()
#include using namespace std; //加法器 class Add { int x; public: Add(int x = 0):x(x) {} //有疑问 void operator()(int& r) { r += x; } //有疑问 void operator()(){ cout << "^_^,我是括号运算符!" << endl;转载 2012-10-07 12:42:54 · 377 阅读 · 0 评论 -
+、+=、=运算符重载
//运算符重载掌握得不够 #include using namespace std; class F { int n; int d; public: F(int n = 0, int d = 1):n(n),d(d) {} //重载<<,以未化简的分数形式输出 friend ostream& operator<<(ostream& o,const F& f) { return转载 2012-09-28 00:04:21 · 481 阅读 · 1 评论 -
重载前++,后加加(用到了呀元)
#include using namespace std; class Integer { int x; public: Integer(int x = 0):x(x){} friend ostream& operator<< (ostream& o,const Integer& i) { return o<<i.x; } //重载成成员函数,转载 2012-10-07 13:08:24 · 1996 阅读 · 0 评论