纯虚函数和抽象类

语法格式:

virtual 返回值类型 函数名 (函数参数) = 0;

 

含有纯虚函数的类为抽象类。

 

注意点:

抽象类不能常见对象:因为纯虚数没有函数体,不是完整的函数,无法调用,因此无法实例化。

抽象函数一般用来作为基类,让派生类去实现虚函数。

 

在这个抽象类中,如果有其他不是虚函数的成员函数,那么只要定义一个基类指针指向派生类,就可以直接调用这些正常的成员函数。

抽象类与虚函数的说明:

1) 一个纯虚函数就可以使类成为抽象基类,但是抽象基类中除了包含纯虚函数外,还可以包含其它的成员函数(虚函数或普通函数)和成员变量。

2) 只有类中的虚函数才能被声明为纯虚函数,普通成员函数和顶层函数均不能声明为纯虚函数。

多态实例代码:

 

//主程序
#include <iostream>
#include <string>
using namespace std;

#include "allclass.h"
int main()
{
	Employee *p[4];
	cout << "小时工" << endl;
	p[0] = new Technician();
	cout << p[0]->getname() << " 工号:" << p[0]->getnum() << " 薪水:" << p[0]->getsalary() << endl;
	cout << "销售人员:" << endl;
	p[1] = new Salesman();
	cout << p[1]->getname() << " 工号:" << p[1]->getnum() << " 薪水:" << p[1]->getsalary() << endl;
	cout << "管理员:" << endl;
	p[2] = new Manager();
	cout << p[2]->getname() << " 工号:" << p[2]->getnum() << " 薪水:" << p[2]->getsalary() << endl;
	cout << "销售经理" << endl;
	p[3] = new SaleManger();
	cout << p[3]->getname() << " 工号:" << p[3]->getnum() << " 薪水:" << p[3]->getsalary() << endl;



	system("pause");
}

//.h头文件
#include <iostream>
using namespace std;

class Employee 
{
public:
	 Employee();
	 virtual void setname();
	 virtual void setnum();
     virtual  string getname();
	 virtual string  getnum();
	 virtual double getsalary();
protected:
	string m_name;
	string m_num;
	double m_salary;
};

class Technician : public Employee
{
public:
	Technician();
	void SetWorkTime();
	void  SetWorkSalary();
	double getsalary();
private:
	double m_WorkTime;
	double m_WorkSalary;
};

class Salesman :public Employee
{
public :
	Salesman();
	virtual void SetSalePri();
	virtual void SetSalePro();
	double getsalary();
protected:
	double m_SalePri;
	double m_SalePro;
};

class Manager :public Employee
{
public:
	Manager();
	void SetFixSalary();
	double getsalary();
protected:
	double m_FixSalary;
};

class SaleManger : public Salesman
{
public:
	SaleManger();
	void SetFixSalary();
	double getsalary();
protected:
	double m_FixSalary;
};

//.cpp文件
#include "allclass.h"
#include<string>

Employee::Employee()
{
	setname();
	setnum();
}

string Employee::getname(){ return m_name; }

string Employee::getnum(){ return m_num; }

double Employee::getsalary(){ return m_salary; }


void Employee::setname()
{
	string name;
	cout << "输入姓名" << endl;
	getline(cin, name);
	m_name = name;
}

void Employee::setnum()
{
	string num;
	cout << "输入工号" << endl;
	getline(cin, num);
	m_num = num;
}

void Technician::SetWorkSalary()
{
	int salary;
	cout << "输入时薪" << endl;
	cin >> salary;
	m_WorkSalary= salary;
}

void Technician::SetWorkTime()
{
	int worktime;
	cout << "输入时长" << endl;
	cin >> worktime;
	m_WorkTime = worktime;
}

Technician::Technician()
{
	SetWorkSalary();
	SetWorkTime();
	cin.get();
}

double Technician::getsalary()
{
	m_salary = m_WorkTime*m_WorkSalary;
	return m_salary;
}

Salesman::Salesman()
{
	SetSalePri();
	SetSalePro();
	cin.get();
}

void Salesman::SetSalePri()
{
	cout << "输入销售额" << endl;
	cin >> m_SalePri;
}

void Salesman::SetSalePro()
{
	cout << "输入销售比例" << endl;
	cin >>m_SalePro;
}

double Salesman::getsalary()
{
	m_salary = m_SalePro*m_SalePri;
	return m_salary;
}

Manager::Manager()
{
	SetFixSalary();
	cin.get();
}

void Manager::SetFixSalary()
{
	cout << "固定薪资" << endl;
	cin >> m_FixSalary;
}

double Manager::getsalary()
{
	m_salary = m_FixSalary;
	return m_salary;
}

SaleManger::SaleManger()
{
	SetFixSalary();
	cin.get();
}

void SaleManger::SetFixSalary()
{
	cout << "固定薪资" << endl;
	cin >> m_FixSalary;
}

double SaleManger::getsalary()
{
	m_salary = m_FixSalary+Salesman::getsalary();
	return m_salary;
} 

阅读更多
个人分类: C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