#include<iostream> using namespace std; class Student { private: string name; int age; double high;

#include<iostream>
using namespace std;
class Student
{
private:
string name;
int age;
double high;
char sex;
public:
Student(){};
Student(string n,int a,double h,char s);
void display();
void changage();
void set_data();
};
Student::Student(string n,int a,double h,char s)
{
name=n;age=a;high=h;sex=s;
}
void Student::display()
{
cout<<"姓名:"<<name<<'\t'<<"年龄:"<<age<<'\t'<<"身高:"<<high<<'\t';
switch(sex)
{
case 'w':cout<<"男";break;
default:cout<<"女";break;
}
}
void Student::changage(string n,int a,double h,char s)
{
name=n;age=a;high=h;sex=s;
}
void Student::set_data()
{
cout<<"请依次输入姓名,年龄,身高,性别:"
cin>>name>>age>>high>>sex>>endl;
}
void main()
{
Student a1,a2;
a1("zhanghua",23,1.56,'w');
a1.display();
a2.set_data();
a2.display();
}
请高手指点迷津!重点是输出函数怎么错了!!!

Student a1,a2;
a1("zhanghua",23,1.56,'w');首先这里会编译不过,构造函数是在声明的时候调用的,
Student a1("zhanghua",23,1.56,'w');应该这样

或者
Student a1,a2;

a1.changage("zhanghua",23,1.56,'w');
然后这里
a2.display();由于a2里面的变量都没赋值,导致值时乱码,也是有问题的
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-09-09
#include<iostream>
using namespace std;
class Student
{
private:
string name;
int age;
double high;
char sex;
public:
Student(){};
Student(string n,int a,double h,char s);
void display();
void changage();//这个函数啊,怎么与下面的实现不一样啊,搞什么
void set_data();
};
Student::Student(string n,int a,double h,char s)
{
name=n;age=a;high=h;sex=s;
}
void Student::display()
{
cout<<"姓名:"<<name<<'\t'<<"年龄:"<<age<<'\t'<<"身高:"<<high<<'\t';
switch(sex)
{
case 'w':cout<<"男";break;
default:cout<<"女";break;
}
}
void Student::changage(string n,int a,double h,char s)
{
name=n;age=a;high=h;sex=s;
}
void Student::set_data()
{
cout<<"请依次输入姓名,年龄,身高,性别:"//上面的cout都有分号,这块怎么没了?
cin>>name>>age>>high>>sex>>endl;//endl是变量?不是的话你想给他传什么?
}
void main()
{
Student a1,a2;
a1("zhanghua",23,1.56,'w');//这句啊,怎么学的c++啊
a1.display();
a2.set_data();
a2.display();
}
//错误提示都不贴,看来是个新手,而且还不会提问本回答被网友采纳

使用c++面向对象的程序设计方法,找出十个学生成绩中的最高者,并输出...
include<iostream>#include<cstring>using namespace std;class student{private:string name;int score;public:student(){}student(string _n,int _s){name=_n;score=_s;}friend student getMaxScore(student ss[],int n);void initStudent(string _n,int _s){score=_s;name=_n; }void pr...

C++输入名字,输出其学号,成绩问题
include <iostream>#include <string>using namespace std;class Student {private:float Score1,Score2,Score3;int Number;string Name;public:Student(string name, int number, float s1, float s2, float s3) {Name = name;Number = number;Score1 = s1;Score2 = s2;Score3 = s3;}int Ge...

实现Student类,包含姓名,学号,专业,综合成绩4个属性及相关get&&set函数...
using namespace std;class Student { private:string name;int id;string major;int score;public:Student();Student(string name, int id, string major, int score);void setName(string name);string getName(void)const;void setId(int id);int getId(void)const;void setMajor(string major)...

用c++编写一个学生类。输出每个学生的姓名、学号、成绩
include<iostream>#include<string>using namespace std;class Student{public: Student(string s,int a,int b):name(s),id(a),score(b){} void display();private: string name; int id; int score;};void Student::display(){ cout << "姓名:" << ends << name << endl; cout <<...

C++程序设计中提示expected ')' before 'sname'的错误, 怎么改?_百...
include<iostream>#include<string>using namespace std;class student {private:string name;long num;int x,y,z;public:student (string sname,long snum,int sx,int sy,int sz) \/\/这是构造函数,放到类定义中{name=sname;num=snum;x=sx;y=sy;z=sz;}void play(){cout<<name<<"学生的...

编写一个程序,已有若干个学生数据(见右表),包括学号,姓名,成绩,要求输出...
include<iostream> include<string> using namespace std;class student { public:student(string m,int n,double d);~student();static void avg();void display();private:int no;string name;double deg;static double sum;static double num;};student::student(string m,int n,double d){ n...

C++中怎么调用子类的程序??
include <iostream>#include <string>using namespace std;class student{private: int id; string name; string clas; string address;public: void students(int id,string name,string clas,string address) { this->id=id; this->name=name; this->clas=clas; t...

按程序注释中的提示补充代码使程序完整, 求救,刚学C++有点不太懂...
include <iostream> include<string> using namespace std;class Student \/\/学生 { private:string name; \/\/姓名 int age; \/\/年龄 public:Student(){ cout << "输入学生的姓名和年龄:" << endl;cin >> name >> age;} string getName(){ return name;} int getAge(){ return age;} vo...

跪求一C++程序设计。类似于学生成绩管理系统、世界杯积分系统。急用啊...
include <iostream> include <vector> include <string> using namespace std;class Student { private:string name;double English,Math,Chinese,Physics,score;public:Student(double E = 0, double M = 0, double C = 0, double P = 0){ English = E;Math = M;Chinese = C;Physics = P...

用c++编写一个程序,要求创建一个类,输入若干个学生的数据,包括学号,姓名...
参考代码:include <iostream>#include <string>using namespace std;#define MAX 100class Student{private:string num;string name;double subject[3];double total;public:void input(Student s[], int);void sort(Student s[], int);void print(Student s[], int);}stu[MAX];void Student::...

相似回答