本文学习自 狄泰软件学院 唐佐林老师的 C++课程
实验1:子类构造初探
实验2: 子类构造深度剖析
实验3:对象的析构
创建子类对象的时候,会调用子类的构造函数,同时会默认调用父类的无参构造函数或者默认参数的构造函数。如果父类没有提供无参构造函数或者无参构造函数,必须通过子类在初始化列表中显式调用父类的有参构造函数。
实验1:子类构造初探
#include <iostream>
#include <string>
using namespace std;
class Parent
{
public:
Parent()
{
cout << "Parent()" << endl;
}
Parent(string s)
{
cout << "Parent(string s) : " << s << endl;
}
};
class Child : public Parent
{
public:
Child()
{
cout << "Child()" << endl;
}
Child(string s) : Parent(s)
{
cout << "Child(string s) : " << s << endl;
}
};
int main()
{
/*
无参子类对象,调用子类无参构造函数,并且会默认调用父类的无参构造函数或者默认参数的构造函数
此时如果父类没有无参构造函数或者默认参数的构造函数,会编译不过。
*/
Child c;
/*
构建子类对象,调用子类有参构造函数,如果没有显式调用父类构造函数,会默认调用父类的无参构造函数或者默认参数的构造函数
如果此时父类没有无参构造函数或者默认参数的构造函数,会编译不过。如果是显示调用父类构造函数 则会调用父类的构造函数(初始化列表)
*/
Child cc("cc");
return 0;
}
mhr@ubuntu:~/work/c++$ g++ 46-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out
Parent()
Child()
Parent(string s) : cc
Child(string s) : cc
mhr@ubuntu:~/work/c++$
实验2: 子类构造深度剖析
#include <iostream>
#include <string>
using namespace std;
class Object
{
public:
Object(string s)
{
cout << "Object(string s) : " << s << endl;
}
};
class Parent : public Object
{
public:
Parent() : Object("Default")
{
cout << "Parent()" << endl;
}
Parent(string s) : Object(s)
{
cout << "Parent(string s) : " << s << endl;
}
};
class Child : public Parent
{
Object mO1;
Object mO2;
public:
Child() : mO1("Default 1"), mO2("Default 2")
{
cout << "Child()" << endl;
}
Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
{
cout << "Child(string s) : " << s << endl;
}
};
int main()
{
Child cc("cc");
return 0;
}
mhr@ubuntu:~/work/c++$
mhr@ubuntu:~/work/c++$
mhr@ubuntu:~/work/c++$ g++ 46-2.cpp
mhr@ubuntu:~/work/c++$ ./a.out
Object(string s) : cc
Parent(string s) : cc
Object(string s) : cc 1
Object(string s) : cc 2
Child(string s) : cc
mhr@ubuntu:~/work/c++$
实验3:对象的析构
#include <iostream>
#include <string>
using namespace std;
class Object
{
string ms;
public:
Object(string s)
{
cout << "Object(string s) : " << s << endl;
ms = s;
}
~Object()
{
cout << "~Object() : " << ms << endl;
}
};
class Parent : public Object
{
string ms;
public:
Parent() : Object("Default")
{
cout << "Parent()" << endl;
ms = "Default";
}
Parent(string s) : Object(s)
{
cout << "Parent(string s) : " << s << endl;
ms = s;
}
~Parent()
{
cout << "~Parent() : " << ms << endl;
}
};
class Child : public Parent
{
Object mO1;
Object mO2;
string ms;
public:
Child() : mO1("Default 1"), mO2("Default 2")
{
cout << "Child()" << endl;
ms = "Default";
}
Child(string s) : Parent(s), mO1(s + " 1"), mO2(s + " 2")
{
cout << "Child(string s) : " << s << endl;
ms = s;
}
~Child()
{
cout << "~Child() " << ms << endl;
}
};
int main()
{
Child cc("cc");
cout << endl;
return 0;
}
mhr@ubuntu:~/work/c++$
mhr@ubuntu:~/work/c++$
mhr@ubuntu:~/work/c++$ g++ 46-3.cpp
mhr@ubuntu:~/work/c++$ ./a.out
Object(string s) : cc
Parent(string s) : cc
Object(string s) : cc 1
Object(string s) : cc 2
Child(string s) : cc
~Child() cc
~Object() : cc 2
~Object() : cc 1
~Parent() : cc
~Object() : cc
mhr@ubuntu:~/work/c++$