CPPeffective3

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值