继承中的构造,初始化父类语法

继承中的构造,初始化父类语法:

//employee.h
#pragma once
#include<iostream>
class Employee
{
public:
	Employee();
	Employee(const long, const char*);
	virtual ~Employee(){}

	const char*getName()const;
	const long getNumber()const;
	virtual double earnings()const = 0;
	virtual void print() const = 0;
protected:
	long number;
	const char *name;
};
//employee.cpp
#include"employee.h"
#include<iostream>

Employee::Employee()
{
	this->number = 0;
	this->name = NULL;
}

Employee::Employee(const long number, const char*name)
{
	this->number = number;
	this->name = name;
}


//pieceWorker.h
#pragma once
#include"employee.h"

class PieceWorker :public Employee
{
public:
	//PieceWorker():Employee();//错误!Employee应该在定义中
	PieceWorker();
	PieceWorker(const long number, const char*name, double wagePerPiece = 0.0, int quantity = 0);
	~PieceWorker(){}
	void setWage(double);
	void setQuantity(int);
	virtual double earnings() const;
	virtual void print() const;
private:
	double wagePerPiece;
	int quantity;
};


//pieceWorker.cpp
#include"pieceworker.h"
#include<iostream>
using namespace std;

PieceWorker::PieceWorker() :Employee()
{
	this->wagePerPiece = 0.0;
	this->quantity = 0;
}

PieceWorker::PieceWorker(const long number, const char*name, double wagePerPiece, int quantity) :Employee(number,name)
{
	this->wagePerPiece = wagePerPiece;
	this->quantity = quantity;
}
void PieceWorker::setWage(double wagePerPiece)
{
	this->wagePerPiece = wagePerPiece;
}
void PieceWorker::setQuantity(int quantity)
{
	this->quantity = quantity;
}
double PieceWorker::earnings() const
{
	return wagePerPiece*quantity;
}
void PieceWorker::print() const
{
	cout << "Number:" << number << "  Name:" << name << "  Salary:" << earnings() << endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值