您的位置:首页 > 编程语言 > C语言/C++

c++ inheritance -- 继承

2015-09-04 01:41 399 查看
c++ inheritance -- 继承
终于要决心弄明白继承了,以前仅限于大学时学习,以后工作也没有用,现在就依照(百度百科)文章写些测试的代码。

文章说
=============================
C++继承  在C++语言中,一个派生类可以从一个基类派生,也可以从多个基类派生。从一个基类派生的继承称为单继承;从多个基类派生的继承称为多继承。   派生类的定义格式   单继承的定义格式如下:   class <派生类名>:<继承方式><基类名>   {   <派生类新定义成员>   };   其中,<派生类名>是新定义的一个类的名字,它是从<基类名>中派生的,并且按指定的<继承方式>派生的。<继承方式>常使用如下三种关键字给予表示:   public 表示公有基类;   private 表示私有基类;   protected 表示保护基类;   多继承的定义格式如下:   class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,…   {   <派生类新定义成员>   };   可见,多继承与单继承的区别从定义格式上看,主要是多继承的基类多于一个。   如果省略继承方式,对'class'将采用私有继承,对'struct'将采用公有继承。   也就是说   class Base1{};   struct Base2{};   class Derive:Base1,Base2{};   那么,Derive类将私有继承Base1,公有继承Base2。相当于:   class Derive:private Base1,public Base2{};派生类的三种继承方式  公有继承(public)、私有继承(private)、保护继承(protected)是常用的三种继承方式。   1. 公有继承(public)   公有继承的特点是基类的公有成员和保护成员作为派生类的成员时,它们都保持原有的状态,而基类的私有成员仍然是私有的,不能被这个派生类的子类所访问。   2. 私有继承(private)   私有继承的特点是基类的公有成员和保护成员都作为派生类的私有成员,并且不能被这个派生类的子类所访问。   3. 保护继承(protected)   保护继承的特点是基类的所有公有成员和保护成员都成为派生类的保护成员,并且只能被它的派生类成员函数或友元访问,基类的私有成员仍然是私有的。   下面列出三种不同的继承方式的基类特性和派生类特性。   
publicprotectedprivate
公有继承publicprotected不可见
私有继承privateprivate不可见
保护继承protectedprotected不可见
  为了进一步理解三种不同的继承方式在其成员的可见性方面的区别,下面从三种不同角度进行讨论。对于公有继承方式  (1) 基类成员对其对象的可见性:   公有成员可见,其他不可见。这里保护成员同于私有成员。   (2) 基类成员对派生类的可见性:   公有成员和保护成员可见,而私有成员不可见。这里保护成员同于公有成员。   (3) 基类成员对派生类对象的可见性:   公有成员可见,其他成员不可见。   所以,在公有继承时,派生类的对象可以访问基类中的公有成员;派生类的成员函数可以访问基类中的公有成员和保护成员。这里,一定要区分清楚派生类的对象和派生类中的成员函数对基类的访问是不同的。对于私有继承方式  (1) 基类成员对其对象的可见性:   公有成员可见,其他成员不可见。   (2) 基类成员对派生类的可见性:   公有成员和保护成员是可见的,而私有成员是不可见的。   (3) 基类成员对派生类对象的可见性:   所有成员都是不可见的。   所以,在私有继承时,基类的成员只能由直接派生类访问,而无法再往下继承。对于保护继承方式  这种继承方式与私有继承方式的情况相同。两者的区别仅在于对派生类的成员而言,对基类成员有不同的可见性。   上述所说的可见性也就是可访问性。关于可访问性还有另的一种说法。这种规则中,称派生类的对象对基类访问为水平访问,称派生类的派生类对基类的访问为垂直访问。一般规则  公有继承时,水平访问和垂直访问对基类中的公有成员不受限制;   私有继承时,水平访问和垂直访问对基类中的公有成员也不能访问;   保护继承时,对于垂直访问同于公有继承,对于水平访问同于私有继承。   对于基类中的私有成员,只能被基类中的成员函数和友元函数所访问,不能被其他的函数访问。   基类与派生类的关系   任何一个类都可以派生出一个新类,派生类也可以再派生出新类,因此,基类和派生类是相对而言的。基类与派生类之间的关系1. 派生类是基类的具体化  类的层次通常反映了客观世界中某种真实的模型。在这种情况下,不难看出:基类是对若干个派生类的抽象,而派生类是基类的具体化。基类抽取了它的派生类的公共特征,而派生类通过增加行为将抽象类变为某种有用的类型。2. 派生类是基类定义的延续  先定义一个抽象基类,该基类中有些操作并未实现。然后定义非抽象的派生类,实现抽象基类中定义的操作。例如,虚函数就属此类情况。这时,派生类是抽象的基类的实现,即可看成是基类定义的延续。这也是派生类的一种常用方法。3. 派生类是基类的组合  在多继承时,一个派生类有多于一个的基类,这时派生类将是所有基类行为的组合。   派生类将其本身与基类区别开来的方法是添加数据成员和成员函数。因此,继承的机制将使得在创建新类时,只需说明新类与已有类的区别,从而大量原有的程序代码都可以复用,所以有人称类是“可复用的软件构件”。继承成员的调整1.恢复访问控制方式  访问声明采用作用域"::" ,它的一般形式为:基类名::成员名;。在派生类的类界面中,将这些访问声明放在合适的访问控制保留字之后,从而改变在派生类中该成员的访问控制方式。2.继承成员的重定义  如果在派生类中定义了一个函数原型与继承成员函数一模一样的成员函数,则该函数实现的函数体是对继承成员函数的重定义。
