初始化列表 类名::类名(形参1,形参2,...形参n):数据成员1(形参1),数据成员2(形参2),...,数据成员n(形参n) { ... } 规则1,初始化列表进行数据成员的初始化 规则2,初始化列表进行父类的初始化 #include <iostream> using namespace std; class A { private: int i; public: A(int ii) :i(ii)//初始化列表 { std::cout << "A::A()"…
赋值兼容规则是指在公有派生情况下,一个派生类的对象可以作为基类的对象来使用的情况. 约定类derived是从类base公有派生而来的,则指如下3种情况: (1)派生的对象可以赋给基类的对象.例如: derived d; base b; b=d; (2)派生类的对象可以初始化基类的引用.例如: derived d; base& br=d; (3)派生类的对象的地址可以赋给指向基类的指针.例如: derived d; base *pb=&d; 把子类的对象交给父类的指针或引用就成了向上造型.…
对象组合,就是一个类的对象作为另外一个类的成员,涉及类的对象,对象是实体,玩实 继承,涉及类,类是概念,玩虚 public: 所有人都可以接触 private: 数据放private protected: 留给子类…
区分初始化,赋值 #include <iostream> using namespace std; class Fi { public: Fi() {}//1构造函数 }; class Fee { public: Fee(int) {}//2构造函数 Fee(const Fi&) {}//3构造函数 }; void main() { Fee fee = ;//2构造函数 Fi fi;//1构造函数 Fee fum = fi;//3构造函数 fum = fi;//赋值 system(&q…
Static in C++ Two basic meanings Static Storage --allocated once at a fixed address Visibility of a name --internal linkage Don't use static except inside functions and classes. Uses of "static" in C++ Static free functions----deprecated弃用 Stati…
一旦写了一个类,给它3个函数: 1default construtor 2virtual destructor 3copy constructor Constructions vs. assignment Every object is constructed once Every object should be destroyed once --Failure to invoke delete() --invoke delete() more than once Once an object…
所有带virtual的类的对象,里面最上面有一个隐藏的指针vptr,指向一张表vtable #include <iostream> using namespace std; class A { public: A() :i() {} virtual void f() { std::cout << "A::f()" << std::endl; } int i; }; void main() { A a, b; a.f(); std::cout <…
数据类型 & 别名=对象名; #include <iostream> using namespace std; int * f(int * x) { (*x)++; return x; } int & g(int & x) { x++; return x; } int x; int & h() { int q;//!return q return x; } void main() { ; std::cout << a << std::en…
error C2131: 表达式的计算结果不是常数 #include <iostream> using namespace std; void main() { ; int finalGrade[class_size]; ; int arr[a];//error C2131: 表达式的计算结果不是常数 int x; std::cin >> x; const int size = x; double classAverage[size];//error C2131: 表达式的计算结果…
使用inline说明的函数称内联函数. 在C++中,除具有循环语句.switch语句的函数不能说明为内联函数外,其他函数都可以说明为内联函数. #include <iostream> using namespace std; inline int f(int i) { ; } void main() { ); int b = f(a); std::cout << a << " " << b << std::endl; syst…