首页 新闻 会员 周边 捐助

C++编程问题,求解答。

0
悬赏园豆:10 [已解决问题] 解决于 2012-11-18 12:46

我想用showall函数来显示所有人员的信息,但是编译出错。还有怎样才可以以文件方式将输入的各类人员信息进行保存。求各位大神指点。

代码如下:

 

#include <iostream>
#include <string>
using namespace std;

class employee                                                  //声明基类职工类employee
{public:                                             
protected:                                                          
 string number;
 string name;
 int age;
 string sex;                                                  
 int grade;                                                            //以上五行为私有数据成员
};

class technician:public employee                         
{public:
 void input1();
void show1();
float data1()
{float worktime;
cout<<"please enter the worktime:"<<endl;
cin>>worktime;
wages=worktime*100;
return 0;
}
 protected:
float wages;
};
   
 void technician::input1()                 //用于输入技术员的信息
 {cout<<"you choice the technicians input"<<endl;
 cout<<"please enter the technician's number:"<<endl;
 cin>>number;
    cout<<"please enter the technician's name:"<<endl;
 cin>>name;
 cout<<"please enter the technician's age:"<<endl;
 cin>>age;
 cout<<"please enter the technician's sex:"<<endl;
 cin>>sex;
 cout<<"please enter the technician's grade:"<<endl;
 cin>>grade;
 }
 void technician::show1()                //用于输出技术员的信息
 {cout<<"number:"<<number<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"sex:"<<sex<<endl;
 cout<<"position:technician"<<endl;
 cout<<"wages:"<<wages<<endl;
 cout<<"grade:"<<grade<<endl;
 }


class salesman:public employee                                  //声明销售员类salesman,公有继承职工基类employee
{public:
void input2();
void show2();
float data2()                                                   //定义计算销售员工资的函数
{float salesvalume;                                              
cout<<"please enter the salesvalume:"<<endl;                          //从键盘上输入销售额
cin>>salesvalume;
wages=salesvalume*0.04;
return 0;
}
protected:
 float wages;                                                //wages为保护成员
};

void salesman::input2()
{cout<<"you choice the salesmans input"<<endl;
 cout<<"please enter the salesman's number:"<<endl;
 cin>>number;
    cout<<"please enter the salesman's name:"<<endl;
 cin>>name;
 cout<<"please enter the salesman's age:"<<endl;
 cin>>age;
 cout<<"please enter the salesman's sex:"<<endl;
 cin>>sex;
 cout<<"please enter the salesman's grade:"<<endl;
 cin>>grade;
}
void salesman::show2()                                       //在类外定义成员函数show2,用于输出salesman的相关信息
{cout<<"number:"<<number<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"sex:"<<sex<<endl;
 cout<<"position:salesman"<<endl;
    cout<<"wages:"<<wages<<endl;
 cout<<"grade:"<<grade<<endl;
}

class manager:public employee
{public:
void input3();
void show3();
float data3()
{wages=8000;
return 0;
}
protected:
 float wages;
};

void manager::input3()                 //用于输入经理的信息
 {cout<<"you choice the managers input"<<endl;
 cout<<"please enter the manager's number:"<<endl;
 cin>>number;
    cout<<"please enter the manager's name:"<<endl;
 cin>>name;
 cout<<"please enter the manager's age:"<<endl;
 cin>>age;
 cout<<"please enter the manager's sex:"<<endl;
 cin>>sex;
 cout<<"please enter the manager's grade:"<<endl;
 cin>>grade;
 }
 void manager::show3()                //用于输出经理的信息
 {cout<<"number:"<<number<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"sex:"<<sex<<endl;
 cout<<"position:manager"<<endl;
 cout<<"wages:"<<wages<<endl;
 cout<<"grade:"<<grade<<endl;
 }

class salesmangaer:public employee
{public:
void input4();
void show4();
float data4()
{float salesvalume;
cout<<"please enter the salesvalume:";                          //从键盘上输入销售额
cin>>salesvalume;
wages=salesvalume*0.005+5000;
return 0;
}
private:
 float wages;

};

void salesmangaer::input4()                 //用于输入经理的信息
 {cout<<"you choice the salesmangaers input"<<endl;
 cout<<"please enter the salesmangaer's number:"<<endl;
 cin>>number;
    cout<<"please enter the salesmangaer's name:"<<endl;
 cin>>name;
 cout<<"please enter the salesmangaer's age:"<<endl;
 cin>>age;
 cout<<"please enter the salesmangaer's sex:"<<endl;
 cin>>sex;
 cout<<"please enter the salesmangaer's grade:"<<endl;
 cin>>grade;
 }
 void salesmangaer::show4()                //用于输出经理的信息
 {cout<<"number:"<<number<<endl;
    cout<<"name:"<<name<<endl;
    cout<<"age:"<<age<<endl;
    cout<<"sex:"<<sex<<endl;
 cout<<"position:salesmangaer"<<endl;
 cout<<"wages:"<<wages<<endl;
 cout<<"grade:"<<grade<<endl;
 }


