在类中如何使用cin啊?

t52693791 2010-08-16 09:03:38
我是新手这个问题应该比较基础,比如现在有这样一个程序

#include<iostream>
#include<string>
using namespace std;
//------------------------------
class student
{public:
void display();
protected:
int num;
string name;
char sex;
};
void student::display()
{
cout<<"num:"<<num<<'\n';
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
//----------------------------------
class student1:protected student
{
public:
void display1();
private:
int age;
string addr;
};
void student1::display1()
{
display();
cout<<"age:"<<age<<'\n';
cout<<"addr:"<<addr<<'\n';
}
//---------------------------
int main()
{
student1 xize;
xize.display1();
return 0;
}
这里面没有赋值,我想问下如果通过cin输入,使用户直接输入这些数据这个代码应该怎么改啊?
...全文
170 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chaoliu1024 2010-08-16
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;
//------------------------------
class student
{
public:
void display();
void get();
protected:
int num;
string name;
char sex;
};
void student::display()
{
cout<<"num:"<<num<<'\n';
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
void student::get() // mark
{
cout<<"输入num:";
cin>>num;
cout<<"输入name:";
cin>>name;
cout<<"输入sex:";
cin>>sex;
}
//----------------------------------
class student1:protected student
{
public:
void display1();
void get1(); // mark
private:
int age;
string addr;
};
void student1::display1()
{
display();
cout<<"age:"<<age<<'\n';
cout<<"addr:"<<addr<<'\n';
}
void student1::get1() // mark
{
get(); // mark
cout<<"输入age:";
cin>>age;
cout<<"输入addr:";
cin>>addr;
}
//---------------------------
int main()
{
student1 xize;
xize.get1();
xize.display1();
return 0;
}
Jim_King_2000 2010-08-16
  • 打赏
  • 举报
回复
一般来说,继承体系的输入输出应该先定义一个虚函数进行输入输出,然后再重载operator<<,在其中调用该虚函数。代码如下:

// student.h
class student
{
public:
virtual void input(istream &is);
virtual void display(ostream &os);

private:
int num_;
string name_;
};

class specific_student : public student
{
public:
virtual void input(istream &is);
virtual void display(ostream &os);

private:
int age_;
string addr_;
};

istream &operator>>(istream &is, student &s);
ostream &operator<<(ostream &os, student &s);

// student.cpp
void student::input(istream &is)
{
is >> num_;
is >> name_;
}

void student::display(ostream &os)
{
os << "number: " << num_ << "\n";
os << "name: " << name_ << "\n";
}

void specific_student::input(istream &is)
{
student::input(is);
is >> age_;
is >> addr_;
}

void specific_student::display(ostream &os)
{
student::display(os);
os << "age: " << age_ << "\n";
os << "address: " << addr_ << "\n";
}

istream &operator>>(istream &is, student &s)
{
s.input(is);
return is;
}

ostream &operator<<(ostream &os, student &s)
{
s.display(os);
return os << "\n";
}


arong1234 2010-08-16
  • 打赏
  • 举报
回复
重载istream& operator>>(istream & in, student1& stu);
这是最基本的做法,运算符重载一章应该会讲
t52693791 2010-08-16
  • 打赏
  • 举报
回复
难道没人?在线等。。。。

64,682

社区成员

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

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