C++特性
2015-09-09 22:34
369 查看
#include <iostream> using namespace std; class A { public: int _a; A() { _a = 1; } void print() { printf("%d\n", _a); } }; class B: public A { public: int _a; B() { _a = 2; } }; int main() { B b; b.print(); printf("%d\n", b._a); return 0; } |
1 2 | 1 2 |
2.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <iostream> using namespace std; class base { private: int m_i; int m_j; public: base(int i):m_j(i),m_i(m_j){} base():m_j(0),m_i(m_j){} int get_i(){return m_i;} int get_j(){return m_j;} }; int main() { base obj(98); cout<<obj.get_i()<<endl <<obj.get_j()<<endl; return 0; } |
1 2 | -858993460 98 |
3.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #include <iostream> using namespace std; class B { private: int data; public: B() { cout<<"default constructor"<<endl; } ~B() { cout<<"destructed"<<endl; } B(int i):data(i) { cout<<"constructor by parameter"<<data<<endl; } }; B Play(B b) { return b; } int main() { B temp = Play(5); return 0; } |
1 2 3 | constructor by parameter5 destructed destructed |
4.
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <iostream> using namespace std; class CA { public: void f() { cout << "CA f()" << endl; } virtual void ff() { cout << "CA ff()" << endl; f(); } }; class CB : public CA { public : virtual void f() { cout << "CB f()" << endl; } void ff() { cout << "CB ff()" << endl; f(); CA::ff(); } }; class CC : public CB { public: virtual void f() { cout << "C f()" << endl; } }; int main() { CB b; CA *ap = &b; CC c; CB &br = c; CB *bp = &c; ap->f(); cout << endl; b.f(); cout << endl; br.f(); cout << endl; bp->f(); cout << endl; ap->ff(); cout << endl; bp->ff(); cout << endl; return 0; } |
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 | CA f() CB f() C f() C f() CB ff() CB f() CA ff() CA f() CB ff() C f() CA ff() CA f() |
相关文章推荐
- C/C++基本数据类型的存储空间长度及取值范围
- C++ Primer Plus (第6版)笔记心的---处理数据
- c++ Primer 第13章 拷贝控制
- C++学习心得
- C++ <使用函数运算符重载执行类对象加法>
- VS2010 C++ 学习笔记(七)virtual 虚函数
- C++ 华为 合唱队
- C++11中int,float,double与string的转化
- C++ Pirmer : 第十四章 : 重载运算符与类型转换之函数调用运算符与标准库的定义的函数对象
- JetBrains公司的 号称C++神器的 Clion
- C++用法的学习心得
- c++学习心得
- C++用法的学习心得
- 【leetcode】Single Number III --C++
- Effective C++ 条款34 区分接口继承和实现继承
- c++ stl 之迭代器浅析
- c++ 字符串输出限制突破
- C++用法的学习心得
- 欢迎使用CSDN-markdown编辑器
- C++用法的学习心得