C++问题,求解如何修改,要输出时能有如图结果
#include<iostream>#include<string>usingnamespacestd;classperson{public:person(intyear...
#include<iostream>
#include<string>
using namespace std;
class person
{
public:
person(int year,int month,int day,string name,char sex,d1):date(year,month,date)
{
n=name;
s=sex;
}
void showperson()
{
cout<<"姓名"<<"\t"<<"性别"<<"\t"<<"出生年月"<<endl;
cout<<n<<"\t"<<s<<"\t"<<y<<"/"<<m<<"/"<<d<<endl;
}
protected:
string n;
char s;
date d1;
};
class date
{
public:
date(int year,int month,int day)
{
y=year;
m=month;
d=day;
}
void showday()
{
cout<<y<<"/"<<m<<"/"<<d;
}
protected:
int y;
int m;
int d;
};
class worker:public person
{
public:
worker(int year,int month,int day,string name,char sex,int num,int money):person(year,month,day,name,sex)
{
n1=num;
m1=money;
}
void showworker()
{
cout<<"姓名"<<"\t"<<"性别"<<"\t"<<"出生年月"<<"\t"<<"职工号"<<"\t"<<"工资"<<endl;
cout<<n<<"\t"<<s<<"\t"<<y<<"/"<<m<<"/"<<d<<"\t"<<n1<<"\t"<<m1<<endl;
}
int getmoney()
{
return m1;
}
protected:
int n1;
int m1;
int s1;
};
int main()
{
int sum;
worker w1(1994,12,15,"李丽",'f',10030,2700);
w1.showworker();
worker w2(1990,7,16,"王芳",'f',10035,3500);
w2.showworker();
worker w3(1991,8,25,"李明",'m',10055,5500);
w3.showworker();
sum=w1.getmoney()+w2.getmoney()+w3.getmoney();
cout<<"工资总额为:"<<sum<<endl;
}
error C2629: unexpected 'class person ('
error C2334: unexpected token(s) preceding ':'; skipping apparent function body
error C2146: syntax error : missing ';' before identifier 'd1'
error C2501: 'date' : missing storage-class or type specifiers
: error C2501: 'd1' : missing storage-class or type specifiers
: warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错. 展开
#include<string>
using namespace std;
class person
{
public:
person(int year,int month,int day,string name,char sex,d1):date(year,month,date)
{
n=name;
s=sex;
}
void showperson()
{
cout<<"姓名"<<"\t"<<"性别"<<"\t"<<"出生年月"<<endl;
cout<<n<<"\t"<<s<<"\t"<<y<<"/"<<m<<"/"<<d<<endl;
}
protected:
string n;
char s;
date d1;
};
class date
{
public:
date(int year,int month,int day)
{
y=year;
m=month;
d=day;
}
void showday()
{
cout<<y<<"/"<<m<<"/"<<d;
}
protected:
int y;
int m;
int d;
};
class worker:public person
{
public:
worker(int year,int month,int day,string name,char sex,int num,int money):person(year,month,day,name,sex)
{
n1=num;
m1=money;
}
void showworker()
{
cout<<"姓名"<<"\t"<<"性别"<<"\t"<<"出生年月"<<"\t"<<"职工号"<<"\t"<<"工资"<<endl;
cout<<n<<"\t"<<s<<"\t"<<y<<"/"<<m<<"/"<<d<<"\t"<<n1<<"\t"<<m1<<endl;
}
int getmoney()
{
return m1;
}
protected:
int n1;
int m1;
int s1;
};
int main()
{
int sum;
worker w1(1994,12,15,"李丽",'f',10030,2700);
w1.showworker();
worker w2(1990,7,16,"王芳",'f',10035,3500);
w2.showworker();
worker w3(1991,8,25,"李明",'m',10055,5500);
w3.showworker();
sum=w1.getmoney()+w2.getmoney()+w3.getmoney();
cout<<"工资总额为:"<<sum<<endl;
}
error C2629: unexpected 'class person ('
error C2334: unexpected token(s) preceding ':'; skipping apparent function body
error C2146: syntax error : missing ';' before identifier 'd1'
error C2501: 'date' : missing storage-class or type specifiers
: error C2501: 'd1' : missing storage-class or type specifiers
: warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错. 展开
2个回答
展开全部
问题较多,已修改
#include<iostream>
#include<string>
using namespace std;
class date //类定义要在引用之前,或在引用前先声明
{
public:
date(int year,int month,int day)
{
y=year;
m=month;
d=day;
}
void showday()
{
cout<<y<<"/"<<m<<"/"<<d;
}
protected:
int y;
int m;
int d;
};
class person
{
public:
person(int year,int month,int day,string name,char sex ):d1(year,month,day) //这句问题较多,自己比照看看
{
n=name;
s=sex;
}
void showperson()
{
cout<<n<<"\t"<<s<<"\t";
d1.showday() ; //无法直接引用date保护成员变量,只能调用这个函数
}
protected:
string n;
char s;
date d1;
};
class worker:public person
{
public:
worker(int year,int month,int day,string name,char sex,int num,int money):person(year,month,day,name,sex)
{
n1=num;
m1=money;
}
void showworker() //同上情况
{
showperson() ;
cout<<"\t"<<n1<<"\t"<<m1<<endl;
}
int getmoney()
{
return m1;
}
protected:
int n1;
int m1;
int s1;
};
int main()
{
int sum;
worker w1(1994,12,15,"李丽",'f',10030,2700);
worker w2(1990,7,16,"王芳",'f',10035,3500);
worker w3(1991,8,25,"李明",'m',10055,5500);
cout<<"姓名"<<"\t"<<"性别"<<"\t"<<"出生年月"<<"\t"<<"职工号"<<"\t"<<"工资"<<endl; //这个不能放到类函数中,不然会输出多次
w1.showworker();
w2.showworker();
w3.showworker();
sum=w1.getmoney()+w2.getmoney()+w3.getmoney();
cout<<"工资总额为:"<<sum<<endl;
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询