c++ effective3


#include <string>
#include <iostream>
#include <vector>
//using  std::string; //using namespace std:
using namespace std;
class Person
{
public:
	Person();
	virtual ~Person();

private:
	string name;
	string address;
};

Person::Person()
{
}

Person::~Person()
{
}
class Student
{
public:
	Student();
	~Student();

private:
	string schoolName;
	string schoolAddress;
};

Student::Student()
{
}

Student::~Student()
{
}
class Window
{
public:
	Window();
	~Window();
	string name()const;
	virtual void dispaly() const;
private:

};

Window::Window()
{
}

Window::~Window()
{
}
class WIndowWithScrollBars:public Window
{
public:
	WIndowWithScrollBars();
	~WIndowWithScrollBars();
	virtual void display() const
	{
		Window::dispaly();//static_cast<Window>(*this).dispaly();
	}
private:

};


bool ValidateStudent(Student s);
bool ValiateStudent(const Student& s);

void PrintNameAndDisPlay(Window w)
{
	cout << w.name() << endl;//
	w.dispaly();				//error  forever call Window display

}void PrintNameAndDisplay(const Window& w)
{
	cout << w.name() << endl;
	w.dispaly();
}
class Rational
{
public:
	Rational();
	~Rational();

private:
	int n, d;
	friend const Rational operator*(const Rational& lhs, const Rational& rhs);
	//friend const Rational& operator*(const Rational& lhs, const Rational& rhs);
};
//const Rational& operator*(const Rational& lhs, const Rational& rhs)
{
	//Rational result(lhs.n * rhs.n, lhs.d * rhs.d);
	//Rational* result = new Rational(lhs.n * rhs.n, lhs.n * rhs.d);
	//static Rational result = Rational(lhs.n * rhs.n, lhs.d* rhs.d)
	//return result;
}
Rational::Rational()
{
}

Rational::~Rational()
{
}
//bool opertator== (const Rational & lhs, const Rational & rhs);
inline const Rational operator*(const Rational& lhs, const Rational& rhs)
{
	//return Rational(lhs.n*rhs.n,lhs.d*rhs.d)
}
class AccessLevels
{
public:
	AccessLevels();
	~AccessLevels();
	int getReadOnly()const { return readOnly; }
	void setReadWrite(int value) { readWrite =value; }

private:
	int noAccess;
	int readOnly;
	int readWrite;
	int writeOnly;
};

AccessLevels::AccessLevels()
{
}

AccessLevels::~AccessLevels()
{
}
namespace WebBrowserStuff{

	class WebBrowser
	{
	public:
		WebBrowser();
		~WebBrowser();
		void clearCache();
		void clearHistory();
		void removeCookies();
		void clearEverything();
	private:

	};
void clearBrower(WebBrowser& wb) {
	wb.clearCache();
	wb.clearHistory();
	wb.removeCookies();
}

WebBrowser::WebBrowser()
{
}

WebBrowser::~WebBrowser()
{
}
}
namespace WebbrowserStuff
{

}
string encryptPassword(const string& password)
{
	//string encrypted;//error
	if (password.length() < 10)
	{
		return;
	}
	string encrypted(password);
	//encrypted = password;
	//encryp(encrypted)
	return encrypted;
}
class Widget
{
public:
	Widget();
	~Widget();
	explicit Widget(int size);
private:

};

Widget::Widget()
{
}

Widget::~Widget()
{
}
class Point
{
public:
	Point();
	~Point();

	void setX(int newVal);
	void setY(int newVal);
	struct RectData
	{
		Point ulhc;
		Point lrhc;
	};

private:

};

Point::Point()
{
}

Point::~Point()
{
}
class Rectangle
{
public:
	Rectangle();
	~Rectangle();
	const Point& upperLeft() const { return pData->ulhc; }//const
	Point& lowerRight() const { return pData->lrhc; }
private:
	std::aoto_ptr<RectData>pData;
};
Rectangle::Rectangle()
{
}

Rectangle::~Rectangle()
{
}
class GUIObject {};
void doSomeWork(const Widget& w);
const Rectangle boundingBox(const GUIObject& obj);
int main()
{

	//17 provision make interfaces friend
	//18 provision class design type
	//19 provision  prefer pass-by-reference-to-const to pass-by-value
	Student plato;
	bool platoIsOK = ValidateStudent(plato);
	WIndowWithScrollBars wwsb;
	PrintNameAndDisPlay(wwsb);
	//20.provision Donot try return a reference when you must return an object
	//22 provision declare data members private
	AccessLevels ac;
	//23 preter non-member non -friend function to member function
	WebBrowserStuff::WebBrowser wb;
	//24 declare non membner funtions when type converssion should apply paraments
	//25 consider support for a non_throwing swap

	//27 POstpone variable definitions as long as possible
	//28 Minimize casting
	doSomeWork(Widget(15));
	doSomeWork(static_cast<Widget>(15));
	typedef std::vector<std::auto_ptr<Window>> VPW;
	VPW winPtrs;
	for (VPW::iterator iter = winPtrs.begin(); iter!= winPtrs.end(); iter++)
	{
		/*if (WIndowWithScrollBars* psw = dynamic_cast<WIndowWithScrollBars*>(iter->get())
		{
			psw->display();
		}*/
		(*iter)->display();

	}
	//29 Avoid returning handles to object internals
	GUIObject* pgo;
	const Point* pUpperLeft = &(boundingBox(*pgo).upperLeft);

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值