到底要怎么做?
小弟才学C++没几天,搞个程序怎么也搞不定,那位朋友帮忙看看,感激不尽。
//==========employ2.h==================
#ifndef employ2_h
#define employ2_h
class employee
{
public:
employee(const char *,const char *);
~employee();
const char* getfirstname() const;
const char* getlastname() const;
virtual float earnings() const=0;
virtual void print() const=0;
private:
char *firstname;
char *lastname;
};
#endif
//===============employe2.cpp==================//
#include<employ2.h>
#include<string.h>
#include<iostream.h>
#include<assert.h>
employee::employee(const char *first,const char *last)
{
firstname=new char [strlen(first)+1];
assert(firstname!=0);
strcpy(firstname,first);
lastname=new char [strlen(last)+1];
assert(lastname!=0);
strcpy(lastname,last);
}
employee::~employee()
{delete []firstname;
delete []lastname;
}
const char* employee::getfirstname()const
{return firstname;}
const char* employee::getlastname()const
{return lastname;}
//================boss1.h========================
//#ifndef boss1_h
//#define boss1_h
#include<iostream.h>
#include "employ2.h"
class boss: public employee
{public:
boss(const char*,const char*,float=0.0);
void setweeklysalary(float);
virtual float earnings() const;
virtual void print() const;
private:
float weeklysalary;
};
//#endif
//==============boss1.cpp======================
boss::boss(const char *first,const char*last,float s)
:employee(first,last)
{weeklysalary=s>0?s:0;}
void boss::setweeklysalary(float s)
{weeklysalary=s>0?s:0;}
float boss::earnings()const
{return weeklysalary;}
void boss::print()const
{cout<<"\nboss:"<<getfirstname()<<""<<getlastname();
}
//=======================main=============================
#include<iostream.h>
//#include<iomanip.h>
#include"employ2.h"
#include"boss1.h"
//#include<commisi.h>
//#include<piece1.h>
main()
{
//cout<<setiosflages(ios::showpoint)<<setprecision(2)//
employee *ptr;
boss b("john","simith",800);
ptr=&b;
ptr->print();
cout<<"eran $"<<ptr->earnings();
b.print();
b.earnings();
return 0;
}
程序很简单,是书上的例程。但怎么也运行不了,到底是为什么?
还有高手请顺便谈谈,怎么学才比较快!