int add1()
{salesman salem;
salem.input2();
salem.data2();
salem.show2();
return 0;
}
 
int add2()
{technician techni;
techni.input1();
techni.data1();
techni.show1();
return 0;
}

int add3()
{manager manag;
manag.input3();
manag.data3();
manag.show3();
return 0;
}

int add4()
{salesmangaer salemanag;
salemanag.input4();
salemanag.data4();
salemanag.show4();
return 0;
}

int showall()             //显示所有人员的信息
{int j;
 for(j=1;j<=100;j++)
 salesman.show2()
technician.show1()
manager.show3()
salesmangaer.show4()
}

float new1()
{cout<<"请选择想要输入的人员:"<<endl;
 cout<<"1:销售员"<<endl;
 cout<<"2:技术员"<<endl;
 cout<<"3:经理"<<endl;
 cout<<"4:销售经理"<<endl;
 cout<<"5:显示所有人员信息"<<endl;
 int a;
 cin>>a;
 switch (a)
 {case 1:add1();break;
 case 2:add2();break;
 case 3:add3();break;
 case 4:add4();break;
 case 5:showall();break;
 }
return 0;
}


int main()
{int i;
 for(i=1;i<=100;i++)
 new1();
return 0;}

C++
Mr小区的主页 Mr小区 | 初学一级 | 园豆:61
提问于:2012-09-09 11:15
< >
分享
最佳答案
0

那几行后面没加分号。。。

收获园豆:5
向往-SONG | 老鸟四级 |园豆:4853 | 2012-09-09 12:39

加了error更多。。。

Mr小区 | 园豆:61 (初学一级) | 2012-09-09 12:46

@Mr小区: 

int j;
 for(j=1;j<=100;j++){
     salesman sm;
     sm.show2();
     technician tm;
     tm.show1();
     manager mer;
     mer.show3();
     salesmangaer smer;
     smer.show4();
 }
     return 0;

 这样。

向往-SONG | 园豆:4853 (老鸟四级) | 2012-09-09 13:08

@向往-SONG: 这样改了是没错,但是运行出来就是如下图,为什么呢?

Mr小区 | 园豆:61 (初学一级) | 2012-09-09 19:12

@Mr小区: 原因是在main()中你增加了100个成员,按@向往-SONG的方法,在输出时,每次都重新定义一个对象,没有初始化,就打印这个对象的信息,肯定要给出一些错误的信息。(不是该问题的解决方法:好的习惯,在写类的时候最好写上构造函数)

错误原因在于增加的100个成员是怎样存储起来的。我认为:你增加了100个成员实际上只有四个。分别为saleman/techni/manag/salemanag.其余的都是在覆盖这些对象。

还有你的每个saleman/techni/manag/salemanag对象都是add中的局部对象。在其他地方不能用(应该把声明对象的语句放在main中)。把new1()里的内容直接写到main()中用while循环,再在case中加一个退出项。

@咸鱼 | 园豆:180 (初学一级) | 2012-09-28 10:10
其他回答(3)
0

定义的对象捏?

gooner | 园豆:204 (菜鸟二级) | 2012-09-09 13:40
0

你的几个子类的input都有相同的功能,可以把这些方法放到父类中

diplomacy | 园豆:208 (菜鸟二级) | 2012-09-10 09:04
0

你这个例子 父类 和子类目标很明确,你像有一些方法可以让基类声明,子类继承父类并将其方法实现就好了,这样才体现继承和多态性啊,你比如 input  函数 和  show函数 和data函数都可以写成这样的形式啊.

 

简单的举个例子:

class A

{
 public:

      A();

     virtual ~A();

public:

      virtual void inPut();

  virtual void data();

  virtual void showMsg() const  = 0;

 

};

 

class B:public A{};

 

class C:public:A{};

 

class D:public:A{};

 

让子类具体实现父类的方法就可以了,这叫重写(覆盖);

 

只是给你一个建议,如果有哪里不对请指出,希望对你有帮助。

收获园豆:5
励志 | 园豆:220 (菜鸟二级) | 2012-09-14 13:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册