学习BoolanC++笔记_05(C++面向对象高级编程(下)第五周)

作者: ayaoko

出处: http://www.cnblogs.com/fyc006/>

关于作者:小可才疏学浅还请多多赐教!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可邮件(270469391@qq.com)咨询.

对象模型:
关于vptr和vtbl、关于this、关于Dynamic Binding
动态绑定:指针 虚函数 向上转型。
A* pa=new B;
pa->vfunc1();
多态vfunc1:
 
 
扩展虚指针和虚函数表
(转载)C++ 虚函数表解析
这个可增加对虚函数表的理解: (陈皓:http://blog.csdn.net/haoel/article/details/1948051/) 

了解相关使用:

关于 Const

 
关于New,Delete,重载Operator new,Operator delete

 C++ primer 第五版 19.1.1 重载new和delete

 

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 using namespace std;
  5 
  6 /** \brief
  7  * Ayaoko test program week5_0
  8  * \param
  9  * \param
 10  * \return
 11  *
 12  */
 13 
 14 namespace week5_0 //namespace week5_0
 15 {
 16 
 17 class CFruit
 18 {
 19 public:
 20     CFruit()
 21     {
 22         cout<<"构造CFruit基类"<<endl;
 23     };
 24     virtual ~CFruit()
 25     {
 26         cout<<"析构~CFruit基类"<<endl;
 27     };
 28 
 29     void print() {   }
 30     virtual void process() {   }
 31 protected:
 32 
 33 private:
 34     int no;
 35     double weight;
 36     char key;
 37 
 38 };
 39 
 40 class CApple: public CFruit
 41 {
 42 public:
 43     CApple()
 44     {
 45         cout<<"构造CApple派生类"<<endl;
 46     }
 47     ~CApple()
 48     {
 49         cout<<"析构~CApple派生类"<<endl;
 50     }
 51 
 52     void save() {   }
 53     virtual void process() {   }
 54 protected:
 55 
 56 private:
 57     int size;
 58     char type;
 59 };
 60 
 61 
 62 } //namespace week5_0
 63 
 64 /** \brief
 65  * Ayaoko test program week5_1
 66  * \param
 67  * \param
 68  * \return
 69  *
 70  */
 71 
 72 namespace week5_1 //namespace week5_1
 73 {
 74 
 75 class CFruit
 76 {
 77 public:
 78     CFruit()
 79     {
 80         cout<<"构造CFruit基类"<<endl;
 81     };
 82     virtual ~CFruit()
 83     {
 84         cout<<"析构~CFruit基类"<<endl;
 85     };
 86 
 87     void print() {   }
 88     virtual void process() {   }
 89 protected:
 90 
 91 private:
 92     int no;
 93     double weight;
 94     char key;
 95 
 96 };
 97 
 98 class CApple: public CFruit
 99 {
100 public:
101     CApple()
102     {
103         cout<<"构造CApple派生类"<<endl;
104     }
105     ~CApple()
106     {
107         cout<<"析构~CApple派生类"<<endl;
108     }
109 
110     inline void* operator new(size_t _size);
111     inline void  operator delete(void* _pv);
112     inline void* operator new[](size_t _size);
113     inline void  operator delete[](void* _pv);
114 
115 
116     void save() {   }
117     virtual void process() {   }
118 protected:
119 
120 private:
121     int size;
122     char type;
123 };
124 
125 inline void* CApple::operator new(size_t _size)
126 {
127     cout <<"In overloaded new."<<endl;
128     if(void *mem = malloc(_size))
129         return mem;
130     else
131         throw bad_alloc();
132 
133 }
134 
135 inline void  CApple::operator delete(void* _pv)
136 {
137     cout <<"In overloaded delete."<<endl;;
138     if(_pv)
139         free(_pv);
140 }
141 inline void* CApple::operator new[](size_t _size)
142 {
143     cout <<"In overloaded new[]."<<endl;
144     if(void *mem = malloc(_size))
145         return mem;
146     else
147         throw bad_alloc();
148 }
149 inline void  CApple::operator delete[](void* _pv)
150 {
151     cout <<"In overloaded delete[]."<<endl;;
152     if(_pv)
153         free(_pv);
154 
155 }
156 
157 } //namespace week5_1
158 
159 
160 /** \brief
161  *  Ayaoko test program main
162  * \param
163  * \param
164  * \return
165  *
166  */
167 
168 int main()
169 {
170     cout<<"测试1↓↓↓↓↓↓↓↓↓↓↓↓↓↓"<<endl;
171     cout<<"CFruit:------------------"<<endl;
172     week5_0::CFruit* pFruit=new week5_0::CFruit;
173     cout<<"~Fruit:------------------"<<endl;
174     delete pFruit;
175 
176     cout<<"\n测试2↓↓↓↓↓↓↓↓↓↓↓↓↓↓"<<endl;
177     cout<<"CApple:------------------"<<endl;
178     week5_0::CApple* pApple=new week5_0::CApple;
179     cout<<"~CApple:------------------"<<endl;
180     delete pApple;
181 
182     cout<<"\n测试3↓↓↓↓↓↓↓↓↓↓↓↓↓↓"<<endl;
183     cout<<"CApple:------------------"<<endl;
184     week5_1::CApple* pApple1=new week5_1::CApple;
185     cout<<"~CApple:------------------"<<endl;
186     delete pApple1;
187 
188     cout<<"\n测试4↓↓↓↓↓↓↓↓↓↓↓↓↓↓"<<endl;
189     cout<<"CApple:------------------"<<endl;
190     week5_1::CApple* pApple2=new week5_1::CApple[1];
191     cout<<"~CApple:------------------"<<endl;
192     delete[] pApple2;
193 
194     getchar();
195     return 0;
196 }

 

 

 

 

 

 
作者:ayaoko
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

posted on 2017-02-20 06:29  ayaoko  阅读(240)  评论(0编辑  收藏  举报

导航