想知道构造函数怎样工作的吗?
我来告诉你
首先我们来看定义未知数的顺序;
我们先定义height,在定义weight,那么构造函数是按照先初始化height在初始化weight的顺序构造的,并不是按照构造函数的顺序,举个例子:
#include <iostream> using namespace std; class people{ public: people(int x); void a(); ~people(); private: string name="小明"; int height; int weight; }; people::people(int x=9):height(x),weight(height){} //先初始化height,再将height赋值给weight people::~people(){ } void people::a(){ cout<<name<<' '<<height<<' '<<weight; } int main(){ people cao(170); cao.a(); return 0; }
#include <iostream> using namespace std; class people{ public: people(int x); void a(); ~people(); private: string name="小明"; int height; int weight; }; people::people(int x=9):height(weight),weight(x){} //先初始化height,但height依赖于weight,由于weight没有初始化,所以出现错误; people::~people(){ } void people::a(){ cout<<name<<' '<<height<<' '<<weight; } int main(){ people cao(170); cao.a(); return 0; }
3.类的继承
子承父业吗,子类继承父类,分为三种继承方式;
(1)公共继承:父类的公共属性在子类还是公共属性,保护属性还是保护属性,私有属性子类访 问不到
(2)保护继承:父类的公共属性和保护属性在子类中都变为保护属性;私有还是访问不到
(3)私有继承:父类的公共属性在子类变为保护属性,父类的保护属性变为私有属性,私有访问 不到;
举例说明:
(1)公共继承:
#include <iostream> using namespace std; class father{ public: string a="father 公共"; protected: string b="father 保护"; private: string c="father 私有"; }; class son:public father{ public: // string a="son 公共"; // string b="son 保护"; // c="son 私有"; void dain(){ cout<<a<<endl<<b<<endl<<endl; // cout<<c; //报错 } }; int main(){ son linyuan; linyuan.dain(); cout<<linyuan.a<<endl; // cout<<linyuan.b<<endl; //报错 return 0; }
(2)保护继承
#include <iostream> using namespace std; class father{ public: string a="father 公共"; protected: string b="father 保护"; private: string c="father 私有"; }; class son:protected father{ public: // string a="son 公共"; // string b="son 保护"; // c="son 私有"; void dain(){ cout<<a<<endl<<b<<endl<<endl; // cout<<c; //报错 } }; int main(){ son linyuan; linyuan.dain(); // cout<<linyuan.a<<endl; // cout<<linyuan.b<<endl; //报错 return 0; }
(3)私有继承
#include <iostream> using namespace std; class father{ public: string a="father 公共"; protected: string b="father 保护"; private: string c="father 私有"; }; class son:private father{ public: // string a="son 公共"; // string b="son 保护"; // c="son 私有"; void dain(){ cout<<a<<endl<<b<<endl<<endl; // cout<<c; //报错 } }; int main(){ son linyuan; linyuan.dain(); // cout<<linyuan.a<<endl; // cout<<linyuan.b<<endl; //报错 return 0; }
那么继承方式讲完了,其实C++类还支持同时继承多个类例如;
#include <iostream> using namespace std; class father1{ public: string name1; father1(){ name1="小刚"; } }; class father2{ public: string name1; father2(){ name1="小明"; } }; class son:public father1,public father2{ public: string name1; son(){ name1="木木渊"; } }; int main(){ son a; cout<<a.name1<<endl; cout<<a.father1::name1<<endl; cout<<a.father2::name1; }
派生类和父类继承元素重名时可以通过命名空间来分别 ,函数也是如此实现多继承;
接下来我们看一下析构函数的情况;
#include <iostream> using namespace std; class father1{ public: string name1; father1(){ cout<<"father1 构造函数"<<endl; name1="小刚"; } ~father1(){ cout<<"father1 析构函数"<<endl; } }; class father2{ public: string name1; father2(){ name1="小明"; cout<<"father2 构造函数"<<endl; } ~father2(){ cout<<"father2 析构函数"<<endl; } }; class son:public father1,public father2{ public: string name1; son(){ name1="木木渊"; cout<<"son 构造函数"<<endl; } ~son(){ cout<<"son 析构函数"<<endl; } }; int main(){ son a; cout<<"main 要结束了"<<endl; }
很明显son先继承father1,在继承father2,因此构造函数先是father1,再father2,最后son,析构函数正好相反;