C++基础三

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daividtu/article/details/78391067

友元的两种表示形式

#include <string>
#include <iostream>

using namespace std;
//友元的两种表现形式 
//1.友元函数
//2. 友元类

//作业
//实现 类中的友元函数 friend void Boy::introduce();

class Girl
{
public:
	//友元函数不能使用this
	friend void modify(Girl *girl, int age);
	friend class Boy;
	
	//friend void Boy::introduce();
	
	void tell();

private:
	int age = 16;
	string name = "大笑";
};

//girl class的函数实现
void Girl::tell() {

	cout << "age == " << age << endl;
}

class Boy
{
public:
	Boy(Girl girl) {
		this->girl = girl;

	}

	~Boy() {
	
	}

	void changGirl() {
		girl.age = 28;
		girl.name = "vava";
	}

	//已经声明为友元函数 可以访问和修改私有属性s
	void introduce() {
		cout << "My Girl friend age : "<<girl.age << "name = "<<girl.name <<endl;
	}

public:
	Girl girl;
};

//外部实现
void modify(Girl *girl, int age) {

	girl->age = age;
}

/*void main() {

	/*Girl *girl = new Girl();
	
	modify(girl, 31);

	girl->tell();

	delete girl;

	Girl girl;
	Boy boy = Boy(girl);

	boy.introduce();

	boy.changGirl();
	boy.girl.tell();
	boy.introduce();
	system("pause");
}*/

模板方法,模板类,类型转换

#include <string>
#include <iostream>
#include <list>
using namespace std;

/*void myswap(int& a, int& b) {
	int tmp = 0;
	tmp = a;
	a = b;
	b = tmp;
}

void myswap(char& a, char& b) {
	char tmp = 0;
	tmp = a;
	a = b;
	b = tmp;
}*/

template <typename T>
void myswap(T& a, T& b) {
	T tmp = 0;
	tmp = a;
	a = b;
	b = tmp;
	cout << "myswap T" << endl;
}

template<class  T>
class  A
{
public:
	A(T a) {
		this->a = a;
	}
	 ~A() {
	 }

protected:
	T a;
};

template <class  T>
class C : public A<T>
{
public:
	
	C(T c, T a) : A<T>(a) {
		this->c = c;
	}

	void tell() {
		cout << "a : " << a << "c " << c << endl;
	}
private:
	T c;
};


/*void main() {

	int a = 999;
	int b = 666;

	myswap(a,b);
	cout << "a = "<< a << "b = " <<b << endl;

	char c = '9';
	char v = '6';

	myswap(c, v);

	cout << "c = " << c << "v = " << v << endl;

	//list<string>


	system("pause");
}*/

//c++类型转换 通用 常用
// static_cast 普通值类型转换
//const_cast 去常量
//dynamic_cast 基类和派生类之间的转换


//reinterpret_cast 不通用 不常用  函数指针的转换 (一般在Void * 之间转)

void func(const char c[]) {
	//c[1] = 'a';
	//去掉const 关键字
	char* c_p = const_cast<char *>(c);
	c_p[1] = 'X';
}

/*void main() {

	int i = 8;
	double d = 9.5;

	//i = (int)d;
	i = static_cast<int> (d);

	char c[] = "hello";
	cout << "c = " << c << endl;
	func(c);
	cout << "c = " << c << endl;

	system("pause");
}*/

class Person {

public:
	virtual void print() {
		cout << "人" << endl;
	}
};

class Man : public Person
{
public:
	void print() {
		cout << "男人" << endl;
	}

	void chasing() {
		cout << "泡妞" << endl;
	}
};

class Woman :public Person
{
public:
	
	void print() {
		cout << "女人" << endl;
	}

	void createBaby() {
		cout << "造小孩" << endl;
	}
};

void funx(Person* obj) {

	//Man *m = (Man *)obj;
	//m->chasing();

	Man *m = dynamic_cast<Man*>(obj);
	if (m != nullptr) {
		m->chasing();
	}
	else {
		cout << "还需要去趟泰国" << endl;
	}


}

/*void main() {

	//Woman *wm = new Woman();
	Man *m = new Man();
	funx(m);
	//funx(wm);

	system("pause");
}*/

/*void main() {
	
	try
	{
		int age = 300;
		if (age > 200) {
			throw "xxx";
		}
	}
	catch (int a)
	{
		cout << "int 异常"<<endl;
	}
	catch (char * b) {
		cout << "char b" <<b << endl;
	}
	system("pause");
}*/
//标准异常 (类似于java中javaNullPointerException)

class NullPointerException : public exception {

public:

	NullPointerException(char * msg) :exception(msg) {
		
	}
};

void main() {

	try
	{
		int age = 300;
		if (age > 200) {
			throw NullPointerException("pgone");
		}
	}
	catch (int a)
	{
		cout << "int 异常" << endl;
	}
	catch (char * b) {
		cout << "char b" << b << endl;
	}
	catch (NullPointerException msg) {
		
	}
	system("pause");
}




猜你喜欢

转载自blog.csdn.net/daividtu/article/details/78391067