一.继承的引出
class News
{
public:
void header()
{
cout<<"header"<<endl;
}
void footer()
{
cout<<"footer"<<endl;
}
void left()
{
cout<<"left"<<endl;
}
void content()
{
cout<<"content"<<endl;
}
};
void test01()
{
News news;
news.header();
news.footer();;
news.left();
news.content();
News yule;
yule.header();
yule.footer();;
yule.left();
yule.content();
}
class BasePage
{
public:
void header()
{
cout<<"header"<<endl;
}
void footer()
{
cout<<"footer"<<endl;
}
void left()
{
cout<<"left"<<endl;
}
};
class New:public BasePage
{
public:
void content()
{
cout<<"content"<<endl;
}
};
class Yule:public BasePage
{
public:
void content()
{
cout<<"Yule"<<endl;
}
};
class Game:public BasePage
{
public:
void content()
{
cout<<"LOL"<<endl;
}
};
void test02()
{
New news;
news.content();
news.footer();
news.header();
news.left();
Yule yu;
yu.footer();
yu.header();
yu.left();
yu.content();
}
二.3种继承方式
1.公有继承
class Son1:public Base1
{
public:
void Func()
{
cout<<m_A<<endl;
cout<<m_B<<endl;
}
};
class Son2:protected Base1
{
public:
void Func()
{
cout<<m_A<<endl;
cout<<m_B<<endl;
}
};
void myFunc()
{
Son1 son1;
son1.m_A;
Son2 son2;
}
2.保护继承
class Son2:protected Base1
{
public:
void Func()
{
cout<<m_A<<endl;
cout<<m_B<<endl;
}
};
void myFunc()
{
Son1 son1;
son1.m_A;
Son2 son2;
}
3.私有继承
class Son:public Base1
{
public:
int m_D;
};
void test03()
{
cout<<sizeof(Son)<<endl;
}
三.继承中的构造和析构
class Base3
{
public:
Base3()
{
cout<<"gouzao"<<endl;
}
~Base3()
{
cout<<"xigou"<<endl;
}
};
class Son3 :public Base3
{
public:
Son3()
{
cout<<"Son3 gouzao"<<endl;
}
~ Son3()
{
cout<<"Son3 xigou"<<endl;
}
};
void test04()
{
Base3 b3;
Son3 s3;
}
class Base4
{
public:
Base4(int a)
{
this->m_A = a;
cout<<"arg gouzao"<<endl;
}
int m_A;
};
class Son4:public Base4
{
public:
Son4(int a):Base4(a)
{
cout<<" Son4 gouzao"<<endl;
}
};
void test05()
{
Son4 s4(1000);
}
四.继承中的同名处理
1.普通同名成员函数和成员属性
class Base5
{
public:
Base5()
{
m_A= 100;
}
void func()
{
cout<<"Base5 func"<<endl;
}
int m_A;
};
class Son5 :public Base5
{
public:
Son5()
{
m_A = 200;
}
void func()
{
cout<<"Son5 func"<<endl;
}
int m_A;
};
void test06()
{
Son5 s5;
cout<<s5.m_A<<endl;
cout<<s5.Base5::m_A<<endl;
s5.func();
s5.Base5::func();
}
2.静态同名成员函数和属性
class Base6
{
public:
static int m_A;
};
int Base6::m_A = 10;
class Son6:public Base6
{
};
void test07()
{
cout<<Son6::m_A<<endl;
}
class Base7
{
public:
static void func()
{
cout<<"Base7 func"<<endl;
}
static void func(int a)
{
cout<<"Base7 func int"<<endl;
}
static int m_A;
};
int Base7::m_A = 10;
class Son7:public Base7
{
public:
static void func()
{
cout<<"Son7 func"<<endl;
}
static int m_A;
};
int Son7::m_A = 20;
void test08()
{
cout<<Son7::m_A<<endl;
cout<<Base7::m_A<<endl;
Son7::func();
Son7::Base7::func();
Base7::func();
}
五.多继承问题及其解决
class Base8
{
public:
Base8()
{
m_A = 10;
}
int m_A;
};
class Base9
{
public:
Base9()
{
m_A = 20;
}
int m_A;
};
class Son8:public Base8,public Base9
{
public:
int m_C;
int m_D;
};
void test09()
{
cout<<sizeof(Son)<<endl;
Son8 s8;
cout<<s8.Base8::m_A<<endl;
}
六.菱形继承问题及解决
class Animal
{
public:
int m_Age;
};
class Sheep:virtual public Animal
{
public:
};
class Tuo:virtual public Animal
{
};
class SheepTuo :public Sheep,public Tuo
{
};
void test10()
{
SheepTuo st;
st.Sheep::m_Age = 10;
st.Tuo::m_Age = 20;
cout<<st.Sheep::m_Age<<endl;
cout<<st.Tuo::m_Age <<endl;
cout<<st.m_Age <<endl;
}