类的公有继承

来源:互联网 发布:什么是大数据应用平台 编辑:程序博客网 时间:2024/04/29 02:36
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
class Person
{
public:
 Person(string Strname = "pertter", int Age = 20) :m_strName(Strname), m_Age(Age)
 {
  cout << "Person 构造函数" << endl;
 };
 ~Person()
 {
  cout << "Person 析构函数" << endl;
 }
 void eat()
 {
  cout << "eat" << endl;
 }
 void walk()
 {
  cout << m_strName << endl;
  cout << m_Age << endl;
 }
 void read()
 {
  cout << "read" << endl;
 }
protected:
 string m_strName;
 int m_Age;
private:
};
class Solidier :public Person
{
public:
 Solidier(string strCode = "001"):m_strCode(strCode)
 {
  cout << "Solidier 构造函数" << endl;
 };
 ~Solidier()
 {
  cout << "Solider 析构函数" << endl;
 }
 void set()
 {
  m_strCode = 002;
 }
 void attack()
 {
  cout << "fire!!!" << endl;
  cout << m_strCode << endl;
 }
protected:
 string m_strCode;
private:
};

int main()
{
 Solidier *soldier = new Solidier;
 Person * person = new Person;
 
 soldier->eat();
 soldier->walk();
 soldier->read();
 soldier->set();
 soldier->attack();

 system ("pause");
 return 0;
}