到底要怎么做?

tortic 2004-03-13 10:25:09
小弟才学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;
}
程序很简单,是书上的例程。但怎么也运行不了,到底是为什么?
还有高手请顺便谈谈,怎么学才比较快!
...全文
24 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
angelo23 2004-03-13
  • 打赏
  • 举报
回复
呃~和编译器无关的。""是从当前目录开始搜索,而<>则直接从include 文件夹搜索,你的employ2.h是和main.cpp放在一个工作目录下而不是一个库文件,当然要用""
柯本 2004-03-13
  • 打赏
  • 举报
回复
我用dev-cpp,二个问题
1.//===============employe2.cpp==================//
#include <employ2.h> 应为#include "employ2.h" /// 与编译器有关
2.//==============boss1.cpp======================
#include "boss1.h" // 少加了
-------------------结果----------------------
boss:johnsimitheran $800
boss:johnsimith
angelo23 2004-03-13
  • 打赏
  • 举报
回复
另外如果按照标准写的话,main要声明为int型的,iostream.h改成iostream, iomanip.h改成iomanip然后用using namespace std;
angelo23 2004-03-13
  • 打赏
  • 举报
回复
几个地方要改一下(如果你是放在不同文件中的话):
employ2.h: 析构函数最好声明为virtual,因为你是把employ作为虚拟基类
employ2.cpp: #include "employ2.h"
boss1.cpp: #include "boss1.h"
dddd8888 2004-03-13
  • 打赏
  • 举报
回复
// dassdfa.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <iterator>
#include<assert.h>
using namespace std;

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;

};
//===============employe2.cpp==================//


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
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<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;
}

可以运行啊
Kernel_Don 2004-03-13
  • 打赏
  • 举报
回复
boss:johnsimitheran $800
boss:johnsimithPress any key to continue

===================
vc6.0可以运行,结果如上
Quain 2004-03-13
  • 打赏
  • 举报
回复
在main函数那个文件头上加上
#include <iostream.h>
#include <assert.h>
Jinhao 2004-03-13
  • 打赏
  • 举报
回复
这么多代码,你把编译提示写出来,OK?
tortic 2004-03-13
  • 打赏
  • 举报
回复
由于作过些调试,所以有些注释符

64,282

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