型別轉換
建構函式執行順序
classa;
a a;
a.x =5;
// 錯誤
classa;
classb:
public a
;void b::
function()
example:
#include
#include
#include
"rectangle.h"
using
namespace std;
intmain()
#ifndef _rectangle_h
#define _rectangle_h
#include
"point.h"
class
rectangle
:public point
float
geth()
const
float
getw()
const
private
://新增私有資料成員
float w, h;};
#endif
//_rectangle_h
#ifndef _point_h
#define _point_h
class
point
void
move
(float offx,
float offy)
float
getx()
const
float
gety()
const
private
://私有資料成員
float x, y;};
#endif
//_point_h
classa;
classb;
classc:
public a,
private b
;void a::
seta
(int x)
void b::
setb
(int x)
void c::
setc
(int x,
int y,
int z)
intmain()
#include
using
namespace std;
class
base1};
class
base2
:public base1 };
class
derived
:public base2 };
void
fun(base1* ptr)
intmain()
/*base1::display()
base1::display()
base1::display()
*/
預設情況下
#include
using
namespace std;
classb;
b::b()
b::b
(int i)
b::~b(
)void b::
print()
const
classc:
public b
;c::c(
)c::c(
int i,
int j):b
(i),
c(j)
c::~c(
)void c::
print()
const
intmain()
/*constructor called.
constructor called.56
destrutor called.
destructor called.
*/
#include
using
namespace std;
class
base1};
class
base2};
class
base3};
class
derived
:public base2,
public base1,
public base3
private
: base1 member1;
base3 member3;
base2 member2;};
intmain()
/*construting base2 2
constructing base1 1
construing base3 *
constructing base1 3
construing base3 *
construting base2 4
*/
若派生類沒有宣告複製建構函式
#include
using
namespace std;
class
base1};
class
base2};
class
derived
:public base1,
public base2
//同名函式成員};
intmain()
/*member of derived
member of base1
member of base2
*/
classa;
classb;
classc:
public a,
public b
;int
main()
#include
using
namespace std;
class
base0};
class
base1
:public base0
;class
base2
:public base0
;class
derived
:public base1,
public base2 };
intmain()
/*d.var0 d.base0::var0 有二義性
*/
派生和繼承
1.派生類的概念 以物件導向的程式設計的觀點來看,繼承表達的是物件類之間的相互關係。這種關係使得某類物件可以繼承另外一類物件的特徵和能力。如果一類物件繼承了另一類物件的特徵和能力,這個類就叫做所繼承類的派生類。1.1 宣告乙個派生類的一般格式是 class 派生類名 派生方式 public or p...
繼承和派生
繼承 類和類之間的關係 派生 子類在父類基礎上衍變出一些新的特性或者功能 子模擬父類更加強大 父類 parentclass superclass 基類 子類 childclass subclass 派生類 nsobject 還可以稱為根類 private 私有繼承 protected 受保護繼承 p...
繼承和派生
繼承中的基本語法 通過公有繼承,派生類得到了基類中除建構函式 析構函式之外的所有成員。include using namespace std class parent void howtoprint2 parent base void main int b protected private cla...