1.基本的运算符重载
#include <iostream>
using namespace std;
class Point{
int _x, _y;
public:
Point(int x = 0, int y = 0) :_x(x), _y(y){}
Point& operator++();
Point operator++(int);
Point& operator--();
Point operator--(int);
friend ostream& operator<<(ostream& o, Point& p);
};
Point& Point::operator++() //前置++,左值++i
{
_x++;
_y++;
return *this; //返回引用
}
Point Point::operator++(int)//后置++,int只是用来区分前缀或后缀
{
Point temp = *this;
++*this; //复用前缀++
return temp;
}
Point& Point::operator--()
{
_x--;
_y--;
return *this;
}
Point Point::operator--(int)
{
Point temp = *this;
--*this;
return temp;
}
ostream& operator<<(ostream& o, Point& p)
{
o << "(" << p._x << "," << p._y << ")";
return o;
}
int main()
{
Point p(1, 2);
cout << "p="<<p << endl;
cout << "p++="<<p++ << endl;
cout <<"++p="<< ++p << endl;
cout << "p--="<<p-- << endl;
cout << "--p="<<--p << endl;
return 0;
}
运行结果:
2.虚函数
#include <iostream>
using namespace std;
class Vehicle{
public:
int MaxSpped;
int Weight;
void Run(){ cout << "Vehicle run." << endl; }
void Stop(){ cout << "Vehicle stop." << endl; }
virtual void Run(){ cout << "Vehicle run." << endl; }
virtual void Stop(){ cout << "Vehicle stop." << endl; }
};
class Bicycle :virtual public Vehicle{
public:
int Height;
void Run(){ cout << "Bicycle run." << endl; }
void Stop(){ cout << "Bicycle stop." << endl; }
};
class Motorcar :virtual public Vehicle{
public:
int SeatNum;
void Run(){ cout << "Motorcar run." << endl; }
void Stop(){ cout << "Motorcar stop." << endl; }
};
class Motorcycle :public Bicycle, public Motorcar{
public:
void Run(){ cout << "Motorcycle run." << endl; }
void Stop(){ cout << "Motorcycle stop." << endl; }
};
int main()
{
Vehicle v;
v.Run();
v.Stop();
Bicycle b;
b.Run();
b.Stop();
Motorcar m;
m.Run();
m.Stop();
Motorcycle mc;
mc.Run();
mc.Stop();
Vehicle *vp = &v;
vp->Run();
vp->Stop();
vp = &b;
vp->Run();
vp->Stop();
vp = &m;
vp->Run();
vp->Stop();
vp = &mc;
vp->Run();
vp->Stop();
return 0;
}
(1).基类的Run()和Stop()非Virtual函数,运行结果:
(1).基类的Run()和Stop()为Virtual函数,运行结果:
虚函数和虚继承不是一回事,虚继承解决的是继承关系中变量冗余的问题,虚函数是解决类对象的多态问题的。
3.
在多态概念中,基类的指针既可以指向基类的对象,又可以指向派生类的对象。我们可以使用dynamic_cast类型转换操作符来判断当前指针(必须是多态类型)是否能够转换成为某个目的类型的指针。
同学们先查找dynamic_cast的使用说明(如http://en.wikipedia.org/wiki/Run-time_type_information#dynamic_cast),然后使用该类型转换操作符完成下面程序(该题无输入)。
函数int getVertexCount(Shape * b)计算b的顶点数目,若b指向Shape类型,返回值为0;若b指向Triangle类型,返回值为3;若b指向Rectangle类型,返回值为4。
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
class Shape{
public:
Shape() {}
virtual ~Shape() {}
};
class Triangle : public Shape{
public:
Triangle() {}
~Triangle() {}
};
class Rectangle : public Shape {
public:
Rectangle() {}
~Rectangle() {}
};
/*用dynamic_cast类型转换操作符完成该函数*/
int getVertexCount(Shape * b){
Rectangle* rectangle = dynamic_cast<Rectangle*>(b);
if (rectangle != nullptr)
return 4;
Triangle* triangle = dynamic_cast<Triangle*>(b);
if (triangle != nullptr)
return 3;
return 0;
}
int main() {
Shape s;
cout << getVertexCount(&s) << endl;
Triangle t;
cout << getVertexCount(&t) << endl;
Rectangle r;
cout << getVertexCount(&r) << endl;
}
来自清华大学MOOC课件