C++学习-虚函数和多态

------------------------------------------------------------------------------------------------------------------------------------------------

//虚函数的声明方式 virtual void show(){}

#include <iostream>
using namespace std;
class Base
{
public:
	Base(int year = 2016) :year(year){}
	virtual void show()
	{
		cout <<"year:"<< year << endl;
	}
protected:
	int year;
};
class Date :public Base
{
public:
	Date(int month=10) :month(month){}
	void show()
	{
		cout << "date:" << month << endl;
	}
protected:
	int month;
};
//没有虚函数 : 子类中的同名函数会覆盖掉父类中同名函数
//虚函数  无论被继承多少次,都是虚函数
int main()
{

	//对象.成员函数 不存在覆盖
	//派生类指针---调用基类的同名成员函数
	//1.作用域限定指定基类成员
	//正常情况的调用
	Base *pB,object; 
	pB = &object;
	object.show();   //基类对象调用基类成员
	pB->show();		 //基类成员调用基类成员

	//不正常的情况下
	//基类指针调用,但是调用子类成员
	//多态体现之一,1.子类对象初始化父类指针
	//			   2.需要virtual函数
	Base *pD = new Date;  //通过子类初始化基类指针
	pD->show();      //基类指针调用同名函数 如果是虚函数,调用子类中的

	//多态体现之二:引用  ---要满足子类指针初始化基类指针
	Date Dobject2;
	Base *pB2 = &Dobject2;
	pB2->show();  //基类指针调用虚函数

	//dynamic_cast 
	system("pause");

	return 0;
}

------------------------------------------------------------------------------------------------------------------------------------------------



------------------------------------------------------------------------------------------------------------------------------------------------


#include <iostream>
using namespace std;
class A
{
public:
	A(int a = 0) :a(a){}
	void print()
	{
		cout << a << endl;
	}
private:
	int a;
};
class B:public A
{
public:
	B(int b = 0,int a=1):A(a),b(b){}
private:
	int b;
	//int a;   //类中的成员只能通过构造函数初始化
	//print()
};
void PrintA(A &a)
{
	//print()
	a.print();
}


int main()
{
	//普通的用法
	A a;
	PrintA(a);
	//特殊的情况 ----继承过程,形参是父类对象,子类对象当作父类对象处理
	// b(b,a)
	B b(1,2);   //构造对象就是调用构造函数的过程
	//  :A(a),b(b) ------a.a=2; b.b=1
	PrintA(b);
	system("pause");
	return 0;
}
- ------------------------------------------------------------------------------- ----------------------------------------------------------------


//没有函数体的函数  就叫做纯虚函数 
//拥有纯虚函数的类就叫做抽象类
//抽象类:不能创建对象,一般都是用做接口,或者说是当作架构作用
//纯虚函数的写法: virtual 函数 =0;
#include<iostream>
using namespace std;
//ADT   抽象数据类型  abstract data type
class stack
{
public:
	virtual ~stack(){}    //析构函数没有纯虚析构函数
	virtual int size() const = 0;
	virtual bool empty() const = 0;
	virtual void push(int &theElement) = 0;
	virtual void pop() = 0;
	virtual int top() const = 0;
};

//抽象类被继承的时候 :纯虚函数必须要被实现 
class ArrayStack :public stack
{
public:
	int size() const{ return 0; }
	bool empty() const{ return 0; }
	void push(int &theElement){}
	void pop(){}
	int top() const{ return 0; }
	void output()
	{ 
		cout << 1 << endl; 
	}
};
int main()
{
	//创建指针
	stack *mystack = new ArrayStack;
	int a = 1;
	mystack->push(a);
	mystack->push(a);
	mystack->push(a);
	mystack->push(a);
	while (mystack->empty())
	{
		cout<<mystack->top()<<endl;
		mystack->pop();
	}
	system("pause");
	return 0;
}

- ------------------------------------------------------------------------------- ----------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------------------------------


#include<iostream>
using namespace std;
class B
{
public:
	 virtual ~B()   //虚析构函数
	{
		cout << "~B~B()" << endl;
	}
};

class D :public B
{
public:
	~D()  
	{
		cout << "~D::D~()" << endl;
	}
};
int main()
{
	//当你在继承中使用子类对象初始化基类指针,这个基类析构函数要为virtual函数
	{
		B *p = new D;
		delete p;
	}
	system("pause");
	return 0;
}
- ------------------------------------------------------------------------------- ----------------------------------------------------------------


------------------------------------------------------------------------------------------------------------------------------------------------

猜你喜欢

转载自blog.csdn.net/ydpawx/article/details/72392149