第五章 多态性例题

运算符的重载

编写程序,定义一个描述三维空间坐标点的类,用成员函数重载“+”运算符以实现两个三维坐标的相加,用友元函数重载前置“++”运算符以实现三维坐标的加一操作。

期望输入:

1 2 3

4 5 6

期望输出:

P1的坐标为:x:1,y:2,z:3

P2的坐标为:x:4,y:5,z:6

P1+P2的坐标为:x:5,y:7,z:9

++P1的坐标为:x:2,y:3,z:4

期望输入:

1.2 2.3 3.4

2.1 3.2 4.3

期望输出:

P1的坐标为:x:1.2,y:2.3,z:3.4

P2的坐标为:x:2.1,y:3.2,z:4.3

P1+P2的坐标为:x:3.3,y:5.5,z:7.7

++P1的坐标为:x:2.2,y:3.3,z:4.4

期望输入:

2.2 3.3 4

1.1 5.5 6

期望输出:

P1的坐标为:x:2.2,y:3.3,z:4

P2的坐标为:x:1.1,y:5.5,z:6

P1+P2的坐标为:x:3.3,y:8.8,z:10

++P1的坐标为:x:3.2,y:4.3,z:5

#include<iostream>
using namespace std;
class Point
{
public:
	Point(double x,double y,double z):m_x(x),m_y(y),m_z(z){}
	friend Point& operator++(Point&);
	Point operator+(Point& p)
	{
		Point temp=p;
		temp.m_x=this->m_x+p.m_x;
		temp.m_y=this->m_y+p.m_y;
		temp.m_z=this->m_z+p.m_z;
		return temp;
	}
	void print()
	{
		cout<<"x:"<<m_x<<",y:"<<m_y<<",z:"<<m_z<<endl;
	}
private:
	double m_x;
	double m_y;
	double m_z;
};
Point& operator++(Point& p)
{
	p.m_x++;
	p.m_y++;
	p.m_z++;
	return p;
}
int main()
{
	double x1,y1,z1,x2,y2,z2;
    cin>>x1>>y1>>z1;
    cin>>x2>>y2>>z2;
    Point P1(x1, y1, z1);
    Point P2(x2, y2, z2);
    cout << "P1的坐标为:";
    P1.print();
    cout << "P2的坐标为:";
    P2.print();
    Point P3 = P1 + P2;
    cout << "P1+P2的坐标为:";
    P3.print();
    ++P1;
    cout << "++P1的坐标为:";
    P1.print();
    return 0;
}

学校人员管理系统-虚基类和虚函数

【问题描述】

