c++ 面向对象基础Test——继承、重载、slice赋值

n

// CppTest1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <string>
#include <iostream>

#define PTLN(str) std::cout << "[" << str << "]" << "-------------------" << std::endl;
#define PT(str) std::cout << str << std::endl;

class Person
{
public:

	// 构造函数
	Person()
	{
		PT("Person()");
	}

	// 析构函数
	virtual ~Person()
	{
		PT("~Person()");
	}

	// 定义虚方法
	virtual void fun()
	{
		PT("Person::fun()");
	}

	// 定义方法
	void fun0()
	{
		PT("Person::fun0()");
	}


};

class Fan : public Person
{
public:

	// 构造函数
	Fan()
	{
		PT("Fan()");
	}

	// 析构函数
	virtual ~Fan()
	{
		PT("~Fan()");
	}

	// 方法重载
	virtual void fun() override
	{
		PT("Fan::fun()");
	}

	// 方法覆盖
	void fun0()
	{
		PT("Fan::fun0()");
	}

};

int _tmain(int argc, _TCHAR* argv[])
{
	
	Fan* f1 = new Fan();

	PTLN(0)

	f1->fun0();

	f1->fun();

	PTLN(1) //------------------

	Person* p1 = f1;

	p1->fun0();

	p1->fun();

	PTLN(2) //------------------

	Person p2 = *f1;

	(&p2)->fun0();

	(&p2)->fun();

	PTLN(3) //------------------

	delete f1;

	system("pause");

	return 0;
}

运行结果:

Person()
Fan()
[0]-------------------
Fan::fun0()
Fan::fun()
[1]-------------------
Person::fun0()
Fan::fun()
[2]-------------------
Person::fun0()
Person::fun()
[3]-------------------
~Person()
~Fan()
~Person()
请按任意键继续. . .



n

你可能感兴趣的:(C++)