目录
继承
概念
#include <iostream>
using namespace std;
// 基类
class Father
{
private:
string name = "张";
public:
void set_name(string name)
{
this->name = name;
}
string get_name()
{
return name;
}
void work()
{
cout << "我的工作是厨师,我负责炒菜" << endl;
}
};
// 派生类
class Son:public Father
{
};
int main()
{
Son son;
cout << son.get_name() << endl;
son.work();
return 0;
}
#include <iostream>
using namespace std;
// 基类
class Father
{
private:
string name = "张";
public:
void set_name(string name)
{
this->name = name;
}
string get_name()
{
return name;
}
void work()
{
cout << "我的工作是厨师,我负责炒菜" << endl;
}
};
// 派生类
class Son:public Father
{
public:
void init()
{
set_name("王");
}
void work()
{
cout << "我的工作是司机" << endl;
}
void game()
{
cout << "开挖掘机" << endl;
}
};
int main()
{
Son son;
son.init();
cout << son.get_name() << endl;
son.work();
son.game();
son.Father::work();
return 0;
}
构造函数
1.派生类与基类构造函数的关系
#include <iostream>
using namespace std;
// 基类
class Father
{
private:
string name;
public:
Father(string name):name(name){} // 有参构造函数
string get_name()
{
return name;
}
};
// 派生类
class Son:public Father
{
};
int main()
{
// Son s; // 找不到基类的无参构造函数
// Son s("张"); // 找不派生类的有参构造函数
return 0;
}
2.解决方案
(1)补充基类的无参构造函数
#include <iostream>
using namespace std;
// 基类
class Father
{
private:
string name;
public:
// 无参构造函数
Father():name("张"){}
// 有参构造函数
Father(string name):name(name){}
string get_name()
{
return name;
}
};
// 派生类
class Son:public Father
{
public:
};
int main()
{
Son s;
cout << s.get_name() << endl;
return 0;
}
(2)手动在派生类中调用基类构造函数
1.透传构造
#include <iostream>
using namespace std;
// 基类
class Father
{
private:
string name;
public:
// 有参构造函数
Father(string name):name(name){}
string get_name()
{
return name;
}
};
// 派生类
class Son:public Father
{
public:
// 编译器会自动添加构造函数,透传调用基类无参构造函数(透传构造)
// Son():Father(){}
// 手动添加构造函数,透传构造
Son():Father("王"){}
// 有参构造函数,调用基类有参构造函数
Son(string fn):Father(fn){}
};
int main()
{
Son s;
cout << s.get_name() << endl;
Son s1("张");
cout << s1.get_name() << endl;
return 0;
}
2.委托构造
#include <iostream>
using namespace std;
// 基类
class Father
{
private:
string name;
public:
Father(string name):name(name){} // 有参构造函数
string get_name()
{
return name;
}
};
// 派生类
class Son:public Father
{
public:
Son():Son("李"){} // 委托构造
Son(string fn):Father(fn){} // 有参构造函数,调用基类有参构造函数
};
int main()
{
Son s;
cout << s.get_name() << endl;
Son s1("赵");
cout << s1.get_name() << endl;
return 0;
}
3.继承构造
#include <iostream>
using namespace std;
// 基类
class Father
{
private:
string name;
public:
Father():Father("张"){}
Father(string name):name(name){} // 有参构造函数
string get_name()
{
return name;
}
};
// 派生类
class Son:public Father
{
public:
using Father::Father;
// 只需要补充这一句话,编译器就会自动添加下面两种构造函数
// Son():Father(){}
// Son(string fn):Father(fn){}
};
int main()
{
Son s;
cout << s.get_name() << endl;
Son s1("王");
cout << s1.get_name() << endl;
return 0;
}
3.对象的创建与销毁流程
#include <iostream>
using namespace std;
class Value
{
private:
string str;
public:
Value(string str):str(str)
{
cout << str<< " 调用Value构造函数" << endl;
}
~Value()
{
cout << str << " 调用Value析构函数" << endl;
}
};
class Father
{
public:
static Value s_value;
Value val = Value("Father 成员变量");
Father()
{
cout << "Father 构造函数被调用了" << endl;
}
~Father()
{
cout << "Father 析构函数被调用了" << endl;
}
};
Value Father::s_value = Value("静态FatherValue");
class Son:public Father
{
public:
static Value s_value;
Value val = Value("Son成员变量");
Son()
{
cout << "Son 构造函数被调用了" << endl;
}
~Son()
{
cout << "Son 析构函数被调用了" << endl;
}
};
Value Son::s_value = Value("静态SonValue");
int main()
{
cout << "主函数被调用了" << endl;
// 局部代码块
{
Son s;
cout << "对象执行中" << endl;
}
cout << "主函数结束了" << endl;
return 0;
}
4.多重继承
(1)概念
#include <iostream>
using namespace std;
class Sofa
{
public:
void sit()
{
cout << "可以坐着" << endl;
}
};
class Bed
{
public:
void lay()
{
cout << "可以躺着" << endl;
}
};
// 派生类
class SofaBed:public Sofa,public Bed
{
};
int main()
{
SofaBed sobe;
sobe.lay();
sobe.sit();
return 0;
}
(2)可能出现的问题
1.重名问题
#include <iostream>
using namespace std;
class Sofa
{
public:
void sit()
{
cout << "可以坐着" << endl;
}
void clean()
{
cout << "打扫沙发" << endl;
}
};
class Bed
{
public:
void lay()
{
cout << "可以躺着" << endl;
}
void clean()
{
cout << "打扫床" << endl;
}
};
// 派生类
class SofaBed:public Sofa,public Bed
{
};
int main()
{
SofaBed sobe;
sobe.lay();
sobe.sit();
sobe.Sofa::clean();
sobe.Bed::clean();
return 0;
}
2.菱形继承
#include <iostream>
using namespace std;
// 家具厂
class Furniture
{
public:
void func()
{
cout << "家具厂里有家具" << endl;
}
};
class Sofa:virtual public Furniture
{
};
class Bed:virtual public Furniture
{
};
// 派生类
class SofaBed:public Sofa,public Bed
{
};
int main()
{
SofaBed sobe;
sobe.func();
return 0;
}
权限
1.权限修饰符
类内 | 派生类内 | 全局(类外) | |
private | √ | × | × |
protected | √ | √ | × |
public | √ | √ | √ |
#include <iostream>
using namespace std;
class Test
{
protected:
string str="保护权限";
public:
Test()
{
cout<<str<<endl;
}
};
class Base:public Test
{
public:
Base()
{
cout<<str<<endl;
}
};
int main()
{
Base b;
//cout<<b.str<<endl;// 错误 b是保护权限
return 0;
}
2.不同权限的继承
(1)公有继承
#include <iostream>
using namespace std;
class Base
{
private:
string str1 = "私有成员";
protected:
string str2 = "保护成员";
public:
string str3 = "公有成员";
};
class Son:public Base
{
public:
Son()
{
// cout << str1 << endl; // 错误 str1为私有成员
cout << str2 << endl;
cout << str3 << endl;
}
};
int main()
{
Son s1;
return 0;
}
(2)保护继承
#include <iostream>
using namespace std;
class Base
{
private:
string str1 = "私有成员";
protected:
string str2 = "保护成员";
public:
string str3 = "公有成员";
};
class Son:protected Base
{
public:
Son()
{
//cout << str1 << endl; // 错误 str1为私有成员
cout << str2 << endl;
cout << str3 << endl;
}
};
int main()
{
Son s1;
// cout << s1.str3 << endl; // 保护成员调用失败
return 0;
}
(3)私有继承
#include <iostream>
using namespace std;
class Test
{
private:
string str1="私有成员";
protected:
string str2="保护成员";
public:
string str3="公有成员";
};
class Base:private Test
{
public:
Base()
{
// cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
}
};
class Demo:public Base
{
public:
Demo()
{
cout << str2 << endl;
cout << str3 << endl;
}
};
int main()
{
Base b;
// cout<<b.Test::str3<<endl;
return 0;
}