Time of Update: 2018-12-06
#include <iostream>#include <cstring>using namespace std;class A{public:void fun() {cout<<"A"<<endl;}void fun(int i) {cout<<"A+"<<i<<endl;}};class B:public A{public:void
Time of Update: 2018-12-06
#include <iostream>#include <cstring>using namespace std;class Person{char *name;int age;char *address;public: Person(); Person(char *name,char *address,int age); ~Person();void introduce();};class Student: public Person{char
Time of Update: 2018-12-06
char *strA(){char str[] = "hello world";return str;}這程式有什麼問題,該如何修改?解析:這個str裡存在的地址是函數strA棧裡“hello world”的首地址。函數調用完成,棧幀恢複調用strA之前的狀態,臨時空間被重設,堆棧“回縮”,strA棧幀不再屬於應該訪問的範圍。這段程式可以正確輸出結果,但是這種存取方法違背了函數的棧幀機制。 但是只要另外一個函數調用的話,你就會發現,這種方式的不合理及危險性。
Time of Update: 2018-12-06
#include <iostream>using namespace std;#define PI 3.14void display1() { cout<<"PI="<<PI;}#undef PIvoid display2() { cout<<"PI="<<PI;}int main(){ display1(); display2();int n; cin>>n;return 0;}-------
Time of Update: 2018-12-06
#include <iostream>using namespace std;template <typename parameter>parameter max(parameter x,parameter y){if(x>y) {return x; }else {return y; }}int main(){
Time of Update: 2018-12-06
class ElectricMotor{public: ElectricMotor () {}; virtual ~ElectricMotor () {};public: void StartMotor () { Accelerate (); Cruise (); } void StopMotor () { cout << "Motor stopped" << endl;
Time of Update: 2018-12-06
#include <iostream>using namespace std;class Mammal{public: Mammal():itsAge(1) { }virtual ~Mammal() { }virtual void Speak() const { cout << "Mammal speak!\n"; }protected:int itsAge;};class Dog : public Mammal{public:void Speak()const {
Time of Update: 2018-12-06
代碼:#include <iostream>using namespace std;#define PI 3.14159class Shape{public:virtual void Print()=0;virtual float Area()=0;}; class Circle: public Shape{float R;public: Circle(float R);void Print();float Area();};Circle::Circle(float
Time of Update: 2018-12-06
在給函數傳遞指標效率很高,但是這樣子做很危險,因為這個函數可能修改了指標指向的對象,這樣子失去了傳遞保護盾意義就好比我們把作品的照片給別人欣賞而不是把作品直接給別人欣賞,要是把作品給了別人欣賞,有可能損壞,我們要是把作品裱起來,用玻璃保護,這樣子既能給真品別人欣賞又不損害作品,這個裱起來就像const指標,從而防止對象被修改.例如#include <iostream>using namespace std;void funA(int* a, const int* b){ b[0
Time of Update: 2018-12-06
#include <iostream>#include <string>using namespace std;class Display{public:void operator()(string strIn) const { cout<<strIn<<endl; }};int main(){ Display display; display.operator()("display");
Time of Update: 2018-12-06
#include <iostream>using namespace std;template <typename parameter>class A{private: parameter value;public:void setData(const parameter value) {this->value=value; }void display() const { cout<
Time of Update: 2018-12-06
我們平時對於基礎資料型別 (Elementary Data Type),都是這樣子寫的int n=1;float f = 1.2;double d = 1.0212545565;看起來沒有調用建構函式啊,其實它已經隱式調用了顯示調用建構函式如下int n=int(1);float f = float(1.2);double d = double(1.0212545565);作者: 林羽飛揚出處:http://www.cnblogs.com/zhengyuhong/本文著作權歸作者和部落格園共有,
Time of Update: 2018-12-06
覆蓋任何一個基類的重載方法之後,該方法的其他版本都會被隱藏;如果不希望被隱藏,就繼續覆蓋其他的方法;這個就像建構函式一樣,你自己自訂類建構函式,那麼系統不會再提供任何建構函式.我們可以看看如下例子#include <iostream>using namespace std;class A{public:void fun() const{cout<<"A"<<endl;}void fun(int i) const{cout<<"A+"<<
Time of Update: 2018-12-06
每一個類都有一個虛函數表v-table,每一個類的對象都有一個指向虛函數表的指標v-pointer.每一個對象的v-pointer都指向v-table,而對於每一個虛函數,v-table都包含一個指向它的指標.建立衍生類別中的基類部分時,v-pointer被初始化為指向v-table的正確部分.當衍生類別的建構函式被調用時添加了對象衍生類別的部分,並調整了v-pointer指標使其指向衍生類別中覆蓋的函數(如果有的話).當使用基類指標時,v-pointer將根據基類指標指向的對象的實際類型指向正
Time of Update: 2018-12-06
任何包含一個或者多個的純虛函數都叫做抽象類別,因此不能對它執行個體化.否則會導致編譯錯誤;從抽象類別繼承而來的類若不覆蓋純虛函數,依然是純虛函數,所以衍生類別務必覆蓋純虛函數純虛函數定義可以到我這一篇原始碼#include <iostream>using namespace std;#define PI 3.1415926class Shape{public:virtual void Print()=0;virtual float Area()=0;}; class Circle:
Time of Update: 2018-12-06
文章目錄 指標與引用的區別?
Time of Update: 2018-12-06
這個問題相信很多人都談過了,但是我在學習,我有我的看法,我就喜歡寫寫,拍磚隨意指標數組譬如int* p[4];這個應該比較容易理解,這裡[]和*的優先順序是p和[]先結合運算,組成一個數組,然後再用int*去定義,就是一個指標型數組,就正如int p[4]一樣,定義了一個大小為4的整型數組,而int* p[4]則是定義了一個大小為4的指標數組我們可以這樣子理解例如typedef關鍵詞,為現有類型建立一個新的名字;例如typedef int* intStar;intStar p[4];與int*
Time of Update: 2018-12-06
基本類型到類,類到另外一個類已經在本日記討論過了現在再談類到基本類型利用轉化符函數譬如類A轉換到整型int#include <iostream>using namespace std;class A{public:int i; A() { } A(int k) { i=k; }operator int() {int t = i;return t; }void show() { cout<<"
Time of Update: 2018-12-06
#include <iostream>using namespace std;template <typename Type>Type max(Type x,Type y){if(x>y) {return x; }else {return y; }}template <typename parameter1=int ,typename parameter2=int>class A{ parameter1 x;
Time of Update: 2018-12-06
如果類中的任何一個函數時虛函數,應該將解構函式變成虛解構函式;原因是如果解構函式不是虛的話,那麼如下例子#include <iostream>using namespace std;class Mammal{public: Mammal():itsAge(1) { } ~Mammal() {cout<<"解構函式"; } virtual void Speak() const { cout << "Mammal speak!\n";