面向对象程序设计

面向对象程序设计

Peronal Link: http://segmentfault.com/a/1190000002464822

这节课讲了本门课程 面向对象程序设计中最为重要的一个部分 - 多态

 /*************************************************************************
> File Name: polymorphism.cpp
> Author: Jeremy Wu
> Created Time: Mon 25 May 2015 10:04:07 AM CST
> Function: tune every instrument
************************************************************************/ //多态:一个接口,多种实现
//发生多态现象的条件:
// 1.通过调用虚函数(基类中必须要有,派生类中可有可无建议加上)
// 2.借助指针或者引用调用该虚函数
//
//虚函数只能是 非静态 成员函数
//如果类中有虚函数,最好把 析构函数 也定义为虚函数
//
//如果基类的析构函数是虚函数,由它派生的所有的派生类的析构函数都 自动 成为虚函数
// ·构造函数无法定义为虚函数
//
//基类可以指向它的所有的派生类
//相当于基类给所有的派生类提供了统一的接口
//一般不会直接创建基类的对象
//
//{ 抽象基类 }
//如果一个类中包含纯虚函数,就是抽象类
//抽象类无法创建对象
//如果需要 避免 创建基类对象,应该使用纯虚函数 //接下来是一个例子,有一个基类叫做乐器类
//由它派生出Wind , Brass 和 Gitar 三个类 #include <iostream> using namespace std; class Instrument {
public:
virtual void play (); //virtual 关键字
Instrument () { cout << "Instement () " << endl; }
virtual ~Instrument () { cout << "~Instement () " << endl; }
}; void Instrument::play () { //类的外部定义前不用加 virtual 关键字
cout << "Instrument::play ()" << endl;
} class Wind : public Instrument{
virtual void play () { cout << "Wind::play () " << endl; } //派生类中 virtual 关键字可加可不加
~Wind () { cout << "Wind::~Wind ()" << endl; }
}; class Brass : public Instrument {
virtual void play () { cout << "Brass::play ()" << endl; }
~Brass () { cout << "Brass::~Brass () " << endl; }
}; class Gitar : public Instrument {
virtual void play () { cout << "Gitar::play ()" << endl; }
~Gitar () { cout << "Gitar::~Gitar ()" << endl; }
}; //inst 指针在定义的时候,是静态类型
//在程序运行过程中,它的动态类型就会根据指向对象的类型作改变
//
//有时候把动态绑定称为运行时绑定,也称后继绑定
//run-time binding void tune (Instrument * inst) { //引用传递 或者用指针
inst->play (); //Dynamic binding
//在编译时进行 动态绑定
} int main () { Instrument * p = new Gitar;
Instrument * p2 = new Wind; delete p;
delete p2; return ;
}

接下来老师出了一道题目

描述如下:

以下是我的 Solution:

 /*************************************************************************
> File Name: ployPROB.cpp
> Author: Jeremy Wu
> Created Time: Mon 25 May 2015 10:50:07 AM CST
************************************************************************/ //定义一个形状类的继承结构
//
//Shape -> Triangle
// -> Rectangle
// -> Circle
//
//给形状定义两个操作:
// getArea () 求面积
// getLength () 求周长 #include <iostream>
#include <cmath> const double PI = 3.1415926; using namespace std; class Shape {
public:
virtual double getArea () = ;
virtual double getLength () = ;
virtual ~Shape () { cout << "~Shape ()" << endl; }
Shape () { cout << "Shape ()" << endl; }
}; class Triangle : public Shape {
private:
double x, y, z;
public:
double getArea () {
double p = (x + y + z) / 2.0;
return sqrt (p * (p - x) * (p - y) * (p - z));
}
double getLength () { return x + y + z; }
Triangle (double ix = 0.0, double iy = 0.0, double iz = 0.0) {
x = ix, y = iy, z = iz;
}
~Triangle () { cout << "~Triangle () " << endl; }
}; class Rectangle : public Shape {
private:
double x, y;
public:
double getArea () { return x * y; }
double getLength () { return 2.0 * (x + y); }
Rectangle (double ix = 0.0, double iy = 0.0) {
x = ix, y = iy;
}
~Rectangle () { cout << "~Rectangle () " << endl; }
}; class Circle : public Shape {
private:
double r;
public:
double getArea () { return PI * r * r; }
double getLength () { return 2.0 * PI * r; }
Circle (double ir = 0.0) {
r = ir;
}
~Circle () { cout << "~Circle ()" << endl; }
}; void test (Shape & sp) {
cout << "/***********************************/" << endl;
cout << '\t' << "Area is " << sp.getArea () << endl;
cout << '\t' << "Length is " << sp.getLength () << endl;
cout << "/***********************************/" << endl;
} int main () { Circle p1 (3.0);
Rectangle p2 (3.0, 6.0);
Triangle p3 (1.0, 2.0, 1.5); test (p1);
test (p2);
test (p3); return ;
}

以下是官方版 Solution:

 /*************************************************************************
> File Name: example.cpp
> Author: Jeremy Wu
> Created Time: Mon 25 May 2015 11:34:47 AM CST
************************************************************************/ #include <iostream>
#include <cmath> using namespace std; class Shape {
public:
virtual double area () = ; //不实现基类可以这么写,表示定义一个纯虚函数
virtual double zhou () = ; //pure virtual function
}; class Triangle : public Shape {
double a, b, c;
public:
Triangle (double aa, double bb, double cc) : a (aa), b (bb), c (cc) {}
virtual double area () {
double p = 0.5 * (a + b + c);
return sqrt (p * (p - a) * (p - b) * (p - c));
}
virtual double zhou () {
return a + b + c;
}
}; class Rectangle : public Shape {
double a, b;
public:
Rectangle (double aa, double bb) : a (aa), b (bb) {}
virtual double area () {
return a * b;
}
virtual double zhou () {
return 2.0 * (a + b);
}
}; class Circle : public Shape {
double r;
public:
Circle (double rr) : r (rr) {}
virtual double area () {
return 3.14159 * r * r;
}
virtual double zhou () {
return 2.0 * 3.14159 * r;
}
}; void printArea (Shape * p) {
cout << p->area () << endl;
} void printZhou (Shape * p) {
cout << p->zhou () << endl;
} int main () { Shape * p1, * p2, * p3; //Shape sp; p1 = new Triangle (, , );
p2 = new Rectangle (, 4.6);
p3 = new Circle (5.8); printArea (p1);
printArea (p2);
printArea (p3); printZhou (p1);
printZhou (p2);
printZhou (p3); return ;
}
04-14 22:55