#include <iostream>
#include <string>
class Father {
public:
Father()
: m_data(0)
{
}
virtual ~Father() { }
virtual void print()
{
std::cout << "Father print:" << m_data << std::endl;
}
void output()
{
std::cout << "Father output\t";
print();
}
int m_data;
};
class Son : public Father {
public:
Son()
: Father()
{
}
virtual ~Son() { }
virtual void print()
{
m_data++;
std::cout << "Son print:" << m_data << std::endl;
}
void output()
{
std::cout << "Son output\t";
Father::print();
}
};
int main(int argc, char *argv[])
{
Son s;
Father& b = s;
b.print();
b.output();
Father* c = &s;
c->print();
c->output();
s.print();
s.output();
std::string str(20, '*');
std::cout << str << std::endl;
auto s2 = new Son();
Father& b2 = *s2;
b2.print();
b2.output();
Father* c2 = s2;
c2->print();
c2->output();
s2->print();
s2->output();
}
输出:
Son print:1
Father output Son print:2
Son print:3
Father output Son print:4
Son print:5
Son output Father print:5
********************
Son print:1
Father output Son print:2
Son print:3
Father output Son print:4
Son print:5
Son output Father print:5