C++之类部分探究

#include <iostream>
#include <string>

using namespace std;

class  vclass
{
public:

	string str = "pclass";

	vclass() { cout << "create vlass" << endl; }
	virtual ~vclass() { cout << "vclass destoryed" << endl; }
	void print_pub() { cout << "public" << endl; }
	virtual void print_v() = 0 { cout << "parent function" << endl; } 

private:
	void print_pri() { cout << "private" << endl; }

protected:
	void print_pro() { cout << "protected" << endl; }
	string tmp = "";
};

class child : public  vclass
{
public:
	child() { cout << "create child" << endl; }
	virtual ~child() { cout << "child destoryed" << endl; }
	virtual void print_v() { cout << "child function" << endl; }
	void use_pro() { this->print_pro(); }
	//void use_pri() { this->print_pri(); } invalid
private:

};



int main()
{
	child ch;
	ch.print_v();
	vclass  & vc = ch;
	vc.print_v();
	vc.print_pub();
	ch.use_pro();

	return 0;
}

另外虚析构函数需要特殊注意下

#include <iostream>
#include <string>

using namespace std;

class  vclass
{
public:

	string str = "pclass";

	vclass() { cout << "create vlass" << endl; }
	virtual ~vclass() { cout << "vclass destoryed" << endl; }
	void print_pub() { cout << "public" << endl; }
	virtual void print_v()  { cout << "parent function" << endl; } 

private:
	void print_pri() { cout << "private" << endl; }

protected:
	void print_pro() { cout << "protected" << endl; }
	string tmp = "";
};

class child : public  vclass
{
public:
	child() { cout << "create child" << endl; }
	virtual ~child() { cout << "child destoryed" << endl; }
	virtual void print_v() { cout << "child function" << endl; }
	void use_pro() { this->print_pro(); }
	//void use_pri() { this->print_pri(); } invalid
private:

};



int main()
{
	vclass *vcp[3];
	vcp[0] = new child;
	vcp[1] = new vclass;
	vcp[2] = new vclass;
	delete vcp[0];
	delete vcp[1];
	delete vcp[2];
	return 0;
}

基类友元函数的调用

#include <iostream>
#include <string>

using namespace std;

class  vclass
{
public:

	string str = "str";

	vclass() { cout << "create vlass" << endl; }
	virtual ~vclass() { cout << "vclass destoryed" << endl; }
	void print_pub() { cout << "public" << endl; }
	virtual void print_v()  { cout << "parent function" << endl; } 
	friend ostream & operator << (ostream & cin, vclass &vc) { cout << vc.str << endl; return cin; }
private:
	void print_pri() { cout << "private" << endl; }

protected:
	void print_pro() { cout << "protected" << endl; }
	string tmp = "";
};

class child : public  vclass
{
public:
	child() { cout << "create child" << endl;}
	virtual ~child() { cout << "child destoryed" << endl; }
	virtual void print_v() { cout << "child function" << endl; }
	void use_pro() { this->print_pro(); }
	friend ostream & operator << (ostream & cin, child &ch)
	{
		cout << ch.new_str << endl;
		return cin;
	}
	//void use_pri() { this->print_pri(); } invalid
private:
	string new_str = "new_str";
};



int main()
{
	vclass *vcp[3];
	vcp[0] = new child;
	vcp[1] = new vclass;
	vcp[2] = new vclass;

	child ch;
	cout << ch << endl;
	cout << ((vclass &)ch) << endl;
	cout << *vcp[0] << endl;
	cout << *vcp[1] << endl;

	delete vcp[0];
	delete vcp[1];
	delete vcp[2];
	return 0;
}

一些小的细节

#include <iostream>
#include <cstdio>
#include <string>

using namespace std;
template<typename T , typename M>
class TEst
{
public:
	TEst();
	~TEst();

	/*explicit*/ TEst(M age) :age(age) {  }

	T name;
	M age;

private:

};
template<typename T, typename M>
TEst<T,M>::TEst()
{
}
template<typename T, typename M>
TEst<T,M>::~TEst()
{
}

class MyClass : public TEst<string  , int>
{
public:
	explicit MyClass(int age);
	MyClass() {}
	~MyClass();

private:

};

MyClass::MyClass(int age)
{
	this->age = age;
}

MyClass::~MyClass()
{
}

int main()
{
	using P = int *;//模板别名
	P pointer = NULL;

	using T = TEst<string, int>;

	T t = 123;

	using M = MyClass;

	//MyClass m = 123; invalid
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值