文章目录
一、其他构造函数(如缺省函数)
构造函数分为 有参构造 和 无参构造
其中有参构造 又分为 是否 缺省构造
class MM {
public:
/*MM() {}
MM():age(19),height(1.82),name("libai"){}*/
MM() :age(19),height(1.82),name("1232") {
}
//注意:以上三种情况(构造无参函数)只能选择其中一种
/*MM(int age, double height, string name):name("梦") {
this->age = age;
this->height = height;
}*/
//类似于 设置默认的有参构造函数-----缺省函数
//无论下面的有参构造函数 输入的啥,输出的结果均是默认值,不变
/*MM(int age, double height, string name):name("花"),height(1.65),age(18){}*/
void print() {
cout<< name << "\t" << height << "\t" << age << endl;
}
//传变量,传入的是啥值,输出的就是啥值
MM(int age, double height, string name):name(name),age(age),height(height) {
this->age = age;
this->height;
this->name=name;
}
private:
int age;
double height;
string name;
};
二、什么是类的组合?
定义:只要类与类之间关系是包含关系 (一部分),具体到代码:另一个类的对象是当前这个类数据成员
三、类的组合 使用
1、使用:类的组合 的构造函数需使用初始化参数列表
2、如何使用初始化参数列表
例如:构造函数(参数1,参数2....):数据成员1(参数1),数据成员2(参数2)...{}
MM(string name,int age,double score):name(name),age(age),score(score){}
使用组合类的实例:window类组合了edit 和 button 类
#include<iostream>
#include<string>
using namespace std;
//组合类的创建及使用
class Button
{
public:
Button():x(10),y(10),w(10),h(10){}
//有参函数的 缺省函数
/*Button(int x,int y,int w,int h,string text) :x(11),y(22),w(33),h(44),text("文字") {}*/
Button(int x, int y, int w, int h,string text){
this->h = h;
this->w = w;
this->x = x;
this->y = y;
this->text = text;
}
Button(int x, int y, int w, int h) {
this->h = h;
this->w = w;
this->x = x;
this->y = y;
}
void printBut() {
cout << "Button: ";
cout << x <<" " << y << " " << w << " " << h <<" "<<text<< endl;
}
private:
int x;
int y;
int h;
int w;
string text;
};
class Edit {
public:
Edit() :x(11), y(11), w(11), h(11) {}
Edit(int x, int y, int w, int h) {
this->h = h;
this->w = w;
this->x = x;
this->y = y;
}
void printEdit() {
cout << "Edit: ";
cout << x << " " << y << " " << w << " " << h << endl;
}
private:
int x;
int y;
int h;
int w;
};
//组合类
class Window {
public:
/*Window() {
edit.printEdit();
button.printBut();
}*/
Window(int x, int y, int w, int h) :button(x, y, w, h) {
edit.printEdit();
button.printBut();
}
Window(int xe, int ye, int we, int he, int xb, int yb, int wb,int hb) :button(xb, yb, wb, hb),
edit(xe, ye, we, he) {
edit.printEdit();
button.printBut();
}
//为了解决上面的参数初始化的太多,采用下面这种方案来解决,
//避免组合类种传参太多,采用下面的传一个固定的值来解决这个问题
Window():edit(200,200,200,200),button(300,300,300,300){
edit.printEdit();
button.printBut();
}
private:
Edit edit;
Button button;
};
//在main中使用组合类
int main(){
cout << endl << "调用无参构造函数:" << endl;
Window w;
cout << endl <<"调用一个有参构造函数,一个无参构造函数:" << endl;
Window w1(66,77,88,99);
cout << endl <<"调用两个有参构造函数:" << endl;
Window w3(33, 33, 33, 33, 44, 44, 44, 44);
while (1);
return 0;
}
3、注意事项—构造与析构顺序问题
构造和析构顺序问题:
- 析构顺序和构造顺序相反
- 优先构造分支类的对象,在构造自身
组合类构造顺序只和定义顺序有关,和初始化参数列表一点貌似毛线都没有
#include<iostream>
#include<string>
using namespace std;
class A
{
public:
A(string a="A") :a(a) {
cout << a;
}
~A();
private:
string a;
};
A::~A()
{
cout << "A";
}
class B
{
public:
B(string name="B") :name(name){
cout << name;
}
~B();
private:
string name;
};
B::~B()
{
cout << "B";
}
class C
{
public:
C(string c = "C") :c(c) {
cout << c;
}
~C();
private:
string c;
};
C::~C()
{
cout << "c";
}
class Data
{
public:
Data() :a("A"), b("B"), c("C") //构造和初始化列表顺序是 无关的
{
cout << "D";
}
~Data() {
cout << "D" << endl;
}
private:
//注意: 只与这下面的初始化顺序 有关
A a;
C c;
B b;
//从上往下的顺序 然后为构造本身
//结果为: ACBDDBCA
};
int main() {
{
Data dt;
}
while (1)
{
}
return 0;
}
四、总结
提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了类的组合 的使用以及一些上次未提及的构造函数的其他写法,特别是类的组合日后很常见,为日后的编程起到了很大的作用。