大家帮忙看一下,为什么参数没有传入

lllwwjj123 2010-04-07 05:45:50

//4、定义一个基类Person,从它派生出类Student和类Worker,再由类Student和Worker共同派生出WorStu。
//类Person的属性有姓名、年龄、性别,操作有设置姓名、年龄、性别和显示信息等。类Student的属性增加
//系别、专业和学号属性及相应的操作。类Worker新增部门和工作属性及相应操作。在主函数中声明一个
//WorStu最远派生类的对象,显示相关信息。

#include <iostream>
using namespace std;

class Person
{
public:
Person(){}
~Person(){}
void set_name(string);
void set_age(int);
void set_sex(string);
string show_name(){return name ;}
int show_age(){return age ;}
string show_sex(){return sex ;}
void display();
protected:
string name ;
int age ;
string sex ;
};
class Student:virtual public Person
{
public:
Student(){}
~Student(){}
void set_department(string);
void set_major(string) ;
void set_id(long) ;
string show_department(){return department ;}
string show_major(){return major ;}
long show_id(){return id ;}
protected:
string department ;
string major ;
long id ;
};
class Worker:virtual public Person
{
public:
Worker(){}
~Worker(){}
void set_section(string) ;
void set_job(string);
string show_section(){return section ;}
string show_job(){return job ;}
protected:
string section ;
string job ;
};

class WoeStu:public Student ,public Worker
{
public:
void show() ;
};
void Person::set_name(string a)
{
name = a ;
}
void Person::set_age(int a)
{
age = a ;

}
void Person::set_sex(string a)
{
sex = a ;
}
void Person::display()
{
cout<<"name:\n";
show_name();
cout<<"age:\n";
show_age();
cout<<"sex:\n";
show_sex();
}
void Student::set_department(string a )
{
department = a ;
}
void Student::set_major(string a)
{
major = a ;
}
void Student::set_id(long a)
{
id = a ;
}
void Worker::set_section(string a)
{
section = a ;
}
void Worker::set_job(string a )
{
job = a ;
}
void WoeStu::show()
{
display();
cout<<"department:\n";
show_department();
cout<<"major:\n";
show_major();
cout<<"id:\n";
show_id();
cout<<"section:\n";
show_section();
cout<<"job:\n";
show_job();
}

int main()
{
WoeStu WS ;
WS.set_name("Tom");
WS.set_age(25);
WS.set_sex("man");
WS.set_department("computer science ");
WS.set_major("Computer Science and Technology");
WS.set_id(11320088);
WS.set_section("export");
WS.set_job("sales");
WS.show();
return 0 ;
}

主函数参数没有起作用.
...全文
169 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Julykey 2010-04-08
  • 打赏
  • 举报
回复
其实自己写的东西多看几遍,再动手调试一下,一些小问题都会解决的
chaoliu1024 2010-04-08
  • 打赏
  • 举报
回复
void Person::display()
{
cout<<"name:\n";
cout<<show_name();
cout<<"age:\n";
cout<<show_age();
cout<<"sex:\n";
cout<<show_sex();
}

void WoeStu::show()
{
display();
cout<<"department:\n";
cout<<show_department();
cout<<"major:\n";
cout<<show_major();
cout<<"id:\n";
cout<<show_id();
cout<<"section:\n";
cout<<show_section();
cout<<"job:\n";
cout<<show_job();
}

参数传入了,只是没有打印。。。要cout
为什么加cout会报错呢?因为少了#include<string>头文件,程序中用到了string类,加
#include<string>
就没问题了。我看了半天呢。。。昨晚看也不知道为什么。。。哎。。。只是少了头文件。。。大家以后都要注意啊!
lllwwjj123 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zht_304 的回复:]
string show_department(){return department ;}
string show_major(){return major ;}
……

都只是返回值。 你要cout啊
[/Quote]
用cout<<之后会报错~
lllwwjj123 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zqlclj 的回复:]
C/C++ code

void WoeStu::show()
{
display();
cout<<"department:\n"<<show_department();
cout<<"major:\n"<<show_major();
cout<<"id:\n"<<show_id();
cout<<"section:\n"<<show_se……
[/Quote]
这样不行啊,会出现以下的错误
“错误 1 error C2679: 二进制“<<”: 没有找到接受“std::string”类型的右操作数的运算符(或没有可接受的转换) d:\用户目录\documents\visual studio 2008\projects\project1\实验七(2)\实验七(2)\7.cpp 106 "
lllwwjj123 2010-04-08
  • 打赏
  • 举报
回复
问题已经解决了,谢谢各位的帮忙!
看来程序员这条路还真是曲折啊~~~
lllwwjj123 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhzxlc_06 的回复:]
C/C++ code
void Person::display()
{
cout<<"name:\n";
cout<<show_name();
cout<<"age:\n";
cout<<show_age();
cout<<"sex:\n";
cout<<show_sex();
}


C/C++ code
void Woe……
[/Quote]
刚刚回来我看程序的时候才发现没有string头文件,真是粗心!早上还没看到你的回复呢,呵呵!谢谢了
na2650945 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zhzxlc_06 的回复:]
C/C++ code
void Person::display()
{
cout<<"name:\n";
cout<<show_name();
cout<<"age:\n";
cout<<show_age();
cout<<"sex:\n";
cout<<show_sex();
}


C/C++ code
void Woe……
[/Quote]
这个讲解最全面啦。
zqlclj 2010-04-07
  • 打赏
  • 举报
回复

void WoeStu::show()
{
display();
cout<<"department:\n"<<show_department();
cout<<"major:\n"<<show_major();
cout<<"id:\n"<<show_id();
cout<<"section:\n"<<show_section();
cout<<"job:\n"<<show_job();
}
zht_304 2010-04-07
  • 打赏
  • 举报
回复
string show_department(){return department ;}
string show_major(){return major ;}
……

都只是返回值。 你要cout啊

64,637

社区成员

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

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