# 二、多态的实现

#include <iostream>
using namespace std;
class A
{
public:
virtual void Print() { cout << "A::Print" << endl; }
};
class B : public A
{
public:
virtual void Print() { cout << "B::Print" << endl; }
};
class D : public A
{
public:
virtual void Print() { cout << "D::Print" << endl; }
};
class E : public B
{
virtual void Print() { cout << "E::Print" << endl; }
};
int main()
{
A  a; B b; D d; E e;
A *pa = &a;  B *pb = &b;
pa->Print();    //多态， a.Print()被调用，输出：A::Print
pa = pb;        //基类指针pa指向派生类对象b
pa->Print();  //b.Print()被调用，输出：B::Print
pa = &d;       //基类指针pa指向派生类对象d
pa->Print();  //多态， d. Print ()被调用,输出：D::Print
pa = &e;       //基类指针pa指向派生类对象d
pa->Print();  //多态， e.Print () 被调用,输出：E::Print
return 0;
}


# 三、运算符重载

#### 1.运算符重载的意义

class Complex{

public:

Complex(double r=0.0,double i=0.0)  :  real(r) ,  imag(i){}

void display()   const;

private:
double  real;

double  imag;

};


#### 2.运算符重载的规则

Box operator+(const Box&);


Box operator+(const Box&, const Box&);


#### 代码实例：

#include <iostream>
using namespace std;
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
{
}
void setHeight( double hei )
{
height = hei;
}
// 重载 + 运算符，用于把两个 Box 对象相加
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.height = this->height + b.height;
return box;
}
private:
double length;      // 长度
double height;      // 高度
};
// 程序的主函数
int main( )
{
Box Box1;                // 声明 Box1，类型为 Box
Box Box2;                // 声明 Box2，类型为 Box
Box Box3;                // 声明 Box3，类型为 Box
double volume = 0.0;     // 把体积存储在该变量中
// Box1 详述
Box1.setLength(6.0);
Box1.setHeight(5.0);
// Box2 详述
Box2.setLength(12.0);
Box2.setHeight(10.0);
// Box1 的体积
volume = Box1.getVolume();
cout << "Volume of Box1 : " << volume <<endl;
// Box2 的体积
volume = Box2.getVolume();
cout << "Volume of Box2 : " << volume <<endl;
// 把两个对象相加，得到 Box3
Box3 = Box1 + Box2;
// Box3 的体积
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume <<endl;
return 0;
}


### 注意：

• .：成员访问运算符
• .*, ->*：成员指针访问运算符
• ::：域运算符
• sizeof：长度运算符
• ?:：条件运算符
• #： 预处理符号

# 四、虚函数

#include <iostream>
using namespace std;

//基类People
class People{
public:
People(char *name, int age);
void display();
protected:
char *m_name;
int m_age;
};
People::People(char *name, int age): m_name(name), m_age(age){}
void People::display(){
cout<<m_name<<"今年"<<m_age<<"岁了，是个无业游民。"<<endl;
}

//派生类Teacher
class Teacher: public People{
public:
Teacher(char *name, int age, int salary);
void display();
private:
int m_salary;
};
Teacher::Teacher(char *name, int age, int salary): People(name, age), m_salary(salary){}
void Teacher::display(){
cout<<m_name<<"今年"<<m_age<<"岁了，是一名教师，每月有"<<m_salary<<"元的收入。"<<endl;
}

int main(){
People *p = new People("王志刚", 23);
p -> display();

p = new Teacher("赵宏佳", 45, 8200);
p -> display();

return 0;
}


#include <iostream>
using namespace std;
//基类People
class People{
public:
People(char *name, int age);
virtual void display();  //声明为虚函数
protected:
char *m_name;
int m_age;
};
People::People(char *name, int age): m_name(name), m_age(age){}
void People::display(){
cout<<m_name<<"今年"<<m_age<<"岁了，是个无业游民。"<<endl;
}
//派生类Teacher
class Teacher: public People{
public:
Teacher(char *name, int age, int salary);
virtual void display();  //声明为虚函数
private:
int m_salary;
};
Teacher::Teacher(char *name, int age, int salary): People(name, age), m_salary(salary){}
void Teacher::display(){
cout<<m_name<<"今年"<<m_age<<"岁了，是一名教师，每月有"<<m_salary<<"元的收入。"<<endl;
}
int main(){
People *p = new People("王志刚", 23);
p -> display();
p = new Teacher("赵宏佳", 45, 8200);
p -> display();
return 0;
}


posted @ 2019-10-27 12:38  慕慕慕慕  阅读(129)  评论(0编辑  收藏  举报