Android C++ 学习笔记2

目录

09th_operator

person.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Person {
private:
	static int cnt;
	char *name;
	int age;
	char *work;

public:

	static int getCount(void); 

	Person() {//cout <<"Pserson()"<<endl;
		name = NULL;
		work = NULL;
		cnt++;
	}
	Person(char *name) 
	{
		//cout <<"Pserson(char *)"<<endl;
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
		this->work = NULL;
		cnt++;
	}

	Person(char *name, int age, char *work = "none") 
	{
		cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
		this->age = age;

		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);

		this->work = new char[strlen(work) + 1];
		strcpy(this->work, work);
		cnt++;
	}

	Person(const Person &per) 
	{
		cout <<"Pserson(Person &)"<<endl;
		this->age = per.age;

		this->name = new char[strlen(per.name) + 1];
		strcpy(this->name, per.name);

		this->work = new char[strlen(per.work) + 1];
		strcpy(this->work, per.work);
		cnt++;
	}

	~Person()
	{
		cout << "~Person()"<<endl;
		if (this->name) {
			cout << "name = "<<name<<endl;
			delete this->name;
		}
		if (this->work) {
			cout << "work = "<<work<<endl;
			delete this->work;
		}
	}

	void setName(char *n)
	{
		name = n;
	}
	int setAge(int a)
	{
		if (a < 0 || a > 150)
		{
			age = 0;
			return -1;
		}
		age = a;
		return 0;
	}
	void printInfo(void) const
	{
		//printf("name = %s, age = %d, work = %s\n", name, age, work); 
		cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
	}

	Person& operator=(const Person& p)
	{
		cout << "operator=(const Person& p)"<<endl;
	
		if (this == &p)
			return *this;
		this->age = p.age;

		if (this->name) {
			delete this->name;
		}
		if (this->work) {
			delete this->work;
		}

		this->name = new char[strlen(p.name) + 1];
		strcpy(this->name, p.name);
		
		this->work = new char[strlen(p.work) + 1];
		strcpy(this->work, p.work);

		return *this;
		
	}
	
};

int Person::cnt = 0; /* 定义和初始化 */

int Person::getCount(void) 
{ 
	return cnt; 
}


int main(int argc, char **argv)
{
	const Person p1("zhangsan", 10);

	cout<<"Person p2 = p1" <<endl;
	Person p2 = p1;
	
	Person p3;

	cout<<"p3=p1"<<endl;
	p3 = p1;
	cout<<"end"<<endl;
	p1.printInfo();
	p2.printInfo();
	p3.printInfo();

	

	return 0;
}

point.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() {}
	Point(int x, int y) : x(x), y(y) {}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);
	friend Point operator+(Point &p1, Point &p2);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

Point operator+(Point &p1, Point &p2)
{
	cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}


int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point p2(2, 3);

	Point sum = p1+p2;
	sum.printInfo();

	return 0;
}

point2.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() {}
	Point(int x, int y) : x(x), y(y) {}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);
	friend Point operator+(Point &p1, Point &p2);
	friend Point operator++(Point &p);
	friend Point operator++(Point &p, int a);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

Point operator+(Point &p1, Point &p2)
{
	cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

/* Point p(1,2); ++p; */
Point operator++(Point &p)
{
	cout<<"++p"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
	cout<<"p++"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;	
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point p2(2, 3);

	Point n = ++p1;
	n.printInfo();
	p1.printInfo();

	cout << "******************"<<endl;

	Point m = p2++;
	m.printInfo();
	p2.printInfo();

	return 0;
}

point3.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() 
	{
		cout<<"Point()"<<endl;
	}
	Point(int x, int y) : x(x), y(y) 
	{
		cout<<"Point(int x, int y)"<<endl;
	}

	Point(const Point& p)
	{
		cout<<"Point(const Point& p)"<<endl;
		x = p.x;
		y = p.y;
	}
	~Point() 
	{
		cout<<"~Point()"<<endl;
	}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);
	friend Point operator+(Point &p1, Point &p2);
	friend Point operator++(Point &p);
	friend Point operator++(Point &p, int a);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

Point operator+(Point &p1, Point &p2)
{
	cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

/* Point p(1,2); ++p; */
Point operator++(Point &p)
{
	cout<<"++p"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
	cout<<"p++"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;	
}

int main(int argc, char **argv)
{
	Point p1(1, 2);

	cout<<"begin"<<endl;
	++p1;
	cout << "******************"<<endl;

	p1++;
	cout<<"end"<<endl;


	return 0;
}

point4.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() 
	{
		cout<<"Point()"<<endl;
	}
	Point(int x, int y) : x(x), y(y) 
	{
		cout<<"Point(int x, int y)"<<endl;
	}

	Point(const Point& p)
	{
		cout<<"Point(const Point& p)"<<endl;
		x = p.x;
		y = p.y;
	}
	~Point() 
	{
		cout<<"~Point()"<<endl;
	}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);
	friend Point operator+(Point &p1, Point &p2);
	friend Point& operator++(Point &p);
	friend Point operator++(Point &p, int a);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

Point operator+(Point &p1, Point &p2)
{
	cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

/* Point p(1,2); ++p; */
Point& operator++(Point &p)
{
	cout<<"++p"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
	cout<<"p++"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;	
}

int main(int argc, char **argv)
{
	Point p1(1, 2);

	cout<<"begin"<<endl;
	++p1;
	cout << "******************"<<endl;

	p1++;
	cout<<"end"<<endl;


	return 0;
}

point5.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() 
	{
		cout<<"Point()"<<endl;
	}
	Point(int x, int y) : x(x), y(y) 
	{
		cout<<"Point(int x, int y)"<<endl;
	}

	Point(const Point& p)
	{
		cout<<"Point(const Point& p)"<<endl;
		x = p.x;
		y = p.y;
	}
	~Point() 
	{
		cout<<"~Point()"<<endl;
	}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);
	friend Point operator+(Point &p1, Point &p2);
	friend Point& operator++(Point &p);
	friend Point operator++(Point &p, int a);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

Point operator+(Point &p1, Point &p2)
{
	cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

/* Point p(1,2); ++p; */
Point& operator++(Point &p)
{
	cout<<"++p"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
	cout<<"p++"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;	
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point m, n;

	cout<<"begin"<<endl;
	m = ++p1;
	cout << "******************"<<endl;

	n = p1++;
	cout<<"end"<<endl;


	return 0;
}

point6.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() 
	{
		cout<<"Point()"<<endl;
	}
	Point(int x, int y) : x(x), y(y) 
	{
		cout<<"Point(int x, int y)"<<endl;
	}

	Point(const Point& p)
	{
		cout<<"Point(const Point& p)"<<endl;
		x = p.x;
		y = p.y;
	}
	~Point() 
	{
		cout<<"~Point()"<<endl;
	}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);
	friend Point operator+(Point &p1, Point &p2);
	friend Point& operator++(Point &p);
	friend Point operator++(Point &p, int a);
	friend ostream& operator<<(ostream &o, Point p);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

