VC6.0中编译运行都成功,但使其输出结果时EXE文件停止运行
#include<iostream>#include<string>#include<iomanip>usingnamespacestd;//--------------...
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//----------------------
class Employee{
public:
string name;
string old;
string sex;
string number;
Employee(string na=0,string o=0,string s=0,string num=0);
};
Employee::Employee(string na,string o,string s,string num)
{
name=na;
old=o;
sex=s;
number=num;
}
//-------------------------------
class Manager:public Employee{
string title;
string money;
public:
Manager(string t=0,string m=0);
void print();
};
Manager::Manager(string t,string m)
{
title=t;
money=m;
}
void Manager::print()
{
cout<<"name:"<<name<<'\n';
cout<<"old:"<<old<<'\n';
cout<<"sex:"<<sex<<'\n';
cout<<"number:"<<number<<'\n';
cout<<"title:"<<title<<'\n';
cout<<"money:"<<money<<'\n';
}
//---------------------------------------------
void main()
{
Employee f("兰","100","girl","2010214511");
Manager f1("CEO","三十万");
f1.print();
}
//-----------------------------------------------------
急求解答!!!!!!!!!!!!!! 展开
#include<string>
#include<iomanip>
using namespace std;
//----------------------
class Employee{
public:
string name;
string old;
string sex;
string number;
Employee(string na=0,string o=0,string s=0,string num=0);
};
Employee::Employee(string na,string o,string s,string num)
{
name=na;
old=o;
sex=s;
number=num;
}
//-------------------------------
class Manager:public Employee{
string title;
string money;
public:
Manager(string t=0,string m=0);
void print();
};
Manager::Manager(string t,string m)
{
title=t;
money=m;
}
void Manager::print()
{
cout<<"name:"<<name<<'\n';
cout<<"old:"<<old<<'\n';
cout<<"sex:"<<sex<<'\n';
cout<<"number:"<<number<<'\n';
cout<<"title:"<<title<<'\n';
cout<<"money:"<<money<<'\n';
}
//---------------------------------------------
void main()
{
Employee f("兰","100","girl","2010214511");
Manager f1("CEO","三十万");
f1.print();
}
//-----------------------------------------------------
急求解答!!!!!!!!!!!!!! 展开
3个回答
展开全部
把你的参数0 全改为“0” 就行了, 参数是 string型,不能赋给它int型的。改完之后如下(可以不看,只是做如上的改动):
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//----------------------
class Employee{
public:
string name;
string old;
string sex;
string number;
Employee(string na="0",string o="0",string s="0",string num="0");
};
Employee::Employee(string na,string o,string s,string num)
{
name=na;
old=o;
sex=s;
number=num;
}
//-------------------------------
class Manager:public Employee{
string title;
string money;
public:
Manager(string t="0",string m="0");
void print();
};
Manager::Manager(string t,string m)
{
title=t;
money=m;
}
void Manager::print()
{
cout<<"name:"<<name<<'\n';
cout<<"old:"<<old<<'\n';
cout<<"sex:"<<sex<<'\n';
cout<<"number:"<<number<<'\n';
cout<<"title:"<<title<<'\n';
cout<<"money:"<<money<<'\n';
}
//---------------------------------------------
void main()
{
Employee f("兰","100","girl","2010214511");
Manager f1("CEO","三十万");
f1.print();
}
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
//----------------------
class Employee{
public:
string name;
string old;
string sex;
string number;
Employee(string na="0",string o="0",string s="0",string num="0");
};
Employee::Employee(string na,string o,string s,string num)
{
name=na;
old=o;
sex=s;
number=num;
}
//-------------------------------
class Manager:public Employee{
string title;
string money;
public:
Manager(string t="0",string m="0");
void print();
};
Manager::Manager(string t,string m)
{
title=t;
money=m;
}
void Manager::print()
{
cout<<"name:"<<name<<'\n';
cout<<"old:"<<old<<'\n';
cout<<"sex:"<<sex<<'\n';
cout<<"number:"<<number<<'\n';
cout<<"title:"<<title<<'\n';
cout<<"money:"<<money<<'\n';
}
//---------------------------------------------
void main()
{
Employee f("兰","100","girl","2010214511");
Manager f1("CEO","三十万");
f1.print();
}
追问
恩恩~是运行成功了没错~
可是为什么父类的自定义构造函数失效了呢?
追答
看样子楼主对类还没理解的很透彻,这里父类的构造函数并没有失效,只是你 f1.print() 函数只是输出了f1这个对象的相关信息,而在f1中楼主并没有对f1对象的父类的参数属性赋值,所以输出的就是0了而不是楼主想要的兰 100 等等了,为验证父类构造函数没有失效楼主可以在父类构造函数中加入如下代码:
cout<<name<<" "<<old<<" "<<sex<<" "<<number<<'\n';
也就是如下位置:
Employee::Employee(string na,string o,string s,string num)
{
name=na;
old=o;
sex=s;
number=num;
cout<<name<<" "<<old<<" "<<sex<<" "<<number<<'\n';
}
不知楼主听明白没,没明白可以继续问,也欢迎到小磊客栈来讨论。
参考资料: 小磊客栈
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询