#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;
}