Point operator+(Point &p1, Point &p2)
{
	cout<<"Point operator+(Point &p1, Point &p2)"<<endl;
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

/* Point p(1,2); ++p; */
Point& operator++(Point &p)
{
	cout<<"++p"<<endl;
	p.x += 1;
	p.y += 1;
	return p;
}

/* Point p(1,2); p++; */
Point operator++(Point &p, int a)
{
	cout<<"p++"<<endl;
	Point n;
	n = p;
	p.x += 1;
	p.y += 1;
	return n;	
}

ostream& operator<<(ostream &o, Point p)
{
	cout<<"("<<p.x<<", "<<p.y<<")";
	return o;
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point m, n;

	cout<<"begin"<<endl;
	m = ++p1;
	cout << "******************"<<endl;

	n = p1++;
	cout<<"end"<<endl;

	cout<<m<<" "<<n<<endl;

	return 0;
}

point7.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() 
	{
		cout<<"Point()"<<endl;
	}
	Point(int x, int y) : x(x), y(y) 
	{
		cout<<"Point(int x, int y)"<<endl;
	}

	Point(const Point& p)
	{
		cout<<"Point(const Point& p)"<<endl;
		x = p.x;
		y = p.y;
	}
	~Point() 
	{
		cout<<"~Point()"<<endl;
	}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}

	Point operator+(Point &p)
	{
		cout<<"operator+"<<endl;
		Point n;
		n.x = this->x + p.x;
		n.y = this->y + p.y;
		return n;
	}
	
	/* Point p(1,2); ++p; */
	Point& operator++(void)
	{
		cout<<"operator++(void)"<<endl;
		this->x += 1;
		this->y += 1;
		return *this;
	}
	
	/* Point p(1,2); p++; */
	Point operator++(int a)
	{
		cout<<"operator++(int a)"<<endl;
		Point n;
		n = *this;
		this->x += 1;
		this->y += 1;
		return n;	
	}

	friend Point add(Point &p1, Point &p2);
	friend ostream& operator<<(ostream &o, Point p);
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}


ostream& operator<<(ostream &o, Point p)
{
	cout<<"("<<p.x<<", "<<p.y<<")";
	return o;
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point p2(2, 3);
	Point m, n;

	m = p1 + p2; /* m = p1.operator+(p2); */
	cout<<"add p1,p2 = "<<m<<endl;

	cout<<"begin"<<endl;
	m = ++p1;    /* m = p1.operator++(); */
	cout<<"m = "<<m<<" "<<"p1 = "<<p1<<endl;
	cout << "******************"<<endl;

	n = p1++;    /* m = p1.operator++(0); */
	cout<<"n = "<<n<<" "<<"p1 = "<<p1<<endl;
	cout<<"end"<<endl;

	return 0;
}

10th_inheritance

father_son.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Father {
private:
	int money;
public:
	void it_skill(void)
	{
		cout<<"father's it skill"<<endl;
	}

	int getMoney(void)
	{
		return money;
	}

	void setMoney(int money)
	{
		this->money = money;
	}
};

class Son : public Father {
private:
	int toy;
public:
	void play_game(void)
	{
		int m;
		
		cout<<"son paly game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);
	}
};


int main(int argc, char **argv)
{
	Son s;

	s.setMoney(10);
	cout << s.getMoney()<<endl;

	s.it_skill();
	s.play_game();
	
	return 0;
}

father_son2.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Father {
private:
	int money;

protected:
	int room_key;
	
public:
	void it_skill(void)
	{
		cout<<"father's it skill"<<endl;
	}

	int getMoney(void)
	{
		return money;
	}

	void setMoney(int money)
	{
		this->money = money;
	}
};

class Son : public Father {
private:
	int toy;
public:
	void play_game(void)
	{
		int m;
		
		cout<<"son paly game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		/* 外人不能拿父亲的房间钥匙
		 * 儿子可以
		 */
		room_key = 1; 
	}
};


int main(int argc, char **argv)
{
	Son s;

	s.setMoney(10);
	cout << s.getMoney()<<endl;

	s.it_skill();
	s.play_game();

	//s.room_key = 1;
	
	return 0;
}

father_son3.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Father {
private:
	int money;

protected:
	int room_key;
	
public:
	void it_skill(void)
	{
		cout<<"father's it skill"<<endl;
	}

	int getMoney(void)
	{
		return money;
	}

	void setMoney(int money)
	{
		this->money = money;
	}
};

class Son : public Father {
private:
	int toy;
	//using Father::it_skill;
public:
	using Father::room_key;
	//using Father::money;
	
	void play_game(void)
	{
		int m;
		
		cout<<"son paly game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}
};


int main(int argc, char **argv)
{
	Son s;

	s.setMoney(10);
	cout << s.getMoney()<<endl;

	s.it_skill();
	s.play_game();

	s.room_key = 1;
	
	return 0;
}

father_son4.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Father {
private:
	int money;

protected:
	int room_key;
	
public:
	int address;
	
	void it_skill(void)
	{
		cout<<"father's it skill"<<endl;
	}

	int getMoney(void)
	{
		return money;
	}

	void setMoney(int money)
	{
		this->money = money;
	}
};

class Son_pub : public Father {
private:
	int toy;
public:
	
	void play_game(void)
	{
		int m;
		
		cout<<"son play game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}
};


class Son_pro : protected Father {
private:
	int toy;
public:
	
	void play_game(void)
	{
		int m;
		
		cout<<"son play game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}
};


class Son_pri : private Father {
private:
	int toy;
public:
	
	void play_game(void)
	{
		int m;
		
		cout<<"son play game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}
};


int main(int argc, char **argv)
{
	Son_pub s_pub;
	Son_pro s_pro;
	Son_pri s_pri;

	s_pub.play_game();
	s_pro.play_game();
	s_pri.play_game();


	s_pub.it_skill();
	//s_pro.it_skill();  // error
	//s_pri.it_skill();	 // error

	return 0;
}

father_son5.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Father {
private:
	int money;

protected:
	int room_key;
	
public:
	int address;
	
	void it_skill(void)
	{
		cout<<"father's it skill"<<endl;
	}

	int getMoney(void)
	{
		return money;
	}

	void setMoney(int money)
	{
		this->money = money;
	}
};

class Son_pub : public Father {
private:
	int toy;
public:
	
	void play_game(void)
	{
		int m;
		
		cout<<"son play game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}
};


class Son_pro : protected Father {
private:
	int toy;
public:
	
	void play_game(void)
	{
		int m;
		
		cout<<"son play game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}
};


class Son_pri : private Father {
private:
	int toy;
public:
	
	void play_game(void)
	{
		int m;
		
		cout<<"son play game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}
};

class Grandson_pub : public Son_pub {

public:
	void test(void) {
		room_key = 1; /* address is protected */ 
		address  = 2; /* address is public */ 
	}
};

class Grandson_pro : public Son_pro {

public:
	void test(void) {
		room_key = 1; /* address is protected */ 
		address  = 2; /* address is protected */ 
	}
};

class Grandson_pri : public Son_pri {

public:
	void test(void) {
		//room_key = 1;  // error
		//address  = 2;  // error
	}
};


int main(int argc, char **argv)
{
	Son_pub s_pub;
	Son_pro s_pro;
	Son_pri s_pri;

	s_pub.play_game();
	s_pro.play_game();
	s_pri.play_game();


	s_pub.it_skill();
	//s_pro.it_skill();  // error
	//s_pri.it_skill();	 // error

	Grandson_pub gs_pub;
	Grandson_pro gs_pro;

	gs_pub.address = 2;
	//gs_pro.address = 2;  // error

	return 0;
}

father_son6.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Father {
private:
	int money;

protected:
	int room_key;
	
public:
	void it_skill(void)
	{
		cout<<"father's it skill"<<endl;
	}

	int getMoney(void)
	{
		return money;
	}

	void setMoney(int money)
	{
		this->money = money;
	}
};

class Son : public Father {
private:
	int toy;
	//using Father::it_skill;
public:
	using Father::room_key;
	//using Father::money;
	
	void play_game(void)
	{
		int m;
		
		cout<<"son paly game"<<endl;

		/* money -= 1; 
		 * 错: 不能直接拿父亲的私房钱
		 */

		/*
		 * 但是可以问他要
		 */
		m = getMoney();
		m--;
		setMoney(m);

		room_key = 1; 
	}

	/* 覆写 override */
	void it_skill(void)
	{
		cout<<"son's it skill"<<endl;
	}
	
};


int main(int argc, char **argv)
{
	Son s;

	s.setMoney(10);
	cout << s.getMoney()<<endl;

	s.it_skill();
	s.play_game();

	s.room_key = 1;
	
	return 0;
}

person.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Person {
private:
	static int cnt;
	char *name;
	int age;

public:

	static int getCount(void); 

	Person() {//cout <<"Pserson()"<<endl;
		name = NULL;
		cnt++;
	}
	Person(char *name) 
	{
		//cout <<"Pserson(char *)"<<endl;
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
		cnt++;
	}

	Person(char *name, int age) 
	{
		cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
		this->age = age;

		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);

		cnt++;
	}

	Person(Person &per) 
	{
		cout <<"Pserson(Person &)"<<endl;
		this->age = per.age;

		this->name = new char[strlen(per.name) + 1];
		strcpy(this->name, per.name);

		cnt++;
	}

	~Person()
	{
		cout << "~Person()"<<endl;
		if (this->name) {
			cout << "name = "<<name<<endl;
			delete this->name;
		}
	}

	void setName(char *name)
	{
		if (this->name) {
			delete this->name;
		}
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
	}
	int setAge(int a)
	{
		if (a < 0 || a > 150)
		{
			age = 0;
			return -1;
		}
		age = a;
		return 0;
	}
	void printInfo(void)
	{
		cout<<"name = "<<name<<", age = "<<age<<endl;
	}
};

int Person::cnt = 0; /* 定义和初始化 */

int Person::getCount(void) 
{ 
	return cnt; 
}

class Student : public Person {
};


int main(int argc, char **argv)
{
	Student s;
	s.setName("zhangsan");
	s.setAge(16);
	s.printInfo();

	return 0;
}

person_student.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Person {
private:
	char *name;
	int age;

public:
	int address;
	
	Person() {//cout <<"Pserson()"<<endl;
		name = NULL;
	}
	Person(char *name) 
	{
		//cout <<"Pserson(char *)"<<endl;
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
	}

	Person(char *name, int age) 
	{
		cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
		this->age = age;

		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
	}

	Person(Person &per) 
	{
		cout <<"Pserson(Person &)"<<endl;
		this->age = per.age;

		this->name = new char[strlen(per.name) + 1];
		strcpy(this->name, per.name);
	}

	~Person()
	{
		cout << "~Person()"<<endl;
		if (this->name) {
			cout << "name = "<<name<<endl;
			delete this->name;
		}
	}

	void setName(char *name)
	{
		if (this->name) {
			delete this->name;
		}
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
	}
	int setAge(int a)
	{
		if (a < 0 || a > 150)
		{
			age = 0;
			return -1;
		}
		age = a;
		return 0;
	}
	void printInfo(void)
	{
		cout<<"name = "<<name<<", age = "<<age<<endl;
	}
};


class Student : public Person {
private:
	int grade;
	void setGrade(int grade) {this->grade = grade;}
	int getGrade(void) {return grade;}
public:
	void printInfo(void)
	{
		cout<<"Student ";
		Person::printInfo();
	}
};

void test_func(Person &p)
{
	p.printInfo();
}

int main(int argc, char **argv)
{
	Person p("lisi", 16);

	Student s;
	s.setName("zhangsan");
	s.setAge(16);

	test_func(p);
	test_func(s); /* Person &p = s里面的Person部分;
	               * p引用的是"s里面的Person部分"
	               */
	s.printInfo();

	return 0;
}

11th_multi_inheritance

Sofabed.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Sofa {
public:
	void watchTV(void) { cout<<"watch TV"<<endl; }
};

class Bed {
public:
	void sleep(void) { cout<<"sleep"<<endl; }
};

class Sofabed : public Sofa, public Bed {
};

int main(int argc, char **argv)
{
	Sofabed s;
	s.watchTV();
	s.sleep();
	return 0;
}

Sofabed2.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Sofa {
private:
	int weight;
public:
	void watchTV(void) { cout<<"watch TV"<<endl; }

	void setWeight(int weight) { this->weight = weight; }
	int getWeight(void) const { return weight; }
};

class Bed {
	private:
		int weight;
public:
	void sleep(void) { cout<<"sleep"<<endl; }
	void setWeight(int weight) { this->weight = weight; }
	int getWeight(void) const { return weight; }
};

class Sofabed : public Sofa, public Bed {
};

int main(int argc, char **argv)
{
	Sofabed s;
	s.watchTV();
	s.sleep();

	//s.setWeight(100); /* error, 有二义性 */
	s.Sofa::setWeight(100);
	
	return 0;
}

Sofabed3.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Furniture {
private:
	int weight;
public:
	void setWeight(int weight) { this->weight = weight; }
	int getWeight(void) const { return weight; }
};

class Sofa : virtual public Furniture {
private:
	int a;
public:
	void watchTV(void) { cout<<"watch TV"<<endl; }
};

class Bed : virtual public Furniture {
private:
	int b;
public:
	void sleep(void) { cout<<"sleep"<<endl; }
};

class Sofabed : public Sofa, public Bed {
private:
	int c;
};

int main(int argc, char **argv)
{
	Sofabed s;
	s.watchTV();
	s.sleep();

	s.setWeight(100);
	
	return 0;
}

12th_constructor_2

Sofabed.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Furniture {
private:
	int weight;
public:
	void setWeight(int weight) { this->weight = weight; }
	int getWeight(void) const { return weight; }
public:
	Furniture() { cout <<"Furniture()"<<endl; }
};

class Vertification3C {
public:
	Vertification3C() { cout <<"Vertification3C()"<<endl; }
};

class Sofa : virtual public Furniture , virtual public Vertification3C{
private:
	int a;
public:
	void watchTV(void) { cout<<"watch TV"<<endl; }
public:
	Sofa() { cout <<"Sofa()"<<endl; }
};

class Bed : virtual public Furniture, virtual public Vertification3C {
private:
	int b;
public:
	void sleep(void) { cout<<"sleep"<<endl; }
public:
	Bed() { cout <<"Bed()"<<endl; }
};

class Sofabed : public Sofa, public Bed {
private:
	int c;
public:
	Sofabed() { cout <<"Sofabed()"<<endl; }
};

class LeftRightCom {
public:
	LeftRightCom() { cout <<"LeftRightCom()"<<endl; }
};

class Date {
public:
	Date() { cout <<"Date()"<<endl; }
};

class Type {
public:
	Type() { cout <<"Type()"<<endl; }
};


class LeftRightSofabed : public Sofabed, public LeftRightCom {
private:
	Date date;
	Type type;

public:
	LeftRightSofabed() { cout <<"LeftRightSofabed()"<<endl; }
	
};

int main(int argc, char **argv)
{
	LeftRightSofabed s;
	return 0;
}

Sofabed2.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Furniture {
private:
	int weight;
public:
	void setWeight(int weight) { this->weight = weight; }
	int getWeight(void) const { return weight; }
public:
	Furniture() { cout <<"Furniture()"<<endl; }
};

class Vertification3C {
public:
	Vertification3C() { cout <<"Vertification3C()"<<endl; }
};

class Sofa : virtual public Furniture , virtual public Vertification3C{
private:
	int a;
public:
	void watchTV(void) { cout<<"watch TV"<<endl; }
public:
	Sofa() { cout <<"Sofa()"<<endl; }
};

class Bed : virtual public Furniture, virtual public Vertification3C {
private:
	int b;
public:
	void sleep(void) { cout<<"sleep"<<endl; }
public:
	Bed() { cout <<"Bed()"<<endl; }
};

class Sofabed : public Sofa, public Bed {
private:
	int c;
public:
	Sofabed() { cout <<"Sofabed()"<<endl; }
};

class LeftRightCom {
public:
	LeftRightCom() { cout <<"LeftRightCom()"<<endl; }
};

class Date {
public:
	Date() { cout <<"Date()"<<endl; }
};

class Type {
public:
	Type() { cout <<"Type()"<<endl; }
};


class LeftRightSofabed : public Sofabed, virtual public LeftRightCom {
private:
	Date date;
	Type type;

public:
	LeftRightSofabed() { cout <<"LeftRightSofabed()"<<endl; }
	
};

int main(int argc, char **argv)
{
	LeftRightSofabed s;
	return 0;
}

Sofabed3.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Furniture {
private:
	int weight;
public:
	void setWeight(int weight) { this->weight = weight; }
	int getWeight(void) const { return weight; }
public:
	Furniture() { cout <<"Furniture()"<<endl; }
};

class Vertification3C {
public:
	Vertification3C() { cout <<"Vertification3C()"<<endl; }
};

class Sofa : virtual public Furniture , virtual public Vertification3C{
private:
	int a;
public:
	void watchTV(void) { cout<<"watch TV"<<endl; }
public:
	Sofa() { cout <<"Sofa()"<<endl; }
};

class Bed : virtual public Furniture, virtual public Vertification3C {
private:
	int b;
public:
	void sleep(void) { cout<<"sleep"<<endl; }
public:
	Bed() { cout <<"Bed()"<<endl; }
};

class Sofabed : public Sofa, public Bed {
private:
	int c;
public:
	Sofabed() { cout <<"Sofabed()"<<endl; }
	Sofabed(char *abc) { cout <<"Sofabed(char *abc)"<<endl; }
};

class LeftRightCom {
public:
	LeftRightCom() { cout <<"LeftRightCom()"<<endl; }
	LeftRightCom(char *abc) { cout <<"LeftRightCom(char *abc)"<<endl; }
};

class Date {
public:
	Date() { cout <<"Date()"<<endl; }
	Date(char *abc) { cout <<"Date(char *abc)"<<endl; }
};

class Type {
public:
	Type() { cout <<"Type()"<<endl; }
	Type(char *abc) { cout <<"Type(char *abc)"<<endl; }
};


class LeftRightSofabed : public Sofabed, virtual public LeftRightCom {
private:
	Date date;
	Type type;

public:
	LeftRightSofabed() { cout <<"LeftRightSofabed()"<<endl; }
	LeftRightSofabed(char *str1, char *str2, char *str3) : Sofabed(str1), LeftRightCom(str2), date(str3) { cout <<"LeftRightSofabed()"<<endl; }
	
};

int main(int argc, char **argv)
{
	LeftRightSofabed s("abc", "2343", "yyy");
	return 0;
}

13th_Polymorphism

Human.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
public:
	void eating(void) { cout<<"use hand to eat"<<endl; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
};

void test_eating(Human& h)
{
	h.eating();
}

int main(int argc, char **argv)
{
	Human h;
	Englishman e;
	Chinese c;

	test_eating(h);
	test_eating(e);
	test_eating(c);

	return 0;
}

Human2.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
};

void test_eating(Human& h)
{
	h.eating();
}

int main(int argc, char **argv)
{
	Human h;
	Englishman e;
	Chinese c;

	test_eating(h);
	test_eating(e);
	test_eating(c);

	cout<<"sizeof(Human) = "<<sizeof(h)<<endl;
	cout<<"sizeof(Englishman) = "<<sizeof(e)<<endl;
	cout<<"sizeof(Chinese) = "<<sizeof(c)<<endl;

	return 0;
}

Human3.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
};

void test_eating(Human h)
{
	h.eating();
}

int main(int argc, char **argv)
{
	Human h;
	Englishman e;
	Chinese c;

	test_eating(h);
	test_eating(e);
	test_eating(c);

	cout<<"sizeof(Human) = "<<sizeof(h)<<endl;
	cout<<"sizeof(Englishman) = "<<sizeof(e)<<endl;
	cout<<"sizeof(Chinese) = "<<sizeof(c)<<endl;

	return 0;
}

Human4.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
	virtual ~Human() { cout<<"~Human()"<<endl; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
};

void test_eating(Human h)
{
	h.eating();
}

int main(int argc, char **argv)
{
	Human* h = new Human;
	Englishman* e = new Englishman;
	Chinese* c = new Chinese;

	Human *p[3] = {h, e, c};
	int i;

	for (i = 0; i < 3; i++)
	{
		p[i]->eating();
		delete p[i];
	}


	return 0;
}

Human5.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual void test(void) {cout<<"Human's test"<<endl; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual int test(void) {cout<<"Englishman's test"<<endl; return 1; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual int test(void) {cout<<"Chinese's test"<<endl; return 1; }
};

void test_eating(Human& h)
{
	h.eating();
}

void test_return(Human& h)
{
	h.test();
}



int main(int argc, char **argv)
{
	Human h;
	Englishman e;
	Chinese c;

	test_return(h);
	test_return(e);
	test_return(c);


	return 0;
}

Human6.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};

void test_eating(Human& h)
{
	h.eating();
}

void test_return(Human& h)
{
	h.test();
}



int main(int argc, char **argv)
{
	Human h;
	Englishman e;
	Chinese c;

	test_return(h);
	test_return(e);
	test_return(c);


	return 0;
}

14th_convert

convert.c

cpp 复制代码
#include <stdio.h>

int main(int argc, char **argv)
{
	double d = 100.1;
	int i = d;  // doubleתΪint
	char *str = "100ask.taobao.com";
	int *p = str; // char *תΪint * 

	printf("i = %d, str = 0x%x, p = 0x%x\n", i, str, p);

	return 0;
}

convert2.c

cpp 复制代码
#include <stdio.h>

int main(int argc, char **argv)
{
	double d = 100.1;
	int i = d;  // doubleתΪint
	char *str = "100ask.taobao.com";
	int *p = (int *)str; // char *תΪint * 

	printf("i = %d, str = 0x%x, p = 0x%x\n", i, (unsigned int)str, (unsigned int)p);

	return 0;
}

convert3.cpp

cpp 复制代码
#include <stdio.h>

int main(int argc, char **argv)
{
	double d = 100.1;
	int i = d;  // doubleתΪint
	char *str = "100ask.taobao.com";
	int *p = reinterpret_cast<int *>(str); // char *תΪint * 

	printf("i = %d, str = 0x%x, p = 0x%x\n", i, reinterpret_cast<unsigned int>(str), reinterpret_cast<unsigned int>(p));

	return 0;
}

convert4.cpp

cpp 复制代码
#include <stdio.h>

int main(int argc, char **argv)
{
	double d = 100.1;
	int i = d;  // doubleתΪint
	const char *str = "100ask.taobao.com";
	char *str2 = const_cast<char *>(str);
	int *p = reinterpret_cast<int *>(str2); // char *תΪint * 

	printf("i = %d, str = 0x%x, p = 0x%x\n", i, reinterpret_cast<unsigned int>(str), reinterpret_cast<unsigned int>(p));

	return 0;
}

convert5_Human.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};

void test_eating(Human& h)
{
	Englishman *pe;
	Chinese    *pc;
	
	h.eating();

	/* 想分辨这个"人"是英国人还是中国人? */
	if (pe = dynamic_cast<Englishman *>(&h))
		cout<<"This human is Englishman"<<endl;

	if (pc = dynamic_cast<Chinese *>(&h))
		cout<<"This human is Chinese"<<endl;
	
	
}




int main(int argc, char **argv)
{
	Human h;
	Englishman e;
	Chinese c;

	test_eating(h);
	test_eating(e);
	test_eating(c);


	return 0;
}

convert6_Human.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};

class Guangximan : public Chinese {
public:
	void eating(void) { cout<<"use chopsticks to eat, I come from guangxi"<<endl; }
};

void test_eating(Human& h)
{
	Englishman *pe;
	Chinese    *pc;
	Guangximan *pg;
	
	h.eating();

	/* 想分辨这个"人"是英国人还是中国人? */
	if (pe = dynamic_cast<Englishman *>(&h))
		cout<<"This human is Englishman"<<endl;

	if (pc = dynamic_cast<Chinese *>(&h))
		cout<<"This human is Chinese"<<endl;
	
	if (pg = dynamic_cast<Guangximan *>(&h))
		cout<<"This human is Guangximan"<<endl;
	
}




int main(int argc, char **argv)
{
	//Human h;
	//Englishman e;
	//Chinese c;
	Guangximan g;

	//test_eating(h);
	//test_eating(e);
	//test_eating(c);
	test_eating(g);


	return 0;
}

convert7_Human.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};

class Guangximan : public Chinese {
public:
	void eating(void) { cout<<"use chopsticks to eat, I come from guangxi"<<endl; }
};

void test_eating(Human& h)
{
#if 1
	//Englishman& pe = dynamic_cast<Englishman&>(h);
	Chinese&    pc = dynamic_cast<Chinese&>(h);
	Guangximan& pg = dynamic_cast<Guangximan&>(h);
#else
	Englishman& pe = reinterpret_cast<Englishman&>(h);
	Chinese&	pc = reinterpret_cast<Chinese&>(h);
	Guangximan& pg = reinterpret_cast<Guangximan&>(h);
#endif

	h.eating();
	
}




int main(int argc, char **argv)
{
	//Human h;
	//Englishman e;
	//Chinese c;
	Guangximan g;

	//test_eating(h);
	//test_eating(e);
	//test_eating(c);
	test_eating(g);


	return 0;
}

convert8_Human.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) { cout<<"use hand to eat"<<endl; }
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};

class Guangximan : public Chinese {
public:
	void eating(void) { cout<<"use chopsticks to eat, I come from guangxi"<<endl; }
};

void test_eating(Human& h)
{
#if 1
	//Englishman& pe = dynamic_cast<Englishman&>(h);
	Chinese&    pc = dynamic_cast<Chinese&>(h);
	Guangximan& pg = dynamic_cast<Guangximan&>(h);
#else
	Englishman& pe = reinterpret_cast<Englishman&>(h);
	Chinese&	pc = reinterpret_cast<Chinese&>(h);
	Guangximan& pg = reinterpret_cast<Guangximan&>(h);
#endif

	h.eating();
	
}




int main(int argc, char **argv)
{
	Human h;
	//Englishman e;
	//Chinese c;
	Guangximan g;

	Englishman *pe;

	pe = static_cast<Englishman *>(&h);

	//Englishman *pe2 = static_cast<Englishman *>(&g);

	Chinese *pc = static_cast<Chinese *>(&g);

	return 0;
}

15th_abstract

1th

Human.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) = 0;
	virtual void wearing(void) = 0;
	virtual void driving(void) = 0;
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	void wearing(void) {cout<<"wear english style"<<endl; }
	void driving(void) {cout<<"drive english car"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	void wearing(void) {cout<<"wear chinese style"<<endl; }
	void driving(void) {cout<<"drive chinese car"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};



int main(int argc, char **argv)
{
	//Human h;
	Englishman e;
	Chinese c;

	return 0;
}	

Human2.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	int a;
public:
	virtual void eating(void) = 0;
	virtual void wearing(void) = 0;
	virtual void driving(void) = 0;
	virtual ~Human() { cout<<"~Human()"<<endl; }
	virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
	void eating(void) { cout<<"use knife to eat"<<endl; }
	void wearing(void) {cout<<"wear english style"<<endl; }
	void driving(void) {cout<<"drive english car"<<endl; }
	virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
	virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
	void eating(void) { cout<<"use chopsticks to eat"<<endl; }
	void wearing(void) {cout<<"wear chinese style"<<endl; }
	//void driving(void) {cout<<"drive chinese car"<<endl; }
	virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
	virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};

class Guangximan : public Chinese {
	void driving(void) {cout<<"drive guangxi car"<<endl; }
};

int main(int argc, char **argv)
{
	//Human h;
	Englishman e;
	Guangximan g;

	return 0;
}

2th

Chinese.cpp

cpp 复制代码
#include "Chinese.h"

void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Chinese{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"

void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Englishman {
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Englishman();
};

#endif

main.cpp

cpp 复制代码
#include "Englishman.h"
#include "Chinese.h"

int main(int argc, char **argv)
{
	Englishman e;
	Chinese c;

	e.eating();
	c.eating();
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o Chinese.o Englishman.o
	g++ -o $@ $^

%.o : %.cpp
	g++ -c -o $@ $<

clean:
	rm -f *.o Human	

3th

3.1

Chinese.cpp

cpp 复制代码
#include "Chinese.h"

void Chinese::setName(char *name) 
{
	this->name = name;
}

char *Chinese::getName(void) 
{
	return this->name;
}


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Chinese{
private:
	char *name;
public:
	void setName(char *name);
	char *getName(void);
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"

void Englishman::setName(char *name) 
{
	this->name = name;
}

char *Englishman::getName(void) 
{
	return this->name;
}

void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Englishman {
private:
	char *name;
public:
	void setName(char *name);
	char *getName(void);
	void eating(void);
	void wearing(void);
	void driving(void);
	~Englishman();
};

#endif

main.cpp

cpp 复制代码
#include "Englishman.h"
#include "Chinese.h"

int main(int argc, char **argv)
{
	Englishman e;
	Chinese c;

	e.eating();
	c.eating();
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o Chinese.o Englishman.o
	g++ -o $@ $^

%.o : %.cpp
	g++ -c -o $@ $<

clean:
	rm -f *.o Human	

3.2

Chinese.cpp

cpp 复制代码
#include "Chinese.h"


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Chinese : public Human{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"


void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Englishman : public Human {
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Englishman();
};

#endif

Human.cpp

cpp 复制代码
#include "Human.h"

void Human::setName(char *name) 
{
	this->name = name;
}

char *Human::getName(void) 
{
	return this->name;
}

Human.h

cpp 复制代码
#ifndef _HUMAN_H
#define _HUMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	char *name;

public:
	void setName(char *name);
	char *getName(void);
	
};

#endif

main.cpp

cpp 复制代码
#include "Englishman.h"
#include "Chinese.h"

int main(int argc, char **argv)
{
	Englishman e;
	Chinese c;

	e.setName("Bill");
	c.setName("zhangsan");

	e.eating();
	c.eating();
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o Chinese.o Englishman.o Human.o
	g++ -o $@ $^

%.o : %.cpp
	g++ -c -o $@ $<

clean:
	rm -f *.o Human	

4th

Chinese.cpp

cpp 复制代码
#include "Chinese.h"


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Chinese : public Human{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"


void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Englishman : public Human {
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Englishman();
};

#endif

Human.cpp

cpp 复制代码
#include "Human.h"

void Human::setName(char *name) 
{
	this->name = name;
}

char *Human::getName(void) 
{
	return this->name;
}

Human.h

cpp 复制代码
#ifndef _HUMAN_H
#define _HUMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	char *name;

public:
	void setName(char *name);
	char *getName(void);
	virtual void eating(void){cout<<"use hand to eat"<<endl;}
	virtual void wearing(void){}
	virtual void driving(void){}
	
};

#endif

main.cpp

cpp 复制代码
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"

void test_eating(Human *h)
{
	h->eating();
}


int main(int argc, char **argv)
{
	Englishman e;
	Chinese c;

	Human* h[2] = {&e, &c};
	int i;
	for (i = 0; i < 2; i++)
		test_eating(h[i]);

	
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o Chinese.o Englishman.o Human.o
	g++ -o $@ $^

%.o : %.cpp
	g++ -c -o $@ $<

clean:
	rm -f *.o Human	

5th

Chinese.cpp

cpp 复制代码
#include "Chinese.h"


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Chinese : public Human{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"


void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Englishman : public Human {
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Englishman();
};

#endif

Human.cpp

cpp 复制代码
#include "Human.h"

void Human::setName(char *name) 
{
	this->name = name;
}

char *Human::getName(void) 
{
	return this->name;
}

Human.h

cpp 复制代码
#ifndef _HUMAN_H
#define _HUMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	char *name;

public:
	void setName(char *name);
	char *getName(void);
	virtual void eating(void) = 0;
	virtual void wearing(void) = 0;
	virtual void driving(void) = 0;
	
};

#endif

main.cpp

cpp 复制代码
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"

void test_eating(Human *h)
{
	h->eating();
}


int main(int argc, char **argv)
{
	Englishman e;
	Chinese c;

	Human* h[2] = {&e, &c};
	int i;
	for (i = 0; i < 2; i++)
		test_eating(h[i]);

	
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o Chinese.o Englishman.o Human.o
	g++ -o $@ $^

%.o : %.cpp
	g++ -c -o $@ $<

clean:
	rm -f *.o Human	

6th

Chinese.cpp

cpp 复制代码
#include "Chinese.h"


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Chinese : public Human{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"


void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Englishman : public Human {
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Englishman();
};

#endif

Human.cpp

cpp 复制代码
#include "Human.h"

void Human::setName(char *name) 
{
	this->name = name;
}

char *Human::getName(void) 
{
	return this->name;
}

Human.h

cpp 复制代码
#ifndef _HUMAN_H
#define _HUMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	char *name;

public:
	void setName(char *name);
	char *getName(void);
	virtual void eating(void) = 0;
	virtual void wearing(void) = 0;
	virtual void driving(void) = 0;
	
};

#endif

main.cpp

cpp 复制代码
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"

void test_eating(Human *h)
{
	h->eating();
}


int main(int argc, char **argv)
{
	Englishman e;
	Chinese c;

	Human* h[2] = {&e, &c};
	int i;
	for (i = 0; i < 2; i++)
		test_eating(h[i]);

	
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o libHuman.so
	g++ -o $@ $< -L./ -lHuman

%.o : %.cpp
	g++ -fPIC -c -o $@ $<

libHuman.so : Englishman.o Chinese.o Human.o
	g++ -shared -o $@ $^

clean:
	rm -f *.o Human	

7th

Chinese.cpp

cpp 复制代码
#include "Chinese.h"


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Chinese : public Human{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"


void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman::Englishman() {}
Englishman::Englishman(char *name, int age, char *address)
{
	setName(name);
	this->age = age;
	memset(this->address, 0, 100);
	strcpy(this->address, address);
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Englishman : public Human {
private:
	char address[100];
	int age;
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	Englishman();
	Englishman(char *name, int age, char *address);
	~Englishman();
};

#endif

Human.cpp

cpp 复制代码
#include "Human.h"

void Human::setName(char *name) 
{
	this->name = name;
}

char *Human::getName(void) 
{
	return this->name;
}

Human.h

cpp 复制代码
#ifndef _HUMAN_H
#define _HUMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	char *name;

public:
	void setName(char *name);
	char *getName(void);
	virtual void eating(void) = 0;
	virtual void wearing(void) = 0;
	virtual void driving(void) = 0;
	
};

#endif

main.cpp

cpp 复制代码
#include "Human.h"
#include "Englishman.h"
#include "Chinese.h"

void test_eating(Human *h)
{
	h->eating();
}


int main(int argc, char **argv)
{
	Englishman e("Bill", 10, "sfwqerfsdfas");
	Chinese c;

	Human* h[2] = {&e, &c};
	int i;
	for (i = 0; i < 2; i++)
		test_eating(h[i]);

	
	
	return 0;
}

Makefile

cpp 复制代码
Human: main.o libHuman.so
	g++ -o $@ $< -L./ -lHuman

%.o : %.cpp
	g++ -fPIC -c -o $@ $<

libHuman.so : Englishman.o Chinese.o Human.o
	g++ -shared -o $@ $^

clean:
	rm -f *.o Human	

8th

Chinese.cpp

cpp 复制代码
#include "Chinese.h"


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Human& CreateChinese(char *name, int age, char *address)
{
	return *(new Chinese());
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Chinese : public Human{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"


void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman::Englishman() {}
Englishman::Englishman(char *name, int age, char *address)
{
	setName(name);
	this->age = age;
	memset(this->address, 0, 100);
	strcpy(this->address, address);
}

Human& CreateEnglishman(char *name, int age, char *address)
{
	return *(new Englishman(name, age, address));
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Englishman : public Human {
private:
	char address[100];
	int age;
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	Englishman();
	Englishman(char *name, int age, char *address);
	~Englishman();
};

#endif

Human.cpp

cpp 复制代码
#include "Human.h"

void Human::setName(char *name) 
{
	this->name = name;
}

char *Human::getName(void) 
{
	return this->name;
}

Human.h

cpp 复制代码
#ifndef _HUMAN_H
#define _HUMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	char *name;

public:
	void setName(char *name);
	char *getName(void);
	virtual void eating(void) = 0;
	virtual void wearing(void) = 0;
	virtual void driving(void) = 0;
	
};

Human& CreateEnglishman(char *name, int age, char *address);
Human& CreateChinese(char *name, int age, char *address);

#endif

main.cpp

cpp 复制代码
#include "Human.h"
//#include "Englishman.h"
//#include "Chinese.h"

void test_eating(Human *h)
{
	h->eating();
}


int main(int argc, char **argv)
{
	Human& e = CreateEnglishman("Bill", 10, "sfwqerfsdfas");
	Human& c = CreateChinese("zhangsan", 11, "beijing");

	Human* h[2] = {&e, &c};
	int i;
	for (i = 0; i < 2; i++)
		test_eating(h[i]);

	
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o libHuman.so
	g++ -o $@ $< -L./ -lHuman

%.o : %.cpp
	g++ -fPIC -c -o $@ $<

libHuman.so : Englishman.o Chinese.o Human.o
	g++ -shared -o $@ $^

clean:
	rm -f *.o Human	

9th

Chinese.cpp

cpp 复制代码
#include "Chinese.h"


void Chinese::eating(void) 
{ 
	cout<<"use chopsticks to eat"<<endl; 
}

void Chinese::wearing(void) 
{
	cout<<"wear chinese style"<<endl; 
}

void Chinese::driving(void) 
{
	cout<<"drive chinese car"<<endl; 
}

Chinese::~Chinese() 
{ 
	cout<<"~Chinese()"<<endl; 
}

Human& CreateChinese(char *name, int age, char *address)
{
	return *(new Chinese());
}

Chinese.h

cpp 复制代码
#ifndef _CHINESE_H
#define _CHINESE_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Chinese : public Human{
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	virtual ~Chinese();
};

#endif

Englishman.cpp

cpp 复制代码
#include "Englishman.h"


void Englishman::eating(void) 
{ 
	cout<<"use knife to eat"<<endl; 
}

void Englishman::wearing(void) 
{
	cout<<"wear english style"<<endl; 
}

void Englishman::driving(void) 
{
	cout<<"drive english car"<<endl; 
}

Englishman::~Englishman() 
{ 
	cout<<"~Englishman()"<<endl; 
}

Englishman::Englishman() {}
Englishman::Englishman(char *name, int age, char *address)
{
	setName(name);
	this->age = age;
	memset(this->address, 0, 100);
	strcpy(this->address, address);
}

Human& CreateEnglishman(char *name, int age, char *address)
{
	return *(new Englishman(name, age, address));
}

Englishman.h

cpp 复制代码
#ifndef _ENGLISHMAN_H
#define _ENGLISHMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

#include "Human.h"

using namespace std;

class Englishman : public Human {
private:
	char address[100];
	int age;
public:
	void eating(void);
	void wearing(void);
	void driving(void);
	Englishman();
	Englishman(char *name, int age, char *address);
	virtual ~Englishman();
};

#endif

Human.cpp

cpp 复制代码
#include "Human.h"

void Human::setName(char *name) 
{
	this->name = name;
}

char *Human::getName(void) 
{
	return this->name;
}

Human.h

cpp 复制代码
#ifndef _HUMAN_H
#define _HUMAN_H

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
	char *name;

public:
	void setName(char *name);
	char *getName(void);
	virtual void eating(void) = 0;
	virtual void wearing(void) = 0;
	virtual void driving(void) = 0;
	virtual ~Human() {cout<<"~Human"<<endl;}
	
};

Human& CreateEnglishman(char *name, int age, char *address);
Human& CreateChinese(char *name, int age, char *address);

#endif

main.cpp

cpp 复制代码
#include "Human.h"
//#include "Englishman.h"
//#include "Chinese.h"

void test_eating(Human *h)
{
	h->eating();
}


int main(int argc, char **argv)
{
	Human& e = CreateEnglishman("Bill", 10, "sfwqerfsdfas");
	Human& c = CreateChinese("zhangsan", 11, "beijing");

	Human* h[2] = {&e, &c};
	int i;
	for (i = 0; i < 2; i++)
		test_eating(h[i]);

	delete &e;
	delete &c;
	
	
	return 0;
}

Makefile

bash 复制代码
Human: main.o libHuman.so
	g++ -o $@ $< -L./ -lHuman

%.o : %.cpp
	g++ -fPIC -c -o $@ $<

libHuman.so : Englishman.o Chinese.o Human.o
	g++ -shared -o $@ $^

clean:
	rm -f *.o Human	

16th_template

max.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
T& mymax(T& a, T& b)
{
	cout<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}


int main(int argc, char **argv)
{
	int ia = 1, ib = 2;
	float fa = 1, fb = 2;
	double da = 1, db = 2;
	
	mymax(ia, ib);
	mymax(fa, fb);
	mymax(da, db);

	return 0;
}

max2.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
T& mymax(T& a, T& b)
{
	cout<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

void add(int a, int b)
{
	cout<<"add(int a, int b) ="<<(a+b)<<endl;
}

int main(int argc, char **argv)
{
	const int ia = 1;
	const int ib = 2;
	
	mymax(ia, ib);

	int a = 1;
	double b = 2.1;
	add(a, b);

	mymax(a, b);

	return 0;
}

max3.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}


int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;
	

	mymax(ia, ib);

	return 0;
}

max4.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}


template<typename T>
const T* mymax2(const T* a, const T* b)
{
	cout<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

int main(int argc, char **argv)
{
	char a[]="ab";
	char b[]="cd";
	

	mymax(a, b);  /* T=char[3] */
	mymax2(a, b);

	char a2[]="abc";
	char b2[]="cd";

	//mymax(a2, b2);  /* mymax(char[4], char[3]), 无法推导出T: mymax(char& [4], char& [3]), 因为两个参数类型不一样 */
	mymax2(a2, b2);   /* mymax2(char[4], char[3]), 推导: mymax2(const char *, const char *); */

	return 0;
}

max5.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}


template<typename T>
const T* mymax2(const T* a, const T* b)
{
	cout<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
void test_func(T f)
{
	cout<<__PRETTY_FUNCTION__<<endl;
}

int f1(int a, int b)
{
	return 0;
}

int main(int argc, char **argv)
{
	char a[]="ab";
	char b[]="cd";
	

	mymax(a, b);  /* T=char[3] */
	mymax2(a, b);

	char a2[]="abc";
	char b2[]="cd";

	//mymax(a2, b2);  /* mymax(char[4], char[3]), 无法推导出T: mymax(char& [4], char& [3]), 因为两个参数类型不一样 */
	mymax2(a2, b2);   /* mymax2(char[4], char[3]), 推导: mymax2(const char *, const char *); */

	test_func(f1);
	test_func(&f1);

	return 0;
}

max6.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
const T& mymax(T& a, T& b)
{
	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}


const int& mymax(int& a, int& b)
{
	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;

	cout<<mymax(ia, ib)<<endl;

	return 0;
}

max7.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
const T& mymax(T& a, T& b)
{
	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

#if 0
const int& mymax(int& a, int& b)
{
	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}
#endif

template<typename T>
const T mymax(T a, T b)
{
	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}


int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;

	cout<<mymax(ia, ib)<<endl;

	return 0;
}

max8.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
const T& mymax(T& a, T& b)
{
	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}


const int& mymax(int& a, int& b)
{
	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;

	cout<<mymax(ia, ib)<<endl;

	int *p1=&ia;
	int *p2=&ib;

	cout<<mymax(p1, p2)<<endl;

	return 0;
}

max9.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
const T& mymax(T& a, T& b)
{
	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
const T mymax(T * a, T* b)
{
	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
	return (*a < *b)? *b : *a;
}


const int& mymax(int& a, int& b)
{
	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;

	cout<<mymax(ia, ib)<<endl;

	int *p1=&ia;
	int *p2=&ib;

	cout<<mymax(p1, p2)<<endl;

	return 0;
}

max10.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
const T& mymax(T& a, T& b)
{
	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

template<typename T>
const T mymax(const T * a, const T* b)
{
	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
	return (*a < *b)? *b : *a;
}


const int& mymax(int& a, int& b)
{
	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;

	cout<<mymax(ia, ib)<<endl;

	int *p1=&ia;
	int *p2=&ib;

	cout<<mymax(p1, p2)<<endl;

	return 0;
}

max11.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

#if 0
template<typename T>
const T& mymax(T& a, T& b)
{
	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}
#endif

template<typename T>
const T mymax(const T * a, const T* b)
{
	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
	return (*a < *b)? *b : *a;
}


const int& mymax(int& a, int& b)
{
	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;

	cout<<mymax(ia, ib)<<endl;

	int *p1=&ia;
	int *p2=&ib;

	cout<<mymax(p1, p2)<<endl;

	return 0;
}

max12.cpp

cpp 复制代码
#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

/*
int& max(int& a, int& b)
{
	return (a < b)? b : a;
}

double& max(double& a, double& b)
{
	return (a < b)? b : a;
}

float& max(float& a, float& b)
{
	return (a < b)? b : a;
}
*/

template<typename T>
const T& mymax(const T& a, const T& b)
{
	cout<<"1: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}

#if 0
template<typename T>
const T& mymax(T& a, T& b)
{
	cout<<"2: "<<__PRETTY_FUNCTION__<<endl;
	return (a < b)? b : a;
}
#endif

template<typename T>
const T mymax(T * a, T* b)
{
	cout<<"4: "<<__PRETTY_FUNCTION__<<endl;
	return (*a < *b)? *b : *a;
}


char* mymax(char* a, char* b)
{
	cout<<"3: "<<__PRETTY_FUNCTION__<<endl;
	return strcmp(a, b) < 0? b : a;
}

int main(int argc, char **argv)
{
	int ia = 1;
	int ib = 2;

	cout<<mymax(ia, ib)<<endl;

	int *p1=&ia;
	int *p2=&ib;

	cout<<mymax(p1, p2)<<endl;

	char *str1="hello";
	char *str2="Hellod";

	cout<<mymax(str1, str2)<<endl;
	

	return 0;
}
相关推荐
ChoSeitaku14 分钟前
16.C++入门:list|手撕list|反向迭代器|与vector对比
c++·windows·list
Qhumaing20 分钟前
C++学习:【PTA】数据结构 7-1 实验6-1(图-邻接矩阵)
c++·学习·算法
No0d1es20 分钟前
2025年12月 GESP CCF编程能力等级认证C++一级真题
开发语言·c++·青少年编程·gesp·ccf
2301_7737303127 分钟前
系统编程—在线商城信息查询系统
c++·html
郝学胜-神的一滴28 分钟前
深入理解Linux中的Try锁机制
linux·服务器·开发语言·c++·程序人生
散峰而望1 小时前
【算法竞赛】顺序表和vector
c语言·开发语言·数据结构·c++·人工智能·算法·github
cpp_25011 小时前
B3927 [GESP202312 四级] 小杨的字典
数据结构·c++·算法·题解·洛谷
Cx330❀1 小时前
《C++ 递归、搜索与回溯》第2-3题:合并两个有序链表,反转链表
开发语言·数据结构·c++·算法·链表·面试
小六子成长记2 小时前
【C++】:多态的实现
开发语言·c++
chen_2272 小时前
动态桌面方案
c++·qt·ffmpeg·kanzi