终于要决心弄明白继承了,以前仅限于大学时学习,以后工作也没有用,现在就依照(百度百科)文章写些测试的代码。
public | protected | private | |
公有继承 | public | protected | 不可见 |
私有继承 | private | private | 不可见 |
保护继承 | protected | protected | 不可见 |
- #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
- memset(this,0,sizeof(class ONESEL));