赞
踩
2-2为什么要继承
//Person.h文件
#include<string>
using namespace std;
class Person
{
public:
Person(); //构造函数
~Person();
void eat();
string m_strName;
int m_iAge;
};
//Work.h文件
#include"Person2_2.h"
class Worker:public Person //公有继承Person
{
public:
Worker();
~Worker();
void work();
int m_iSalary;
};
//Person.cpp文件
#include<iostream>
#include"Person2_2.h"
using namespace std;
Person::Person()
{
cout<<"Person()"<<endl;
}
Person::~Person()
{
cout<<"~Person()"<<endl;
}
void Person::eat()
{
cout<<"eat()"<<endl;
}
//Worker.cpp文件
#include<iostream>
#include"Worker2_2.h"
using namespace std;
Worker::Worker()
{
cout<<"Worker()"<<endl;
}
Worker::~Worker()
{
cout<<"~Worker()"<<endl;
}
void Worker::work()
{
cout<<"work()"<<endl;
}
//main文件
//C++远征之继承篇2-2为什么要继承
#include<iostream>
#include<stdlib.h>
#include"Worker2_2.h"
using namespace std;
/***************************/
/*
继承
要求:
1.定义Person类,要求含有m_strName和m_iAge两个数据成员
及构造函数和析构函数、eat函数
2.定义Worker类,要求共有继承Person类,含有数据成员m_iSalary、构造函数
析构函数、work函数
/***************************/
int main(void)
{
/* Worker *p = new Worker(); //结果:先执行父类的构造函数再执行派生类的构造函数
delete p; //释放内存时先释放派生类的内存再释放父类的内存
p = NULL; */
Worker *p = new Worker();
p->m_strName = "CJ";
cout<<"m_strName = "<<p->m_strName<<endl;
p->m_iAge = 10;
cout<<"m_iAge = "<<p->m_iAge<<endl;
p->eat();
p->m_iSalary = 5000;
cout<<"m_iSalary = "<<p->m_iSalary<<endl;
p->work();
delete p;
p = NULL;
system("pause");
return 0;
}
3-2公有继承
代码演示:
//Person.h文件
#include<string>
using namespace std;
class Person
{
public:
Person(); //构造函数
~Person();
void eat();
//private: //若申明为private则子类不可访问该数据成员
protected:
string m_strName;
int m_iAge;
};
//Worker.h文件
#include"Person3_2.h"
class Worker:public Person //公有继承Person
{
public:
Worker();
~Worker();
void work();
int m_iSalary;
};
//Person.cpp文件
#include<iostream>
#include"Person3_2.h"
using namespace std;
Person::Person()
{
cout<<"Person()"<<endl;
}
Person::~Person()
{
cout<<"~Person()"<<endl;
}
void Person::eat()
{
m_strName = "CJ";
m_iAge = 20;
cout<<"eat()"<<endl;
}
//Worker.cpp文件
#include<iostream>
#include"Worker3_2.h"
using namespace std;
Worker::Worker()
{
cout<<"Worker()"<<endl;
}
Worker::~Worker()
{
cout<<"~Worker()"<<endl;
}
void Worker::work()
{
m_strName = "Merry";
m_iAge = 20;
cout<<"work()"<<endl;
}
//main文件
//C++远征之继承篇3-2公有继承代码演示
#include<iostream>
#include<stdlib.h>
#include"Worker3_2.h"
//#include"Person3_2.h"
using namespace std;
/***************************/
/*
继承
要求:
1.定义Person类,要求含有m_strName和m_iAge两个数据成员
及构造函数和析构函数、eat函数
2.定义Worker类,要求共有继承Person类,含有数据成员m_iSalary、构造函数
析构函数、work函数
/***************************/
int main(void)
{
Worker worker;
// worker.m_strName = "CJ";
// cout<<"m_strName = "<<worker.m_strName<<endl;
// worker.m_iAge = 10;
// cout<<"m_iAge = "<<worker.m_iAge<<endl;
// worker.eat();
// worker.m_iSalary = 5000;
// cout<<"m_iSalary = "<<worker.m_iSalary<<endl;
worker.work();
/* Person person;
// person.m_strName = "Merry"; //错误
// person.eat(); */
system("pause");
return 0;
}
3-4保护继承和私有继承
//Perso3_4.h文件
//人类
#include<string>
using namespace std;
class Person
{
public:
Person();
void play();
protected:
string m_strName;
};
//Soldier3_4.h文件
//战士类
#include"Person3_4.h"
//class Soldier:public Person
//class Soldier:protected Person
class Soldier::private Person
{
public:
Soldier();
void work();
protected:
int m_iAge;
private:
};
//Infantry3_4.h文件
//步兵类
#include"Soldier3_4.h"
class Infantry:public Soldier
{
public:
void attack();
};
//Person3_4.cpp文件
#include<iostream>
#include"Person3_4.h"
using namespace std;
Person::Person()
{
m_strName = "Merry";
}
void Person::play()
{
cout<<"Person--play()"<<endl;
cout<<m_strName<<endl;
}
//Soldier3_4.cpp文件
#include"Soldier3_4.h"
#include<iostream>
using namespace std;
Soldier::Soldier()
{
}
void Soldier::work()
{
m_strName = "CJ";
m_iAge = 20;
cout<<m_strName<<endl;
cout<<m_iAge<<endl;
cout<<"Soldier--work()"<<endl;
}
//Infantry3_4.cpp文件
#include"Infantry3_4.h"
#include<iostream>
using namespace std;
void Infantry::attack()
{
m_strName = "Jim";
cout<<m_strName<<endl;
cout<<"Infantry--attack()"<<endl;
}
//main文件
//c++远征之继承篇3-4保护继承和私有继承
#include<iostream>
#include<stdlib.h>
//#include"Soldier3_4.h"
#include"Infantry3_4.h"
/**************************************/
/* 保护继承和私有继承
要求:1.Person类,数据成员:m_strName,成员函数:构造函数、play()
2.Soldier类,数据成员:m_iAge,成员函数:构造函数,work()
3.Infantry步兵类,成员函数:attack()
/**************************************/
int main(void)
{
/* Soldier soldier;
soldier.work();
soldier.play();
*/
Infantry soldier;
soldier.attack();
system("pause");
return 0;
}
4-2隐藏
//Soldier4_2.h
//战士类
#include"Person4_2.h"
class Soldier:public Person
//class Soldier:protected Person
//class Soldier::private Person
{
public:
Soldier();
void work();
void play();
protected:
string m_strName;
private:
};
//Person4_2.h
//人类
#include<string>
using namespace std;
class Person
{
public:
Person();
void play();
protected:
string m_strName;
};
//Soldier4_2.cpp
#include"Soldier4_2.h"
#include<iostream>
using namespace std;
Soldier::Soldier()
{
}
void Soldier::work()
{
m_strName = "CJ";
Person::m_strName = "who_knows";
// cout<<m_strName<<endl;
cout<<"Soldier--work()"<<endl;
}
void Soldier::play()
{
cout<<m_strName<<endl;
cout<<Person::m_strName<<endl;
cout<<"Soldier play()"<<endl;
}
//Person4_2.cpp
#include<iostream>
#include"Person4_2.h"
using namespace std;
Person::Person()
{
m_strName = "Merry";
}
void Person::play()
{
cout<<"Person--play()"<<endl;
cout<<m_strName<<endl;
}
//main文件
//c++远征之继承篇4-2隐藏
#include<iostream>
#include<stdlib.h>
#include"Soldier4_2.h"
/**************************************/
/* 隐藏
要求:1.Person类,数据成员:m_strName,成员函数:构造函数、play()
2.Soldier类,数据成员:无,成员函数:构造函数,play()、work()
/**************************************/
int main(void)
{
Soldier soldier;
soldier.work();
soldier.play();
soldier.Person::play();
system("pause");
return 0;
}
4-5isA
//Person4_5.h
//人类
#include<string>
using namespace std;
class Person
{
public:
Person(string name = "Jim");
virtual ~Person(); //有继承关系时,用虚析构函数
void play();
protected:
string m_strName;
};
//Soldier4_5.h
//战士类
#include"Person4_5.h"
class Soldier:public Person
//class Soldier:protected Person
//class Soldier::private Person
{
public:
Soldier(string name = "James",int age = 20);
virtual ~Soldier();
void work();
protected:
string m_iAge;
};
//Person4_5.cpp
#include<iostream>
#include"Person4_5.h"
using namespace std;
Person::Person(string name)
{
m_strName = name;
cout<<"Person()"<<endl;
}
Person::~Person()
{
cout<<"~Person()"<<endl;
}
void Person::play()
{
cout<<"Person--play()"<<endl;
cout<<m_strName<<endl;
}
//Soldier4_5.cpp
#include"Soldier4_5.h"
#include<iostream>
using namespace std;
Soldier::Soldier(string name,int age)
{
m_strName = name;
m_iAge = age;
cout<<"Soldier()"<<endl;
}
Soldier::~Soldier()
{
cout<<"~Soldier()"<<endl;
}
void Soldier::work()
{
cout<<m_strName<<endl;
cout<<m_iAge<<endl;
cout<<"Soldier--work()"<<endl;
}
//main文件
//c++远征之继承篇4-5~4-6isA
#include<iostream>
#include<stdlib.h>
#include"Soldier4_5.h"
/**************************************/
/* 公有继承:Is a
要求:1.Person类,数据成员:m_strName,成员函数:构造函数、play()
2.Soldier类,数据成员:m_iAge,成员函数:构造函数、构造函数,work()
3.定义函数test1(Person p) test2(Person &p) test3(Person *p)
/**************************************/
void test1(Person p)
{
p.play();
}
void test2(Person &p)
{
p.play();
}
void test3(Person *p)
{
p->play();
}
int main(void)
{
/* Person p = soldier;
Person *p = new Soldier;
p->play();
delete p;
p = NULL; */
Person p;
Soldier s;
test3(&p);
test3(&s);
system("pause");
return 0;
}
5-2多重继承
//Person5_2.h
//人类
#include<string>
using namespace std;
class Person
{
public:
Person(string name = "Jim");
virtual ~Person();
void play();
protected:
string m_strName;
};
//Soldier5_2.h
//战士类
#include<string>
#include"Person5_2.h"
class Soldier:public Person
{
public:
Soldier(string name = "James",int age = 20);
virtual ~Soldier();
void work();
protected:
int m_iAge;
};
//Infantry5_2.h
//步兵类
#include<string>
#include"Soldier5_2.h"
class Infantry:public Soldier
{
public:
Infantry(string name = "Jack",int age = 30);
~Infantry();
void attack();
};
//Person5_2.cpp
#include<iostream>
#include"Person5_2.h"
using namespace std;
Person::Person(string name)
{
m_strName = name;
cout<<"Person()"<<endl;
}
Person::~Person()
{
cout<<"~Person()"<<endl;
}
void Person::play()
{
cout<<"Person--play()"<<endl;
cout<<m_strName<<endl;
}
//Soldier5_2.cpp
#include"Soldier5_2.h"
#include<iostream>
using namespace std;
Soldier::Soldier(string name,int age)
{
m_strName = name;
m_iAge = age;
cout<<"Soldier()"<<endl;
}
Soldier::~Soldier()
{
cout<<"~Soldier()"<<endl;
}
void Soldier::work()
{
cout<<m_strName<<endl;
cout<<m_iAge<<endl;
cout<<"Soldier--work()"<<endl;
}
//Infantry5_2.cpp
#include<string>
#include"Infantry5_2.h"
#include<iostream>
using namespace std;
Infantry::Infantry(string name,int age)
{
m_strName = name;
m_iAge = age;
cout<<"Infantry()"<<endl;
}
Infantry::~Infantry()
{
cout<<"~Infantry()"<<endl;
}
void Infantry::attack()
{
cout<<m_strName<<endl;
cout<<m_iAge<<endl;
cout<<"Infantry--attack()"<<endl;
}
//main文件
//c++远征之继承篇5-2多重继承
#include<iostream>
#include<stdlib.h>
#include"Infantry5_2.h"
/**************************************/
/* 多重继承
要求:1.Person类,数据成员:m_strName,成员函数:构造函数、析构函数、play()
2.Soldier类,数据成员:m_iAge,成员函数:构造函数、析构函数、work()
3.Infantry步兵类,数据成员:无,成员函数:构造函数、析构函数、attack()
4.定义函数test1(Person p),test2(Person &p),test3(Person *p)
/**************************************/
void test1(Person p)
{
p.play();
}
void test2(Person &p)
{
p.play();
}
void test3(Person *p)
{
p->play();
}
int main(void)
{
Infantry infantry;
test1(infantry);
test2(infantry);
test3(&infantry);
// soldier.attack();
system("pause");
return 0;
}
5-3多继承
//Farmer5_3.h
//5-3多继承
#include<string>
using namespace std;
class Farmer
{
public:
Farmer(string name = "Jack");
virtual ~Farmer();
void sow();
protected:
string m_strName;
private:
};
//Worker5_3.h
//5-3多继承
#include<string>
using namespace std;
class Worker
{
public:
Worker(string code = "001");
virtual ~Worker();
void carry();
protected:
string m_strCode;
};
//MigrantWorker5_3.h
//5-3多继承
#include<string>
#include"Farmer.h"
#include"Worker5_3.h"
class MigrantWorker:public Farmer,public Worker
{
public:
MigrantWorker(string name,string code);
~MigrantWorker();
protected:
};
//Farmer5_3.cpp
//5-3多继承
#include<iostream>
#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;
}
//Worker5_3.cpp
#include<iostream>
#include"Worker5_3.h"
using namespace std;
Worker::Worker(string code)
{
m_strCode = code;
cout<<"Worker()"<<endl;
}
Worker::~Worker()
{
cout<<"~Worker()"<<endl;
}
void Worker::carry()
{
cout<<m_strCode<<endl;
cout<<"Worker--carry()"<<endl;
}
//MigrantWorker5_3.cpp
//5-3多继承
#include"MigrantWorker.h"
#include<iostream>
using namespace std;
MigrantWorker::MigrantWorker(string name,string code):Farmer(name),Worker(code)
{
cout<<"MigrantWorker()"<<endl;
}
MigrantWorker::~MigrantWorker()
{
cout<<"~MigrantWorker()"<<endl;
}
//main
//5-3多继承
#include<iostream>
#include<stdlib.h>
#include"MigrantWorker.h"
/*******************************/
/*
多继承
要求:1.Farmer类,数据成员:m_strName,成员函数:构造函数、析构函数、sow()
2.Worker类,数据成员:m_strCode,成员函数:构造函数、析构函数、carry()
3.MigrantWorker类,数据成员:无,成员函数:构造函数、析构函数
/*******************************
int main(void)
{
MigrantWorker *p = new MigrantWorker("Merry","100");
p->carry();
p->sow();
delete p;
p = NULL;
system("pause");
return 0;
}
//6-2虚继承
//Farmer6_2.h
//6-2虚继承
#include"Person6_2.h"
#include<string>
using namespace std;
class Farmer:virtual public Person
{
public:
Farmer(string name = "Jack",string color = "blue");
virtual ~Farmer();
void sow();
protected:
string m_strName;
private:
};
//Worker6_2.h
//6-2多继承
#include"Person6_2.h"
#include<string>
using namespace std;
class Worker:virtual public Person
{
public:
Worker(string code = "001",string color = "blue");
virtual ~Worker();
void carry();
protected:
string m_strCode;
};
//MigrantWorker6_2.h
//6-2多继承
#include<string>
#include"Farmer6_2.h"
#include"Worker6_2.h"
class MigrantWorker:public Farmer,public Worker
{
public:
MigrantWorker(string name,string code,string color);
~MigrantWorker();
protected:
};
//Person6_2.h
#ifndef PERSON_H
#define PERSON_H
#include<string>
using namespace std;
class Person
{
public:
Person(string color = "blue");
virtual ~Person();
void printColor();
protected:
string m_strColor;
};
#endif
//Farmer6_2.cpp
//6-2多继承
#include<iostream>
#include"Farmer6_2.h"
using namespace std;
Farmer::Farmer(string name,string color):Person("Farmer"+color)
{
m_strName = name;
cout<<"Farmer()"<<endl;
}
Farmer::~Farmer()
{
cout<<"~Farmer()"<<endl;
}
void Farmer::sow()
{
cout<<m_strName<<endl;
cout<<"Farmer--sow"<<endl;
}
//Worker6_2.cpp
#include<iostream>
#include"Worker6_2.h"
using namespace std;
Worker::Worker(string code,string color):Person("Worker"+color)
{
m_strCode = code;
cout<<"Worker()"<<endl;
}
Worker::~Worker()
{
cout<<"~Worker()"<<endl;
}
void Worker::carry()
{
cout<<m_strCode<<endl;
cout<<"Worker--carry()"<<endl;
}
//MigrantWorker6_2.cpp
//6-2多继承
#include"MigrantWorker6_2.h"
#include<iostream>
#include<string>
using namespace std;
MigrantWorker::MigrantWorker(string name,string code,string color):Farmer(name,color),Worker(code,color)
{
cout<<"MigrantWorker()"<<endl;
}
MigrantWorker::~MigrantWorker()
{
cout<<"~MigrantWorker()"<<endl;
}
//Person6_2.cpp
#include"Person6_2.h"
#include<iostream>
#include<string>
using namespace std;
Person::Person(string color)
{
m_strColor = color;
cout<<"Person()"<<endl;
}
Person::~Person()
{
cout<<"~Person()"<<endl;
}
void Person::printColor()
{
cout<<m_strColor<<endl;
cout<<"Person--printColor()"<<endl;
}
//main文件
//6-2虚继承
#include<iostream>
#include<stdlib.h>
#include"MigrantWorker6_2.h"
/*******************************/
/*
虚继承
要求:1.Farmer类,数据成员:m_strName,成员函数:构造函数、析构函数、sow()
2.Worker类,数据成员:m_strCode,成员函数:构造函数、析构函数、carry()
3.MigrantWorker类,数据成员:无,成员函数:构造函数、析构函数
4.Person类,数据成员:m_strCode,成员函数:构造函数、析构函数、printColor()
/*******************************/
int main(void)
{
MigrantWorker *p = new MigrantWorker("Merry","200","yellow");
p->Farmer::printColor();
p->Worker::printColor();
delete p;
p = NULL;
system("pause");
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。