c++prime+重虚函数设计

来源:互联网 发布:手机画画软件sketchbook 编辑:程序博客网 时间:2024/06/06 00:56
#include <string>
#include <iostream>
using namespace std;


class worker{
public:
int id;
string name;
worker(int id1=0,string name1=" ",float salary1=0.0):id(id1),name(name1),salary(salary1) {}
virtual void get_salary(int id)
{
cout<<salary<<endl;
}
protected:
float salary;
};


class tem: public worker{
public:
tem(int id1=0,string name1=" ",float salary1=0.0,int level1 = 0):worker(id1,name1,salary1),level(level1) {}
void get_salary(int id) =0;
protected:
int level;
};


class worker_d: public tem{
public:
worker_d(int id1=0,string name1=" ",float salary1=0.0,int level1 = 0):tem(id1,name1,salary1,level1){ }
void get_salary(int id)
{
cout<<id<<"   "<<level<< "    "<<salary;  
}

};

#include "worker.h"


int main()
{
worker a(0,"vivi",3000);
worker_d b(1,"cucu",5000,1);
tem c(2,"cucu",5000,1);
a.get_salary(0);
b.get_salary(1);
}

g++ -o main main.cpp

main.cpp: In function ‘int main()’:
main.cpp:7: error: cannot declare variable ‘c’ to be of abstract type ‘tem’
worker.h:18: note:   because the following virtual functions are pure within ‘tem’:  //纯虚函数
worker.h:21: note: virtual void tem::get_salary(int)

原创粉丝点击