c++類的函式過載、重寫示例
阿新 • • 發佈:2018-12-30
1、檔案cls.h
標頭檔案cls.h實現了基類Base1、Base2,和子類Derived1、Derived2。
#pragma once
#include <iostream>
using namespace std;
class Base1
{
public:
Base1() { cout << "Constrution Base1" << endl; }
//~Base1() { cout << "Destrution Base1" << endl; }
virtual ~Base1() { cout << "Destrution Base1" << endl; }
virtual void f() { cout << "Base1::f" << endl; }
virtual void g() { cout << "Base1::g" << endl; }
virtual void h() { cout << "Base1::h" << endl; }
void func(){ cout << "Base1::func" << endl; }
};
class Base2
{
public:
Base2() { cout << "Constrution Base2" << endl; }
//~Base2() { cout << "Destrution Base2" << endl; }
virtual ~Base2() { cout << "Destrution Base2" << endl; }
virtual void f() { cout << "Base2::f" << endl; }
virtual void g() { cout << "Base2::g" << endl; }
virtual void h() { cout << "Base2::h" << endl; }
void func() { }
};
class Derived1 :public Base1
{
public:
Derived1() { cout << "Constrution Derived1" << endl; }
~Derived1() { cout << "Destrution Derived1" << endl; }
virtual void f() { cout << "Derived1::f" << endl; } // virtual可寫可不寫
virtual void g1() { cout << "Derived1::g1" << endl; }
virtual void h1() { cout << "Derived1::h1" << endl; }
void func() { cout << "Derived1::func" << endl; }
};
class Derived2 :public Base2, public Base1
{
public:
Derived2() { cout << "Constrution Derived2" << endl; }
~Derived2() { cout << "Destrution Derived2" << endl; }
virtual void f() { cout << "Derived2::f" << endl; }
virtual void g1() { cout << "Derived2::g1" << endl; }
virtual void h1() { cout << "Derived2::h1" << endl; }
};
2、主函式
測試函式的派生、虛擬函式重寫、過載等示例。
#include "cls.h"
int main()
{
Base1 b1;
Base2 b2;
Derived1 d1;
Derived2 d2;
// 大小與函式列表有關
cout << "sizeof(Base1) = " << sizeof(Base1) << " sizeof(b1) = " << sizeof(b1) << endl;
cout << "sizeof(Base2) = " << sizeof(Base2) << " sizeof(b2) = " << sizeof(b2) << endl;
cout << "sizeof(Derived1) = " << sizeof(Derived1) << " sizeof(d1) = " << sizeof(d1) << endl;
cout << "sizeof(Derived2) = " << sizeof(Derived2) << " sizeof(d1) = " << sizeof(d1) << endl;
Derived1 d; // d 能直接呼叫Derived1的所有函式、Base1的非虛擬函式,間接呼叫base1的虛擬函式
d.f(); // Derived::f
d.g1(); // Derived::g1
d.Base1::f(); // Base1::f
d.func();
Derived1 *d = new Derived1(); // 同上
d->f(); // Derived::f
d->g1(); // Derived::g1
d->Base1::f(); // Base1::f
d->func(); // Derived1::func 過載
d->Base1::func();// Base1::func 間接呼叫,顯示呼叫
Base1 *b = new Derived1(); // b 只能呼叫Base1的函式,但是虛擬函式會被動態繫結
b->f(); // Derived1::f
b->g(); // Base1::g
b->func(); // Base1::func
Base1 *b = new Base1(); // 顯然只能呼叫base1的函式
b->f(); // Base1::f
b->g(); // Base1::g
b->func();// Base1::func
Base1 *b = new Base1();
delete b; // 不論基類解構函式是否為虛擬函式,都會執行函式體
Base1 *b = new Derived1();
delete b; // 當基類解構函式為虛時,才會析構父類; 否則,只執行基類的解構函式
Derived1 *d = new Derived1();
delete d; // 不論基類解構函式是否為虛擬函式,都會執行函式體
Derived2 d2;//直接呼叫Derived2的函式,間接呼叫父類函式
d2.f(); // Derived2::f
d2.Base1::f(); // Base1::f
d2.Base2::f(); // Base2::f
Derived2 *d2 = new Derived2();//直接呼叫Derived2的函式,間接呼叫父類函式
d2->f(); // Derived2::f
d2->Base1::f(); // Base1::f
d2->Base2::f(); // Base2::f
delete d2;
Base1 *d2 = new Derived2();// 呼叫base1的函式和Derived2重寫的函式,
d2->f(); // 子類重寫 Derived2::f
d2->Base1::f(); // 間接呼叫父類虛擬函式 Base1::f
d2->g(); // 直接呼叫父類虛擬函式 Base1::g
d2->func(); // 直接呼叫父類非虛擬函式 Base1::func
delete d2; //其中一個父類解構函式必須為虛擬函式
return 0;
}