#include <iostream>
#include <assert.h>
using namespace std;
class Rectangle
{
public:
Rectangle(int h,int w) :height(h), widget(w) {}
~Rectangle() {}
virtual void setHeight(int newHeight) { height = newHeight; }
virtual void setWidth(int newWidth) { widget = newWidth; }
virtual int Height() const { return height; }
virtual int Widget() const { return widget; }
private:
int height;
int widget;
};
void makeBigger(Rectangle& r)
{
int oldHeight = r.Height();
r.setWidth(r.Widget() + 10);
if (r.Height() != oldHeight)
{
abort();
}
}
class Spuare:public Rectangle
{
public:
Spuare(int hs, int ws) :Rectangle(hs, ws) {}
~Spuare() {}
private:
};
class Base
{
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int);
virtual void mf2();
void mf3();
void mf3(double);
};
class Derived :public Base
{
public:
virtual void mf1();
void mf3();
void mf4();
using Base::mf1;
using Base::mf3;
};
class GameCharactor;
int defaultHealthCalc(const GameCharactor& gc);
class GameCharactor
{
public:
typedef int (*HealthCalcFunc) (const GameCharactor&);
GameCharactor();
~GameCharactor();
private:
};
GameCharactor::GameCharactor()
{
}
GameCharactor::~GameCharactor()
{
}
class Shape
{
public:
enum ShapeColor { Red,Green,Blue };
virtual void darw(ShapeColor color = Red) const = 0;
};
class Rect :public Shape
{
public:
virtual void draw(ShapeColor color = Green) const { color; };
};
class Circle :public Shape
{
public:
virtual void draw(ShapeColor color) const { color; };
};
int main()
{
//32:make sure public inheritance mo0dels is-a
Spuare s(5,5);
if (s.Widget() != s.Height())
{
abort();
}
makeBigger(s);
if (s.Widget() != s.Height())
{
abort();
}
//33 Avoided hiding inherited names
Derived d;
int x;
d.mf1();
d.mf1(1);// using base::mf1
//34 different between inheritance of inrterfgace and inheritance of impklerementaatinon
// 35 Consider alternatives to virtual functions
Shape* ps;
//Shape* pc = new Circle;
//Shape* pr = new Rect;
//38.Model has -a or is-implemented-in-terns-of through composition
// 39 use private inheritance judiciously
//40 use multiple inheritance judiciously
return 0;
}
effective cpp 4
最新推荐文章于 2022-03-02 11:47:15 发布