Whatafuck先生(AB1B2)

#include <iostream>
using namespace std;
class B1{
public:
	void output();
};

class B2{
public:
	void output();
};

void B1::output()
{
	cout << "call the class B1" << endl;
}

void B2::output()
{
	cout << "call the class B2" << endl;
}

class A : public B1, public B2
{
public:
	void show();
};

void A::show()
{
	cout << "call the class A" << endl;
}

void main()
{
	A a;
	//a.output();  //該語句編譯時會報錯
	a.show();
}

 

#include<iostream>
using namespace std;
class animal
{
public:
	void eat()
	{
		cout << "animal eat" << endl;
	}
	void sleep()
	{
		cout << "animal sleep" << endl;
	}
	void breath()
	{
		cout << "animal breath" << endl;
	}
};

class fish :public animal
{
public:
	void breath()
	{
		cout << "fish bubble" << endl;
	}
};
void fn(animal *pAn)
{
	pAn->breath();
}

void main()
{
	animal *pAn;
	fish fh;
	pAn = &fh;
	fn(pAn);

	system("pause");
}

 

#include<iostream>
using namespace std;
class animal
{
public:
	void eat()
	{
		cout << "animal eat" << endl;
	}
	void sleep()
	{
		cout << "animal sleep" << endl;
	}
	virtual void breath()
	{
		cout << "animal breath" << endl;
	}
};

class fish :public animal
{
public:
	void breath()
	{
		cout << "fish bubble" << endl;
	}
};
void fn(animal *pAn)
{
	pAn->breath();
}

void main()
{
	animal *pAn;
	fish fh;
	pAn = &fh;
	fn(pAn);

	system("pause");
}

 

#include<iostream>
using namespace std;

void change(int& a, int& b);
void main()
{
	int x = 5;
	int y = 3;
	cout << "original x=" << x << endl;
	cout << "original y=" << y << endl;
	change(x, y);//此處如果用指針傳遞,則調用change(&x,&y),這樣很容易讓人迷惑,不知道交換的是x和y的值,還是x和y的地址?此處爲引用,可讀性就比指針要好

	cout << "changed x=" << x << endl;
	cout << "changed y=" << y << endl;

	system("pause");
}
//在change()函數的實現中,,採用了一個小算法,完成了a和b值的交換,
void change(int& a, int& b)
{
	a = a + b;
	b = a - b;
	a = a - b;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章