运算符的重载
编写程序,定义一个描述三维空间坐标点的类,用成员函数重载“+”运算符以实现两个三维坐标的相加,用友元函数重载前置“++”运算符以实现三维坐标的加一操作。
期望输入:
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;
}