当前位置:文档之家› C++上机实验报告实验六

C++上机实验报告实验六

Point&operator--();
Point operator--(int);
void Show();
private:
int _x; //私有数据成员
int _y;
};
Point::Point(){ //Point类构造函数
_x=0;_y=0;}
Point::Point(int x,int y) //Point类构造函数
class Birthday{
public:
Birthday(int Year,int Month,int Day); //构造函数
Birthday(){} //构造函数
~Birthday(){} //析构函数
Birthday(Birthday &p); //复制构造函数
int showBirthday(){
++(this->_y);
return old;
}
Point & Point::operator--() //重载后置--运算符为Point类成员函数
{
_x--;
_y--;
}
Point Point::operator--(int) //重载前置--运算符为Point类成员函数
{
Point old=*this;
void Run(){cout<<"The motorcar is running!"<<endl;}
void Stop(){cout<<"The motorcar has stopped!"<<endl;}
motorcar(){}
~motorcar(){}
};
class motorcycle:public bicycle,public motorcar //以bicycle类和motorcar类作为基类派生新类motorcycle
int show();
int enter();
private:
string number,id,sex; //字符串类型变量数据成员
Birthday p1; //Birthday类数据成员
};
int people::show()
{
cout<<"\n性别 "<<sex<<" 编号 ";
cout<<number;
cout<<year<<"年"<<month<<"月"<<day<<"日";} //内联成员函数
int enter();
private:
int year,month,day;
};
Birthday::Birthday(Birthday &p){
year=p.year;
month=p.month;
day=p.day;
p->Stop();
p=&c; //使指针p指向motorcar类对象c
p->Run(); //通过指针调用motorcar类成员函数
p->Stop();
p=&d; //使指针指向motorbicycle类对象d
p->Run(); //通过指针调用motorcycle类成员函数
p->Stop();
return 0;
virtual void Stop(){cout<<"The vehicle has stopped!"<<endl;} //定义虚函数Stop()
};
class bicycle: virtual public vehicle //定义派生类bicycle,声明基类为派生类的虚基类
{
public:
void Run(){cout<<"The bicycle is running!"<<endl;}
--(this->_x);
--(this->_y);
return old;
}
void Point::Show() //输出Point的坐标值
{
cout<<_x<<","<<_y<<")"<<endl;
}
int main()
{
Point a(2,3); //定义一个Point类对象a
Point b=a++; //定义Point类对象b并用a++初始化b
{
public:
void Run(){cout<<"The motorcycle is running!"<<endl;}
void Stop(){cout<<"The motorcycle has stopped!"<<endl;}
};
int main()
{
vehicle a; //定义vehicle类的一个对象a
Point c=++a; //定义Point类对象c并用++a初始化c
Point d=--a; //定义Point类对象d并用--a初始化d
Point e=a--; //定义Point类对象e并用a--初始化e
cout<<"Point a(";
a.Show(); //输出a的坐标
cout<<"Point b(";
void Stop(){cout<<"The bicycle has stopped!"<<endl;}
bicycle(){}
~bicycle(){}
};
class motorcar: virtual public vehicle //定义派生类motorcar,声明基类为派生类的虚基类
{
public:
b.Show(); //输出b的坐标
cout<<"Point c(";
c.Show(); //输出c的坐标
cout<<"Point d(";
d.Show(); //输出d的坐标
cout<<"Point e(";
e.Show(); //输出e的坐标
return 0;
}
运行结果:
2编写程序定义一个车(vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数。在main()函数中定义vehicle、bicycle、motorcar、motorcycle的对象,调用其Run()、Stop()函数,观察其执行情况。再分别用vehicle类型的指针来调用这几个对象的成员函数,看看能否成功;把Run、Stop定义为虚函数,再试试看。
p[t].enter();
}
for(t=0;t<2;t++) //输出对象数组成员信息
{
cout<<"\n第"<<t+1<<"个人员信息如下:"<<endl;
p[t].show();
}
people p3; //定义people类的对象p3
p3-p[1]; //使用重载运算符将p[1]赋给p3
cout<<"\n第3个人员信息如下:"<<endl;
3. (选做)对实验4中的People类重载“==”运算符和“-”运算符,“==”运算符判断两个people类对象的id属性是否相等;“-”运算符实现People类对象的赋值操作。
实验内容及实验步骤
1.编写程序定义Point类,在类中定义整型的私有成员变量_x_y,定义成员函数Point& operator++();Point operator++(int);以实现对Point类重载“++”(自增)运算符,定义成员函数Point& operator--();Point operator--(int);以实现对Point类重载“--”(自减)运算符,实现对坐标值的改变。程序名:1ab8_1.Cpp
#include <iostream>
using namespace std;
class Point
{
public:
Point();
Point(int x,int y);
~Point(){} //Point类析); //公有成员函数
Point operator++(int);
cout<<" 生日 ";
p1.showBirthday(); //调用Birthday类成员函数
cout<<" 身份证号 "<<id<<endl;
}
int people::enter(){
p1.enter();
cout<<"性别:";
cin>>sex;
cout<<"编号:";
cin>>number;
相关主题