| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 478 人关注过本帖
标题:急!这个类编译不报错,执行却出了大问题。
只看楼主 加入收藏
q236763612
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2008-10-22
收藏
 问题点数:0 回复次数:1 
急!这个类编译不报错,执行却出了大问题。
以下代码中我定义了一个Manager类,是继承的Employee类的。然后动态申请了一个Manager类的数组。可是在准备将数组成员按工资的多少从新排序时,编译没报错,执行却出了问题。不能输出排序结果,而且弹出C++ 2005 出错将关闭的窗口。
相关代码如下!希望大家能在不妥之错指正。





#include<string>
#include<iostream>
#include<cassert>
using namespace std;
class Employee
{protected:
    string name;
    int     ID;
public:
    Employee(char* a, int b)
        :name(a),ID(b)
    {}
    Employee (const Employee& s)
    {
        assert(this!=& s);
        name=s.name;
        ID=s.ID;}
Employee& operator=(const Employee& rhs)
{
    assert(this!=&rhs);
    name=rhs.name;
    ID=rhs.ID;
return *this;
}
void  print()
{ cout<<name<<ID<<endl;}

};
class Manager:public  Employee
{
private:
    int sal;
public:
    Manager(char*a=" ", int b =0, int c =0)
        :Employee(a,b),
        sal(c)
    {}
Manager(const Manager& s)
:Employee(s)
{ sal=s.sal;}
Manager& operator=(const Manager& rhs)
{   ID=rhs.ID;
   name=rhs.name ;

    sal=rhs.sal;
    return *this;}
void print() {cout<<name<<'\t'<<ID<<"号"<<sal<<"圆"<<endl;}
void SetManager(){  cin>>name ; cin>>ID; cin>>sal;}
friend int showsal( const Manager& s)
{ return  s.sal ;}
friend  string  showname (const Manager& t)
{ return   t.name ;}

};
int main ()
{   double  p=0;  int sum=0;
     Manager t;
    Manager *s= new Manager[5];
  assert(s !=NULL);
  cout<<"please enter 5 elements"<<endl;
for(int i=0;i!=5;++i)
{s[i].SetManager ();}
cout<<"finished"<<endl;
for(int i=0;i!=5;++i)
{s[i].print();}
for (int i=0;i!=5;++i)
{
sum=sum+showsal(s[i]);}
  p=sum/5;
  cout<<"平均工资为:"<<p<< "圆"<<endl;
cout<<"工资比较高的人为:"<<endl;
for(int i=0;i!=5;++i)
{
    if( showsal(s[i])>=p)
        cout<<showname(s[i])<<endl;
}
for( int i=0;i!=5;++i)
for(int j=i+1;i!=5;++i)
if(showsal(s[i])>showsal(s[j]))  //这里是排序代码
  {t=s[i];
    s[i]=s[j];
    s[j]=t;}
for(int i=0;i!=5;++i) // 排序后从新输出数组成员
{s[i].print();}

delete []s;

}
搜索更多相关主题的帖子: 编译 
2008-11-08 13:48
debroa723
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:23
帖 子:862
专家分:1954
注 册:2008-10-12
收藏
得分:0 
for( int i=0;i!=5;++i)
for(int j=i+1;i!=5;++i)
2008-11-08 23:53
快速回复:急!这个类编译不报错,执行却出了大问题。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014179 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved