#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;
}
c++ effective3
最新推荐文章于 2022-03-02 11:47:15 发布