=============================
代码

#include <iostream>

#include <cstring>

#include <string>

using namespace std;

enum e_zoo_obj_kind{

null = 0,

#define zk_null (e_zoo_obj_kind(null))

no = 0,

#define zk_no (e_zoo_obj_kind(no))

animal = 1,

#define zk_animal (e_zoo_obj_kind(animal))

plant = 2,

#define zk_plant (e_zoo_obj_kind(plant))

others = 3,

#define zk_others (e_zoo_obj_kind(others))

max = 4

#define zk_max 4

};

static const char * g_zk_str [zk_max ] ={

"null",

"animal",

"plant",

"others"

};

#define NEW_LINE std::cout<<"\n"

static unsigned int g_msg_id = 0;

enum e_msg_type{

mt_fatal = 0, //0

mt_notice,

mt_debug,

mt_info ,

mt_ignore, // 4

mt_max // 5

#define MSG_TYPE_MAX 5

};

static const char *g_msg_type_str[MSG_TYPE_MAX] = {

"FATAL",

"NOTICE",

"DEBUG",

"INFO",

"IGNORE"

};

class Message{

private:

unsigned int id;

protected:

e_msg_type type;

public:

string msg;

public:

Message():id(++g_msg_id),type(mt_ignore),msg("null"){}

Message(e_msg_type t, string m):type(t),msg(m),id(++g_msg_id){}

Message &set_type(e_msg_type t){ type = t; return *this; }

e_msg_type get_type(){ return type; }

Message &set_msg(string m){msg = m; return *this; }

Message &print(void){

cout << "Msg:"

<< "id-" << id

<< ",type-" << g_msg_type_str[type]

<< ",msg-" << msg << endl;

return *this;

}

};

class Obj{

private:

char name [40];

public:

Obj() { strcpy(name,"null") ;}

Obj(char *nm){

strncpy(name,!nm?"null":nm,sizeof(name));

}

void say(){

cout << "name:" << name << endl;

}

void say(Obj *obj){

!obj

? cout << "null\n"

: cout << "name:" << obj->name << endl;

}

void set_name(char *nm){

!nm ?"": strncpy(name,nm,sizeof(name));

}

char *get_name(void) {return name;}

};

