【C++】面向对象编程(二)

默认情形下,成员函数的解析都是编译时静态进行。如果要让成员函数的解析在程序运行时动态进行,需要在成员函数的声明前加上关键字virtual:

//LibMat声明表示,其析构函数和print()函数皆为虚函数
class LibMat{
public:
	LibMat()
	{
		cout<<"LibMat::LibMat() default constructor!\m";
	}
	virtual ~LibMat()
	{
		cout<<"LibMat::~LibMat() destructor!\n";
	}
	virtual void print()const
	{
		cout<<"LibMat::print()--I am a LibMat object!\n";
	}
};

虚函数的作用:
用基类的指针指向不同的派生类的对象时,基类指针调用其虚成员函数,会调用真正指向对象的成员函数,而不是基类中定义的成员函数;若不是虚函数,则只会调用基类中定义的那个函数。

void print(const LibMat &mat)
{
	cout<<"in global print():about to print mat.print()\n";
	//下一行会依据mat实际指向的对象
	//解析该执行哪一个print()成员函数
	mat.print();
}
//main函数中重复调用print()
//并依次将三个对象作为参数传递给它
//每次执行
int main()
{
	cout<<"\n"<<"Creating a LibMat object to print()\n";
	LibMat libmat;
	print(libmat);
	
	cout<<"\n"<<"Creating a Book object to print()\n";
	Book b("The Castle","Franz Kafka");
	print(b);

	cout<<"\n"<<"Creating an AudiBook = object to print()\n";
	AudioBook ab("Man without Qualities","Robert Musil","Kenneth Meyer");
	print(ab);
}
class Book : public LibMat {  //定义派生类Book,继承自LibMat
public:
	Book( const string &title, const string &author )
		: _title( title ), _author( author ){
		cout << "Book::Book( " << _title
			 << ", " << _author << " )  constructor\n";
	}

	~Book(){
		cout << "Book::~Book() destructor!\n";
	}

	virtual void print() const {
		cout << "Book::print() -- I am a Book object!\n"
			 << "My title is: " << _title << '\n'
			 << "My author is: " << _author << endl;
	}

	const string& title() const { return _title; }
	const string& author() const { return _author; }

protected:
	string _title;
	string _author;
};

被声明为protected的所有成员都可以被派生类直接访问;除了派生类之外,都不得直接访问protected成员。

class AudioBook : public Book {
public:
	AudioBook( const string &title,
		       const string &author, 
		       const string &narrator )
		: Book( title, author ), _narrator( narrator )
	{
		cout << "AudioBook::AudioBook( " << _title
			 << ", " << _author
			 << ", " << _narrator
			 << " )  constructor\n";
	}

	~AudioBook(){
		cout << "AudioBook::~AudioBook() destructor!\n";
	}

	virtual void print() const {
		cout << "AudioBook::print() -- I am a AudioBook object!\n"
			 << "My title is: " << _title << '\n'
			 << "My author is: " << _author << '\n'
			 << "My narrator is: " << _narrator << endl;
	}

	const string& narrator() const { return _narrator; }

protected:
	string _narrator;
};

派生类的构造函数作用时顺序:
基类的构造函数、派生类的析构函数、基类的析构函数。

你可能感兴趣的:(C++基础,c++,开发语言,笔记)