总结:1.不管哪种继承,private下的属性和方法都不能被继承。2. protected继承将基类的protected和public都继承为protected 3. private继承将基类的protected和public都继承为private
2018-04-11
int main(void)
{
Soldier soldier;
// 向士兵属性赋值"tomato"
soldier.m_strName = "tomato";
soldier.Person::m_strName= "Jim";
cout << soldier.m_strName << endl;
cout << soldier.Person::m_strName << endl;
// 调用士兵对象方法
soldier.attack();
soldier.Person::attack();
{
Soldier soldier;
// 向士兵属性赋值"tomato"
soldier.m_strName = "tomato";
soldier.Person::m_strName= "Jim";
cout << soldier.m_strName << endl;
cout << soldier.Person::m_strName << endl;
// 调用士兵对象方法
soldier.attack();
soldier.Person::attack();
Farmer.cpp 代码
#include <iostream>
#include <string>
#include "Farmer.h"
using namespace std;
Farmer::Farmer(string name){
m_strName = name;
cout << "Farmer()"<<endl;}
Farmer::~Farmer(){
cout << "~Farmer()"<<endl;}
void Farmer::sow(){
cout << m_strName <<endl;
cout << "Farmer() --sow()"<<endl;}
#include <iostream>
#include <string>
#include "Farmer.h"
using namespace std;
Farmer::Farmer(string name){
m_strName = name;
cout << "Farmer()"<<endl;}
Farmer::~Farmer(){
cout << "~Farmer()"<<endl;}
void Farmer::sow(){
cout << m_strName <<endl;
cout << "Farmer() --sow()"<<endl;}
2018-03-18