class Zoo_obj:public Obj{

private:

e_zoo_obj_kind kind;

public:

Zoo_obj():Obj(),kind(null) {}

Zoo_obj(char *nm, e_zoo_obj_kind k):Obj(nm),kind(k){

}

void say(void){

cout << "Zoo_obj::";

Obj::say();

cout << "kind:" << g_zk_str[kind] << endl;

}

void say(Zoo_obj &obj){

cout << "Zoo_obj::";

Obj::say();

cout << "kind:" << g_zk_str[obj.kind] << endl;

}

e_zoo_obj_kind get_kind(){ return kind; }

Zoo_obj &set_kind(e_zoo_obj_kind k){

kind = k;

return *this;

}

Zoo_obj &print_kind(){

cout << "kind:" << g_zk_str[kind] << endl;

return *this;

}

};

class Animal:public Zoo_obj{

private:

int lags;

public:

Animal(char *nm, int l) :lags(l),Zoo_obj(nm,animal){ }

void say(){

Obj::say();

Zoo_obj::say();

cout << "lag:" << lags << endl;

}

};

class Plant:public Obj, protected Message{

private:

union {

unsigned int property;

struct{

unsigned int

hasleaf:1,

hasflower:1,

hastrunk:1,

hasrattan:1,

private1:1,

private2:1;

};

};

public:

Plant():Obj(),property(0){ }

Plant &set_leaf(bool has) {hasleaf = has; return *this;}

Plant &set_flower(bool has) {hasflower = has; return *this;}

Plant &set_trunk(bool has) {hastrunk = has; return *this;}

Plant &set_rattan(bool has) {hasrattan = has; return *this;}

bool has_leaf(){return hasleaf ;}

bool has_flower(){return hasflower ;}

bool has_trunk(){return hastrunk ;}

bool has_rattan(){return hasrattan;}

Plant & print(void){

Obj::say();

cout << "has leaf:" << hasleaf << endl;

cout << "has flower:" << hasflower << endl;

cout << "has trunk:" << hastrunk << endl;

cout << "has rattan:" << hasrattan << endl;

return *this;

}

};

int main(void){

Zoo_obj obj = Zoo_obj( "cat", e_zoo_obj_kind(animal));

obj.say(); //inherit from Obj in public-style

NEW_LINE;

obj.print_kind().set_kind(no).print_kind(); //series invoking

NEW_LINE;

Plant peony;

peony.set_name("peony"); // Obj::set_name()

peony.set_leaf(true).set_flower(true).print();

NEW_LINE;

Animal dog ("joel's dog",4); // say by its father and grandfather

dog.say();

dog.set_name("black dog") ;

/* Obj::set_name if Zoo_obj was

described by protect, the assembler would show

"error: ‘void Obj::set_name(char*)’ is inaccessible"

*/

NEW_LINE;

test_Mesaage:

Message msg;

msg.print();

NEW_LINE;

test_sizeof:

cout << "sizeof these:"

<< "\nObj: " << sizeof(Obj)

<< "\nMessage: " << sizeof(Message)

<< "\nZoo_obj: " << sizeof(Zoo_obj) << "\t:Obj + enum"

<< "\nAnimal: " << sizeof(Animal) << "\t:Zoo_obj + int"

<< "\nPlant: " << sizeof(Plant) << "\t:Obj,Message + union\n";

}

结果

Zoo_obj::name:cat

kind:animal

kind:animal

kind:null

name:peony

has leaf:1

has flower:1

has trunk:0

has rattan:0

name:joel's dog

Zoo_obj::name:joel's dog

kind:animal

lag:4

Msg:id-2,type-IGNORE,msg-null

sizeof these:

Obj: 40

Message: 12

Zoo_obj: 44 :Obj + enum

Animal: 48 :Zoo_obj + int

Plant: 56 :Obj,Message + union

从test_sizeof段中可以看出,一个类(eg. O)的大小会是他本身和他的祖先(A)大小的和,我真看不出这有什么好,假设他protected继承(P),他的子孙(S)就不能从他的祖先(P)中得到好处,除了徒增自身大小。

还有,用gdb调试可以清楚的看到,在继承时自身空间的排列,先是祖先的空间分配,接着是自身的成员。千万不能简单的在成员函数中像c一样初始化

memset(this,0,sizeof(class ONESEL));

这样真的就**了,我就犯了混,因为继承,使他隐藏的太深了。
这里没有涉及到友元函数,虚函数。
明天吧,在写点测试代码。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: