随笔分类 - C++
摘要:1 #include "mainwindow.h" 2 #include 3 #include 4 #include 5 #include 6 #include 7 8 using namespace std; 9 10 //异构数据结构,每个数组元素都是基类指针 11 //异构链表,每一个结点都是基类的指针,指向了派生类 12 //基类指针基...
阅读全文
摘要:1 #include 2 using namespace std; 3 4 class Aobject 5 { 6 public: 7 //虚函数提供一个接口,自动选择子类的接口 8 //存储某个对象的地址,调用对应的方法 9 virtual void show() 10 { 11 cout show(); 29 ci...
阅读全文
摘要:1 #include 2 using namespace std; 3 4 class myclass 5 { 6 public: 7 int i = 10; 8 void show() 9 { 10 cout show(); 30 p2->show(); 31 cin.get(); 32 }
阅读全文
摘要:为什么要用虚基类 虚继承以及虚基类 1 #include <iostream> 2 using namespace std; 3 4 //虚基类 5 class A 6 { 7 public: 8 int a; 9 public: 10 A() 11 { 12 cout << "A()" << en
阅读全文
摘要:1 #include 2 using namespace std; 3 4 //构造的时候,从父类到子类构造 5 //子类构造先调用父类构造 6 class myclass 7 { 8 public: 9 int x; 10 int y; 11 12 public: 13 myclass(int a,int b):x(a),y(b) 14 ...
阅读全文
摘要:1 #include 2 #include "coder.h" 3 #include "cppcoder.h" 4 #include "cocoscoder.h" 5 using namespace std; 6 7 void test() 8 { 9 cocoscoder man; 10 } 11 12 //继承,父类不依赖于子类,子类依赖于父类 13 //构造...
阅读全文
摘要:三种访问权限 public:可以被任意实体访问 protected:只允许子类及本类的成员函数访问 private:只允许本类的成员函数访问 三种继承特点 1、public继承不改变基类成员的访问权限 2、private继承使得基类所有成员在子类中的访问权限变为private (基类的private
阅读全文
摘要:1 #include "mainwindow.h" 2 #include 3 #include > 4 5 //重载的三种形式,成员函数重载 6 //友元函数重载,可以使用私有变量以及保护变量 7 //一般函数重载都是公有变量 8 9 class buttons 10 { 11 QPushButton *p; 12 int n; 13 14 public...
阅读全文
摘要:1 #include "mainwindow.h" 2 #include 3 #include > 4 5 //重载的三种形式,成员函数重载 6 //友元函数重载,可以使用私有变量以及保护变量 7 //一般函数重载都是公有变量 8 9 class button 10 { 11 QPushButton *p; 12 int x,y; 13 14 f...
阅读全文
摘要:1 #define _CRT_SECURE_NO_WARNINGS 2 #include 3 #include 4 using namespace std; 5 6 //返回值为类的引用不会调用拷贝构造 7 //拷贝构造要深拷贝,赋值重载(=)也要深拷贝 8 //注意对象如果是在栈上,有生命周期,拷贝栈上的对象需要深拷贝 9 10 class mystring 11 {...
阅读全文
摘要:非指针,则深浅拷贝都一样,含有指针则内存共享,指针一致,内容一直 深拷贝,指针不一致,内存一直,内存是独享的 赋值重载如果有返回自身类型对象,会调用拷贝构造,需要重载拷贝构造,这一点是必须要注意的,原理是:先操作一个类,再把操作的这个类拷贝到本类中,即使返回(*this),也会调用 (如果返回值是自
阅读全文
摘要:shortsafe operator ++() 先自增再引用 shortsafe operator ++(int) 先引用再自增后面,需要副本(先用副本进行操作,然后再进行自己类的++改变数据)(返回的都是修改后的数据) ()可以把对象名当做括号来操作 代码示例
阅读全文
摘要:1 #define _CRT_SECURE_NO_WARNINGS 2 #include 3 using namespace std; 4 5 class box 6 { 7 private: 8 int x; 9 int y; 10 int z; 11 12 13 public: 14 box() :x(10), y(20), ...
阅读全文
摘要:1 #define _CRT_SECURE_NO_WARNINGS 2 #include 3 using namespace std; 4 5 class mystring 6 { 7 public: 8 char *pstr; 9 int length; 10 mystring():pstr(nullptr),length(0) 11 { ...
阅读全文
摘要:1 #include 2 using namespace std; 3 4 class myclass 5 { 6 public: 7 int num; 8 public: 9 explicit myclass(int data) :num(data) 10 { 11 12 } 13 }; 14 15 void main() 16 { 1...
阅读全文
摘要:1 #include 2 using namespace std; 3 4 //友元函数的主要作用就是访问私有变量 5 class myclass 6 { 7 public: 8 friend class newclass; 9 private: 10 int x; 11 int y; 12 int z; 13 14 15 }; 16 ...
阅读全文
摘要:1 #include 2 #include 3 using namespace std; 4 5 //友元函数的主要作用就是访问私有变量 6 class myclass 7 { 8 private: 9 int x; 10 int y; 11 12 public: 13 myclass(int a,int b) 14 { 15 ...
阅读全文
摘要:静态函数没有this指针,无法调用成员变量与成员函数 没有对象名也可以访问,也可以通过对象名访问 静态成员函数经常与静态成员数据配合使用 代码示例
阅读全文
摘要:1 #include 2 #include 3 using namespace std; 4 5 class myclass 6 { 7 public: 8 //static只会初始化一次 9 static int num; 10 myclass() 11 { 12 num += 1; 13 } 14 ~my...
阅读全文

浙公网安备 33010602011771号