实验要求:
1.定义Point类,有坐标_x,_y两个成员变量;对Point类重载“++”(自增)、“――”(自减)运算符,实现对坐标值的改变。
2.定义一个车(vehiele)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。观察虚函数的作用。
题目一
编写程序定义Point类,在类中定义整型的私有成员变量_x、_y,定义成员函数Point& operator++();Point operator++(int);以实现对Point类重载“++”(自增)运算符,定义成员函数Point& operator--();Point operator--(int);以实现对Point类重载“--”(自减)运算符,实现对坐标值的改变。程序名:1ab4_1.cpp。
#include <iostream>
using namespace std;
class Point{
public:Point(int X,int Y){x=X;y=Y;};
Point &operator++();//前置
Point operator++(int);//后置
Point &operator--();
Point operator--(int);
void show(){cout<<"("<<x <<","<<y <<")"<<endl; }
private:int x,y;
};
Point&Point::operator++(){
x++;y++;return *this;
}
Point Point::operator++(int){
Point old=(*this);
++(*this);
return old;
}
Point&Point::operator--(){
x--;y--;return *this;
}
Point Point::operator--(int){
Point old=(*this);
--(*this);
return old;
}
int main()
{
int x,y;
cin>>x>>y;
Point a(x,y);
a.show();
(a++).show();
(++a).show();
(a--).show();
(--a).show();
return 0;
}
题目二
编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试试看。程序名:lab4_2.cpp。
#include <iostream>
using namespace std;
class vehiele{
public:virtual void run();
void stop();
};
void vehiele::run(){cout<<"vehicle run!"<<endl;}
class bicycle : virtual public vehiele{
public: virtual void run(){cout<<"bicycle run!"<<endl;}
void stop();
};
class motorcar :virtual public vehiele{
public: virtual void run(){cout<<"motorcar run!"<<endl;}
void stop();
};
class motocycle:public bicycle,public motorcar{
public: virtual void run(){cout<<"motorbicycle run!"<<endl;}
void stop();
};
int main()
{
vehiele a;
bicycle b;
motorcar c;
motocycle d;
a.run();
b.run();
c.run();
d.run();
vehiele *ptr;
ptr=&a;
ptr->run();
ptr=&b;
ptr->run();
ptr=&c;
ptr->run();
ptr=&d;
ptr->run();
return 0;
}
3. (选做)对实验2中的People类重载“==”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等;“=”运算符实现People类对象的赋值操作。
#include <iostream>
#include <string.h>
using namespace std;
class date {
private:
int year;
int month;
int day;
public:
date(int a,int b,int c):year(a),month(b),day(c){}
date(const date& a) {
year = a.year;
month = a.month;
day = a.day;
}
void show() {
cout << year<<" " << month <<" "<< day << endl;
}
date() {}
~date(){}
};
class people {
private:
char name[11];
char number[7];
char sex[3];
date m;
char id[16];
public:
people(char a[11], char b[7], char c[3], date T, char d[16])
{
strcpy(name, a);
strcpy(number, b);
strcpy(sex, c);
m=T;
strcpy(id, d);
}
people(){};
people(const people& d) {
strcpy (name, d.name);
strcpy (number, d.number);
strcpy (sex, d.sex);
strcpy (id, d.id);
m = d.m;
}
~people(){}
void show() {
cout << name << endl;
cout << number << endl;
cout << sex << endl;
m.show();
cout << id << endl;
}
bool operator == (const people &p) {
if (strcmp(id, p.id) == 0)return true;
else return false;
}
people& operator =(const people &p) {
*this = p;
return *this;
}
};
int main()
{
char name[11];
char number[7];
char sex[3];
char id[16];
int x, y, z;
cin>>name>>number>>sex>>id;
cin >> x >> y >> z;
date E(x,y,z);
people a(name,number,sex, E,id);
a.show();
people b;
b = a;
b.show();
if (b == a)cout << "yes" << endl;
else cout << "no" << endl;
}