學習筆記-調用被覆蓋的基類方法

#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

Code-Student:public Person

#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

char str[] = “hello world”;和char *str = “hello world”;區別

char *strA(){char str[] = "hello world";return str;}這程式有什麼問題,該如何修改?解析:這個str裡存在的地址是函數strA棧裡“hello world”的首地址。函數調用完成,棧幀恢複調用strA之前的狀態,臨時空間被重設,堆棧“回縮”,strA棧幀不再屬於應該訪問的範圍。這段程式可以正確輸出結果,但是這種存取方法違背了函數的棧幀機制。      但是只要另外一個函數調用的話,你就會發現,這種方式的不合理及危險性。     

學習筆記-#define * #undef *

#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;}-------

學習筆記-模板函數

#include <iostream>using namespace std;template <typename parameter>parameter max(parameter x,parameter y){if(x>y) {return x; }else {return y; }}int main(){

學習筆記-彙總(組合)

class ElectricMotor{public: ElectricMotor () {}; virtual ~ElectricMotor () {};public: void StartMotor () { Accelerate (); Cruise (); } void StopMotor () { cout << "Motor stopped" << endl;

學習筆記-多態的理解-一個基類指著調用多個衍生類別的虛函數

#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 {

學習筆記-我對多態的理解,歡迎拍磚

代碼:#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

學習筆記-傳遞const指標

在給函數傳遞指標效率很高,但是這樣子做很危險,因為這個函數可能修改了指標指向的對象,這樣子失去了傳遞保護盾意義就好比我們把作品的照片給別人欣賞而不是把作品直接給別人欣賞,要是把作品給了別人欣賞,有可能損壞,我們要是把作品裱起來,用玻璃保護,這樣子既能給真品別人欣賞又不損害作品,這個裱起來就像const指標,從而防止對象被修改.例如#include <iostream>using namespace std;void funA(int* a, const int* b){ b[0

學習筆記-operator()函數,函數對象

#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");

學習筆記-模板類

#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<

學習筆記-隱式調用建構函式

我們平時對於基礎資料型別 (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/本文著作權歸作者和部落格園共有,

學習筆記-隱藏基類的方法

覆蓋任何一個基類的重載方法之後,該方法的其他版本都會被隱藏;如果不希望被隱藏,就繼續覆蓋其他的方法;這個就像建構函式一樣,你自己自訂類建構函式,那麼系統不會再提供任何建構函式.我們可以看看如下例子#include <iostream>using namespace std;class A{public:void fun() const{cout<<"A"<<endl;}void fun(int i) const{cout<<"A+"<<

學習筆記-虛函數工作原理

每一個類都有一個虛函數表v-table,每一個類的對象都有一個指向虛函數表的指標v-pointer.每一個對象的v-pointer都指向v-table,而對於每一個虛函數,v-table都包含一個指向它的指標.建立衍生類別中的基類部分時,v-pointer被初始化為指向v-table的正確部分.當衍生類別的建構函式被調用時添加了對象衍生類別的部分,並調整了v-pointer指標使其指向衍生類別中覆蓋的函數(如果有的話).當使用基類指標時,v-pointer將根據基類指標指向的對象的實際類型指向正

學習筆記-純虛函數

任何包含一個或者多個的純虛函數都叫做抽象類別,因此不能對它執行個體化.否則會導致編譯錯誤;從抽象類別繼承而來的類若不覆蓋純虛函數,依然是純虛函數,所以衍生類別務必覆蓋純虛函數純虛函數定義可以到我這一篇原始碼#include <iostream>using namespace std;#define PI 3.1415926class Shape{public:virtual void Print()=0;virtual float Area()=0;}; class Circle:

引用與指標

文章目錄 指標與引用的區別?

學習筆記-指標數組和數組指標

這個問題相信很多人都談過了,但是我在學習,我有我的看法,我就喜歡寫寫,拍磚隨意指標數組譬如int* p[4];這個應該比較容易理解,這裡[]和*的優先順序是p和[]先結合運算,組成一個數組,然後再用int*去定義,就是一個指標型數組,就正如int p[4]一樣,定義了一個大小為4的整型數組,而int* p[4]則是定義了一個大小為4的指標數組我們可以這樣子理解例如typedef關鍵詞,為現有類型建立一個新的名字;例如typedef int* intStar;intStar p[4];與int*

學習筆記-再談類型轉換

基本類型到類,類到另外一個類已經在本日記討論過了現在再談類到基本類型利用轉化符函數譬如類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<<"

學習筆記-模板函數和模板類

#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;

學習筆記-虛解構函式的好處

如果類中的任何一個函數時虛函數,應該將解構函式變成虛解構函式;原因是如果解構函式不是虛的話,那麼如下例子#include <iostream>using namespace std;class Mammal{public: Mammal():itsAge(1) { } ~Mammal() {cout<<"解構函式"; } virtual void Speak() const { cout << "Mammal speak!\n";

總頁數: 61357 1 .... 9575 9576 9577 9578 9579 .... 61357 Go to: 前往

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.