初识继承
#include<string>
using namespace std;
class Person
{
public:
Person();
~Person();
void eat();
string m_strName;
int m_iAge;
};
#include "Person.h"
class Worker :public Person//Worker公有继承Person类
{
public:
Worker();
~Worker();
void work();
int m_iSalary;
};
#include"Person.h"
#include<iostream>
using namespace std;
Person::Person()
{
cout << "Person()" << endl;
}
Person::~Person()
{
cout << "~erson()" << endl;
}
void Person::eat()
{
cout << "eat" << endl;
}
/Worker.cpp
#include"Worker.h"
#include<iostream>
using namespace std;
Worker::Worker()
{
cout << "Worker()" << endl;
}
Worker::~Worker()
{
cout << "~Worker()" << endl;
}
void Worker::work()
{
cout << "work()" << endl;
}
#include<iostream>
#include<stdlib.h>
#include"Worker.h"
using namespace std;
int main()
{
Worker *p = new Worker();
p->m_strName = "Keiven";
p->m_iAge = 20;
p->eat();
p->m_iSalary = 5000;
p->work();
delete p;
p = NULL;
system("pause");
return 0;
}
Person()
Worker()
eat
work()
~Worker()
~Person()