建立一个简单的学校人员管理系统,包括对学生、员工和在职学生(既是员工又是学生)的管理。需要完成如下功能:
1、建立一个School类,在其中定义增加人员的Append函数。
2、建立一个基类Person类,要具有姓名和性别的属性,并把输出函数ShowMe()定义为虚函数;
3、建立一个员工Staff类和一个学生类Student,均由Person继承而来。要求可以输出员工类(学生类)对象的属性(姓名、性别和工作证号码(或学生学号),分别写出对ShowMe()函数的具体实现。
4、建立一个在职学生类Staff_Student类,由员工类和学生类继承而来。写出对ShowMe()函数的具体实现,可以输出对象属性,。
5、重载,实现用cin为员工类、学生类和在职学生类对象赋值。
6、编写main()主函数,测试上述功能。

【样例输入】

Tom M 101
Andy F 10002
Jesson M 10003 102

【样例输出】

姓名:Tom
性别:M
工号:101
姓名:Andy
性别:F
学号:10002
姓名:Jesson
性别:M
学号:10003
工号:102

#include<iostream>  
#include<string>  
using namespace std;  
class  Person  
{  
public:
Person(string name1="", char s='M')
{
	name=name1;  
    sex=s;  
}  
virtual  void  ShowMe();  
friend  istream&  operator>>(istream  &  in,  Person  &p);  
protected:  
      char  sex;  
      string  name;  
};  

void  Person::ShowMe()  
{  
	cout<<"姓名:"<<name<<endl;
	cout<<"性别:"<<sex<<endl;
}  
istream&  operator>>(istream  &  in,  Person  &p)
{
	in>>p.name>>p.sex;
	return in;
}

class  Staff:virtual public Person
{  
protected:
	int  wID;  //工作号  
public:
	Staff(int  id=0,string  name1="",  char  s='M'):Person(name1,s)
	{
		wID=id;
	}  
virtual  void  ShowMe();  
friend  istream&  operator>>(istream  &  in,  Staff  &p);  
};  
void  Staff::ShowMe()  
{        
	Person::ShowMe();
	cout<<"工号:"<<wID<<endl;
}  
istream&  operator>>(istream  &  in,  Staff  &p)
{
	in>>p.name>>p.sex>>p.wID;
	return in;
}

class  Student:
virtual public Person
{  
protected:
	int sID;  //学号  
public:
	Student(int  id=0,string  name1="",  char  s='M'):Person(name1,s)
	{
		sID=id;
	}  
    virtual  void  ShowMe();  
    friend  istream&  operator>>(istream  &  in,Student  &p);  
};  
void  Student::ShowMe()  
{  
	Person::ShowMe();
	cout<<"学号:"<<sID<<endl;
}  
istream&  operator>>(istream  &  in,Student  &p)
{
	in>>p.name>>p.sex>>p.sID;
	return in;
}

class  Staff_Student:
public Student,public Staff
{  
public:
	Staff_Student(string  name1="",char s='M',int id1=0,int id2=0):Person(name1,s),Student(id1,name1,s),Staff(id2,name1,s){}
    virtual  void  ShowMe();  
    friend  istream&  operator>>(istream  &  in,Staff_Student  &p);  
};  
void  Staff_Student::ShowMe()  
{     
	cout<<"姓名:"<<name<<endl;
	cout<<"性别:"<<sex<<endl;
	cout<<"学号:"<<sID<<endl;
	cout<<"工号:"<<wID<<endl;
}  
istream&  operator>>(istream  &  in,Staff_Student  &p)
{
	in>>p.name>>p.sex>>p.sID>>p.wID;
	return in;
}
class  School  
{
private:  
    Person  *p[100];  
    int  size;  
public:
	School();  
	~School(){}
void  append(Person&);  
void  display();  
};  
School::School()  
{
	size=0;  
}  
void  School::append(Person&  p1)  
{      
	p[size++]=&p1;
}  
void  School::display()  
{
	int i;  
    for(i=0;i<size;i++)
		p[i]->ShowMe();  
}  
int  main()  
{  
	School sch;  
	Staff s1;  
	cin>>s1;  
	sch.append(s1);  
	Student st1;  
	cin>>st1;  
	sch.append(st1);  
	Staff_Student ss1("SS1",'F',1001,1003);  
	cin>>ss1;  
	sch.append(ss1);  
	sch.display();  
	return 0;
}

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
目标: 设计一个小型的学生管理系统的几个功能。 第一步要求: 1、 对于一个学生类,作为整个系统的核心,我们希望这个类能包括学生共有的基本信息:学号(id),出生岁月(birthday),专业(speciality),年级grade。要求在构建学生对象的时候提供最少姓名。 2、 作为学生类的实例,我们希望能通过设置器或访问器(set get方法)来修改这些学生的不同信息。 3、 通过对toString()方法的重写实现信息打印。 4、 通过对equals()方法的重写实现对象比较逻辑。 第二步要求: 5、 在构建学生对象的时候可以只提供姓名信息,要求在学生类内部对学号,自行处理。 -----------------------------------------------------------------------------第三步要求: 6、 设计一个人Person类。学生必须继承人的这个类。姓名name,性别sex,年龄age ---------------------------------------------------------------------------- 第四步要求: 7、设计一个课程类,在学生类中可以定义一个方法设置课程,注意学生类需要调用课程类,而课程类需要学生类中的学生信息,所以建议使用内部类处理。 ---------------------------------------------------------------------------- 第五步要求: 8、做一个信息过滤,要求能存储所有信息,但也可以对所有学生信息根据某个条件进行过滤,把所有女生信息提取出来打印或把某个年龄段的学生打出来等。 ---------------------------------------------------------------------------- 第六步要求: 9、对过滤的条件采用接口回调的方式重构。 ---------------------------------------------------------------------------- 第七步要求: 10、对过滤的条件采用内部类的方式重构。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

꧁༺❀氯ྀൢ躅ྀൢ❀༻꧂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值