为什么构造函数里的参数不能用字符串,要怎样用字符串做参数(“老师”那个参数)
//5.5
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
class School
{
private:
string object;
string name;
int age;
int numeber;
int sex;
public:
School(string &object1,string &name1, int &age1, int &numeber1, char sex1)
{
if (sex1 == 'm')
sex = 0;
if (sex1 == 'f')
sex = 1;
age = age1;
numeber = numeber1;
name = name1;
object =object1;
}
int printf();
};
int School::printf()
{
cout << "身份:" << object << endl;
cout <<"姓名:"<<name<<endl;
cout << "年龄: " << age<<endl;
if (sex == 0)
cout << "性别:男" << endl;
if (sex ==1)
cout<<"性别:女:"<<endl;
cout << "numeber:" << numeber << endl;
}
class Student :public School
{
private:
int ave_gar; //平均成绩
public:
Student(string &object1, string &name1, int &age1, int &numeber1, char sex1,int ave_gar1) :School
(object1, name1, age1, numeber1,sex1), ave_gar(ave_gar1)
{
cout << "平均成绩:" << ave_gar << endl;
}
};
class Teacher :public School
{
private:
string teacher; //教授的学科
public:
Teacher(string &object1, string &name1, int &age1, int &numeber1, char sex1, string &teacher1) :School
(object1,name1,age1,numeber1,sex1), teacher(teacher1)
{
cout << "教授的学科:" << teacher << endl;
}
};
class Staff :public School
{
private:
string work;
Staff(string &object1, string &name1, int &age1, int &numeber1, char sex1,string &work1):School
(object1, name1, age1,numeber1, sex1),work(work1)
{
cout << "工作类型:" << work<<endl;
}
};
int main()
{
Teacher a("教师","王一",32,2017320,'f',72);
a.printf();
Student b("学生","李二",15,2017203,'m',"语文");
b.printf();
Staff c("职工","赵三",28,2017256,'f',"清洁");
c.printf();
return 0;
}
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
class School
{
private:
string object;
string name;
int age;
int numeber;
int sex;
public:
School(string &object1,string &name1, int &age1, int &numeber1, char sex1)
{
if (sex1 == 'm')
sex = 0;
if (sex1 == 'f')
sex = 1;
age = age1;
numeber = numeber1;
name = name1;
object =object1;
}
int printf();
};
int School::printf()
{
cout << "身份:" << object << endl;
cout <<"姓名:"<<name<<endl;
cout << "年龄: " << age<<endl;
if (sex == 0)
cout << "性别:男" << endl;
if (sex ==1)
cout<<"性别:女:"<<endl;
cout << "numeber:" << numeber << endl;
}
class Student :public School
{
private:
int ave_gar; //平均成绩
public:
Student(string &object1, string &name1, int &age1, int &numeber1, char sex1,int ave_gar1) :School
(object1, name1, age1, numeber1,sex1), ave_gar(ave_gar1)
{
cout << "平均成绩:" << ave_gar << endl;
}
};
class Teacher :public School
{
private:
string teacher; //教授的学科
public:
Teacher(string &object1, string &name1, int &age1, int &numeber1, char sex1, string &teacher1) :School
(object1,name1,age1,numeber1,sex1), teacher(teacher1)
{
cout << "教授的学科:" << teacher << endl;
}
};
class Staff :public School
{
private:
string work;
Staff(string &object1, string &name1, int &age1, int &numeber1, char sex1,string &work1):School
(object1, name1, age1,numeber1, sex1),work(work1)
{
cout << "工作类型:" << work<<endl;
}
};
int main()
{
Teacher a("教师","王一",32,2017320,'f',72);
a.printf();
Student b("学生","李二",15,2017203,'m',"语文");
b.printf();
Staff c("职工","赵三",28,2017256,'f',"清洁");
c.printf();
return 0;
}
...全文
相关推荐
字符串做函数参数
字符串做函数参数,字符串copy函数技术推演,错误点等等
定义一个圆类circle,它有一个变量radius(半径)。从键盘输入数据,通过构造函数的参数传递给radius,编程计算并输出圆的的周长和面积。但是,必须确保输入的数据不为复数。
定义一个圆类circle,它有一个变量radius(半径)。从键盘输入数据,通过构造函数的参数传递给radius,编程计算并输出圆的的周长和面积。但是,必须确保输入的数据不为复数。
详解C++ 编写String 的构造函数、拷贝构造函数、析构函数和赋值函数
主要介绍了详解C++ 编写String 的构造函数、拷贝构造函数、析构函数和赋值函数的相关资料,这里提供实例帮助大家理解掌握这部分内容,需要的朋友可以参考下
switch不能字符串比较解决方法
switch不能字符串比较解决方法
浅谈Python类里的__init__方法函数,Python类的构造函数
如果某类里没有__init__方法函数,通过类名字创建的实例对象为空,切没有初始化;如果有此方法函数,通常作为类的第一个方法函数,有点像C++等语言里的构造函数。 class Ca: def __init__(self, v): # 注意前后各两个下划线 self.name = v def pr(self): print "a--->", self.name ia = Ca("Jeapedu") # 本质调用的是__init__方法函数 ia.pr() Ca.pr(ia) 输出结果 a—> Jeapedu a—> Jeapedu 以上这篇浅谈Python类里的__init__方法函数,P


