怎样编写c++语句编写 从键盘中输入数据初始化“类” 20
比如这个例题#include<iostream.h>#include<string.h>classPerson{char*name;intnum;public:Perso...
比如这个例题
#include<iostream.h>
#include<string.h>
class Person{
char *name;
int num;
public:
Person(char *x,int y)
{
name=new char[strlen(x)+1];
strcpy(name,x);
num=y;
}
void print();
};
void Person::print()
{
cout<<"your name is:"<<name;
cout<<endl;
cout<<"your num is:"<<num;
cout<<endl;
}
class Student:public Person{
float score;
int classroom;
public:
Student(float s,int c,char *name,int num):Person(name,num){
score=s;
classroom=c;
}
void disp1();
};
void Student ::disp1()
{
Person::print();
cout<<"your score is:"<<score<<endl;
cout<<"your classroom is:"<<classroom<<endl;
}
void main()
{
Student s(88,6,"sunchangpu",54);
s.disp1();
}
我现在是在主函数中直接给于赋值,我想知道的是在类中 如何在初始化时使用从键盘直接输入给于赋值,希望哪位高手能帮帮忙,在上面的程序修改下。
我就25分积分。。。全给你了~ 展开
#include<iostream.h>
#include<string.h>
class Person{
char *name;
int num;
public:
Person(char *x,int y)
{
name=new char[strlen(x)+1];
strcpy(name,x);
num=y;
}
void print();
};
void Person::print()
{
cout<<"your name is:"<<name;
cout<<endl;
cout<<"your num is:"<<num;
cout<<endl;
}
class Student:public Person{
float score;
int classroom;
public:
Student(float s,int c,char *name,int num):Person(name,num){
score=s;
classroom=c;
}
void disp1();
};
void Student ::disp1()
{
Person::print();
cout<<"your score is:"<<score<<endl;
cout<<"your classroom is:"<<classroom<<endl;
}
void main()
{
Student s(88,6,"sunchangpu",54);
s.disp1();
}
我现在是在主函数中直接给于赋值,我想知道的是在类中 如何在初始化时使用从键盘直接输入给于赋值,希望哪位高手能帮帮忙,在上面的程序修改下。
我就25分积分。。。全给你了~ 展开
6个回答
展开全部
你可以用set,比如你要初始化score,那么写一个void setScore(float socre);函数,用对象调用。
你现在用的是构造函数,也是可以用在键盘输入啊~~定义变量贝
float f;
int c;
char *name = new char[10];//这里的个数你了以自己定,个人感觉C++的话
//用string比较好
int num;
cin>>f;
cin>>c;
for(int i=0;i<10;i++)
cin>>name[i];
cin>>num;
Student s(f, c, name, num);
这种是最基础的,C++你可以重载>>,直接cin>>对象,你想知道更多给我发信息吧,邮箱:xiudewu520@126.com,晚上10点后才有时间看~~~
你现在用的是构造函数,也是可以用在键盘输入啊~~定义变量贝
float f;
int c;
char *name = new char[10];//这里的个数你了以自己定,个人感觉C++的话
//用string比较好
int num;
cin>>f;
cin>>c;
for(int i=0;i<10;i++)
cin>>name[i];
cin>>num;
Student s(f, c, name, num);
这种是最基础的,C++你可以重载>>,直接cin>>对象,你想知道更多给我发信息吧,邮箱:xiudewu520@126.com,晚上10点后才有时间看~~~
展开全部
这里面改了一些东西。就是利用操作符重载。然后再设为友元。我的这个程序是在DEV C++里面运行的。我试了,在VC 6.0下,不行。不过,语法是书上说的,没有错。可能是因为那个编译器不支持吧。
#include<iostream>
#include<string>
using namespace std;
class Person
{
string name;
int num;
public:
friend istream& operator >> (istream &is, Person &p);
void print();
};
void Person::print()
{
cout<<"your name is:"<<name;
cout<<endl;
cout<<"your num is:"<<num;
cout<<endl;
}
class Student:public Person
{
float score;
int classroom;
public:
friend istream& operator >> (istream &is, Student &s);
void disp1();
};
void Student ::disp1()
{
//Person::print();
cout<<"your score is:"<<score<<endl;
cout<<"your classroom is:"<<classroom<<endl;
}
istream& operator >> (istream &is, Person &p)
{
is >> p.name >> p.num;
return is;
}
istream& operator >> (istream &is, Student &s)
{
is >> s.score >> s.classroom;
return is;
}
int main()
{
Person P;
Student s;
cout << "input name and num: ";
cin >> P;
cout << "input score and classroom: ";
cin >> s;
P.print();
s.disp1();
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
class Person
{
string name;
int num;
public:
friend istream& operator >> (istream &is, Person &p);
void print();
};
void Person::print()
{
cout<<"your name is:"<<name;
cout<<endl;
cout<<"your num is:"<<num;
cout<<endl;
}
class Student:public Person
{
float score;
int classroom;
public:
friend istream& operator >> (istream &is, Student &s);
void disp1();
};
void Student ::disp1()
{
//Person::print();
cout<<"your score is:"<<score<<endl;
cout<<"your classroom is:"<<classroom<<endl;
}
istream& operator >> (istream &is, Person &p)
{
is >> p.name >> p.num;
return is;
}
istream& operator >> (istream &is, Student &s)
{
is >> s.score >> s.classroom;
return is;
}
int main()
{
Person P;
Student s;
cout << "input name and num: ";
cin >> P;
cout << "input score and classroom: ";
cin >> s;
P.print();
s.disp1();
system("pause");
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class Person{
char *name;
int num;
public:
void SetName(const char *s);
void SetNum(const int &s){num=s;};
void Input(); //Get Data From KeyBoard
};
void Person::SetName(char *s)
{
if(!s)
{
name=NULL;
cout<<"set name to NULL"<<endl;
return;
}
strcpy(name,s,strlen(s));
}
void Person::Input()
{
char a[80]="";
int i=0;
char s;
cout<<""input name:<<endl;
while((s=getc())!='\n')a[i++]=s;
a[i]='\0';
cout<<"input number:"<<endl;
cin>>i;
SetName(a);
SetNum(i);
cout<<"input over"<<endl;
}
继承类与此类同
char *name;
int num;
public:
void SetName(const char *s);
void SetNum(const int &s){num=s;};
void Input(); //Get Data From KeyBoard
};
void Person::SetName(char *s)
{
if(!s)
{
name=NULL;
cout<<"set name to NULL"<<endl;
return;
}
strcpy(name,s,strlen(s));
}
void Person::Input()
{
char a[80]="";
int i=0;
char s;
cout<<""input name:<<endl;
while((s=getc())!='\n')a[i++]=s;
a[i]='\0';
cout<<"input number:"<<endl;
cin>>i;
SetName(a);
SetNum(i);
cout<<"input over"<<endl;
}
继承类与此类同
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream.h>
#include<string.h>
class Person{
char name[20];
int num;
public:
Person(char na[20]="",int y=0)
{
strcpy(name,na);
num=y;
}
void print();
void set_name()
{
cout<<"请输入姓名:"<<endl;
char na[20];
cin>>na;
strcpy(name,na);
}
void set_num()
{
cout<<"请输入NUM:"<<endl;
int m;
cin>>m;
num=m;
}
};
void Person::print()
{
cout<<"your name is:"<<name;
cout<<endl;
cout<<"your num is:"<<num;
cout<<endl;
}
class Student:public Person{
float score;
int classroom;
public:
Student(float s,int c,char *name,int num):Person(name,num){
score=s;
classroom=c;
}
void disp1();
void set_score()
{
cout<<"请输入Score:"<<endl;
int n;
cin>>n;
score=n;
}
void set_classroom()
{
cout<<"请输入Classroom:"<<endl;
int n;
cin>>n;
classroom=n;
}
};
void Student::disp1()
{
Person::print();
cout<<"your score is:"<<score<<endl;
cout<<"your classroom is:"<<classroom<<endl;
}
void main()
{
Student s(88,6,"sunchangpu",54); //相当与初始化
// s.disp1();
s.set_name();
s.set_num();
s.set_classroom();
s.set_score();
s.disp1();
}
#include<string.h>
class Person{
char name[20];
int num;
public:
Person(char na[20]="",int y=0)
{
strcpy(name,na);
num=y;
}
void print();
void set_name()
{
cout<<"请输入姓名:"<<endl;
char na[20];
cin>>na;
strcpy(name,na);
}
void set_num()
{
cout<<"请输入NUM:"<<endl;
int m;
cin>>m;
num=m;
}
};
void Person::print()
{
cout<<"your name is:"<<name;
cout<<endl;
cout<<"your num is:"<<num;
cout<<endl;
}
class Student:public Person{
float score;
int classroom;
public:
Student(float s,int c,char *name,int num):Person(name,num){
score=s;
classroom=c;
}
void disp1();
void set_score()
{
cout<<"请输入Score:"<<endl;
int n;
cin>>n;
score=n;
}
void set_classroom()
{
cout<<"请输入Classroom:"<<endl;
int n;
cin>>n;
classroom=n;
}
};
void Student::disp1()
{
Person::print();
cout<<"your score is:"<<score<<endl;
cout<<"your classroom is:"<<classroom<<endl;
}
void main()
{
Student s(88,6,"sunchangpu",54); //相当与初始化
// s.disp1();
s.set_name();
s.set_num();
s.set_classroom();
s.set_score();
s.disp1();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你自己定义一个构造函数,键盘输入的数做实参用来初始化形参不久行了。像这样
// 类A
class A
{
A(int f,int g):b(f),c(g){}
privite :
int b;
int c;
};
// 主函数
main()
{
int f_;
int g_;
cout << "输入两个数用来初始化类";
cin >> f_ >> g_;
A aaa(f_,g_);
}
// 类A
class A
{
A(int f,int g):b(f),c(g){}
privite :
int b;
int c;
};
// 主函数
main()
{
int f_;
int g_;
cout << "输入两个数用来初始化类";
cin >> f_ >> g_;
A aaa(f_,g_);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询