C 上机实验报告 实验六
5. 源程序 1. lab8_1.cpp #include<iostream>
using namespace std; class Point { private:
int _x; int _y; public: //构造.析构函数 Point(){} Point(int,int); ~Point(){} //++.--重载 Point& operator ++(); Point operator ++(int); Point& operator --(); Point operator --(int); //输出点坐标 void showPoint(); }; Point::Point(int x,int y) { _x=x; _y=y; } Point& Point::operator ++() { _x++; _y++; return *this; } Point Point::operator ++(int) { Point p=*this; ++(*this); return p; } Point& Point::operator --() { _x--; _y--; return *this; } Point Point::operator --(int) {
cout<<"Input id:"; cin.getline(id,16,'\n'); } //People 内联成员函数,输出人员信息 inline void People::showPeople() { cout<<"Name:"<<name<<endl; cout<<"Number:"<<number<<endl; cout<<"Sex:"<<sex<<endl; cout<<"ID:"<<id<<endl; } bool People::operator ==(People& p) { return id==p.id; } People& People::operator =(People& p) { strcpy(name,); strcpy(number,p.number); birthday=p.birthday;
year=y; month=m; day=d;
} Date::Date(Date &p) {
year=p.year; month=p.month; day=p.day; } //析构 inline Date::~Date(){} //Date 成员函数,设置出生年月日 void Date::setDate() {
int y,m,d; cout<<"Input the year:"; cin>>y; cout<<"Input the month:"; cin>>m; cout<<"Input the day:"; cin>>d; year=y; month=m; day=d; } //Date 内联成员函数,输出年月日 inline void Date::showDate() { cout<<"Birthday is "<<year<<"年"<<month<<"月"<<day<<"日"<<endl; } //People 构造函数 People::People(){}; People::People(char* n,char* nu,char* s,Date b,char* i) { strcpy(name,n); strcpy(number,nu); strcpy(sex,s); birthday=b; strcpy(id,i); } People::People(People &p) { strcpy(name,); strcpy(number,p.number); birthday=p.birthday; strcpy(id,p.id);
Point p=*this; --(*this); return p; } void Point::showPoint() { cout<<"The point is ("<<_x<<","<<_y<<")"<<endl; } int main() { Point apoint(3,5); apoint.showPoint(); (apoint++).showPoint();//测试后置++ apoint.showPoint(); (++apoint).showPoint();//测试前置++ apoint.showPoint(); (apoint--).showPoint();//测试后置-apoint.showPoint(); (--apoint).showPoint();//测试前置-apoint.showPoint();
实验六
1. 实验目的 1.掌握运算符重载的方法 2.学习使用虚函数实现动态多态性
多态性
2. 实验要求 1.定义 Point 类,有坐标_x,_y 两个成员变量;对 Point 类重载“++”(自增)、“――”(自减) 运算符,实现对坐标值的改变。 2.定义一个车(vehiele)基类,有 Run、Stop 等成员函数,由此派生出自行车(bicycle)类、汽车 (motorcar)类,从 bicycle 和 motorcar 派生出摩托车(motorcycle)类,它们都有 Run、Stop 等成 员函数。观察虚函数的作用。 3. (选做)对实验 4 中的 People 类重载“==”运算符和“=”运算符,“==”运算符判断两 个 people 类对象的 id 属性是否相等;“=”运算符实现 People 类对象的赋值操作。
} //People 析构 inline People::~People(){} //People 成员函数,设置各类数据 void People::setName() {
cout<<"Please input the person's name:"; cin.getline(name,11,'\n'); }
void stop(){cout<<"Motorcar is stopping!"<<endl;} }; class Motorcycle:public Bicycle,public Motorcar { public:
void run(){cout<<"Motorcycle is running!"<<endl;} void stop(){cout<<"Motorcycle is stopping!"<<endl;} }; int main() { Vehicle veh; Bicycle bic; Motorcar mot; Motorcycle m; //对象名.函数测试 /*veh.run(); veh.stop(); bic.run(); bic.stop(); mot.run(); mot.stop(); m.run(); m.stop();*/ //基类指针测试 Vehicle* p; p=&veh; p->run(); p->stop(); p=&bic; p->run(); p->stop(); p=&mot; p->run(); p->stop(); p=&m; p->run(思考题 1. 如何将一个运算符重载为类的成员函数? 函数类型 operator 运算符(形参表) {
函数体;
} 2. 如何将一个运算符重载为类的友元函数? friend 函数类型 operator 运算符(形参表) {
函数体;
} 3.如何实现运行时刻的多态? 在基类的成员函数前加上 virtual,就可以在它的派生类中声明相同名字和类型的成员函数, 在运行过程中,系统会自动判断并调用相应类中的成员函数,从而在调用过程中实现多态。
void People::setNumber() {
cout<<"Input number:"; cin.getline(number,7,'\n'); }
void People::setSex() {
cout<<"Input sex:"; cin.getline(sex,3,'\n'); }
void People::setId() {
return 0; } 3. lab8_3 #include<iostream> #include<cstring> using namespace std;
//Date 类 class Date { private:
int year; int month; int day; public: Date(); Date(int y,int m,int d); Date(Date &p); ~Date(); void setDate(); void showDate(); }; //People 类,其中含 Date 类型的数据 class People { private: char name[11]; char number[7]; char sex[3]; Date birthday; public: char id[16]; People(); People(char* n,char* nu,char* s,Date b,char* i); People(People &p); ~People(); bool operator ==(People&); People& operator =(People&); void setName(); void setNumber(); void setSex(); void setId(); void showPeople(); }; //Date 构造函数 Date::Date(){} Date::Date(int y,int m,int d) {