十. 面向抽象类编程——大哥看场子
#include <iostream>
using namespace std;
//大哥的虚衔
class BigBrother
{
public:
//会打人
virtual void fightPeople() = 0;
};
//东方不败
class MissDongFang :public BigBrother
{
public:
virtual void fightPeople()
{
cout<<"使用了葵花宝典打人"<<endl;
}
};
//无崖子
class Wuyazi :public BigBrother
{
public:
virtual void fightPeople()
{
cout<<"使用了北冥神功打人"<<endl;
}
};
//boss
int main()
{
BigBrother * bigbrother = new Wuyazi;
//大哥你给我去打人
bigbrother->fightPeople();
delete bigbrother;
return 0;
}
十一. 纯虚函数和多继承
#include <iostream>
using namespace std;
//接口1
class Interface1
{
public:
virtual void func1(int a,int b) = 0;
virtual void func3(int a,int b) = 0;
};
//接口2
class Interface2
{
public:
virtual void func2(int a) = 0;
};
class Child: public Interface1, public Interface2
{
public:
virtual void func1(int a,int b)
{
cout<<"func1(int,int)..."<<endl;
}
virtual void func2(int a)
{
cout<<"func2(int)..."<<endl;
}
virtual void func3(int a,int b)
{
cout<<"func3(int,int)..."<<endl;
}
};
int main()
{
Interface1 * if1 = new Child;
if1->func1(10,20);
if1->func3(100,300);
Interface2 * if2 = new Child;
if2->func2(10);
return 0;
}
十二. 面向抽象类编程——动物园案例
这里总共有7个文件,除了一个主函数,另外Animal, Cat, Dog每个各自都有自己的cpp 和 h
第一个文件Animal.h
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
//纯虚函数,让子类继承并且实现
virtual void voice() = 0;
Animal();
//抽象类中,或者基类中,一旦考虑可能被继承,
//则最好把析构函数,写成虚析构函数
virtual ~Animal();
};
//架构函数
//让动物叫
void letAnimalCry(Animal * animal);
#endif // ANIMAL_H
第二个文件Cat.h
#ifndef CAT_H
#define CAT_H
#include "Animal.h"
class Cat: public Animal
{
public:
Cat();
~Cat();
virtual void voice();
protected:
private:
};
#endif // CAT_H
第三个文件Dog.h
#ifndef DOG_H
#define DOG_H
#include "Animal.h"
class Dog: public Animal
{
public:
Dog();
~Dog();
virtual void voice();
protected:
private:
};
#endif // DOG_H
第四个文件Animal.cpp
#include "Animal.h"
#include <iostream>
using namespace std;
Animal::Animal()
{
cout<<"Animal::Animal()..."<<endl;
}
Animal::~Animal()
{
cout<<"Animal::~Animal()..."<<endl;
}
void letAnimalCry(Animal * animal)
{
animal->voice();
if(animal != NULL)
{
delete animal;
}
}
第五个文件Cat.cpp
#include<iostream>
#include "Cat.h"
using namespace std;
Cat::Cat()
{
cout<<"Cat::Cat()..."<<endl;
}
Cat::~Cat()
{
cout<<"Cat::~Cat()..."<<endl;
}
void Cat::voice()
{
cout<<"小猫开始哭了,5555"<<endl;
}
第六个文件Dog.cpp
#include "Dog.h"
#include <iostream>
using namespace std;
Dog::Dog()
{
cout<<"Dog::Dog()..."<<endl;
}
Dog::~Dog()
{
cout<<"Dog::~Dog()..."<<endl;
}
void Dog::voice()
{
cout<<"小狗开始哭了,汪汪汪"<<endl;
}
第七个文件main.cpp
#include <iostream>
#include "Animal.h"
#include "Cat.h"
#include "Dog.h"
using namespace std;
int main()
{
//Animal *dog = new Dog;
//letAnimalCry(dog);
//Animal *cat = new Cat;
//cat->voice();
//delete dog;
//delete cat;
//Animal *cat = new Cat;
//letAnimalCry(cat);
//delete cat;
//法二
letAnimalCry(new Dog);
letAnimalCry(new Cat);
return 0;
}
十三. 面向抽象类编程——电脑组装1
#include <iostream>
using namespace std;
//-------抽象层--------------------
//抽象的CPU类
class CPU
{
public:
virtual void calculate() = 0;
};
//抽象的card类
class Card
{
public:
virtual void display() = 0;
};
//抽象的内存类
class Memory
{
public:
virtual void storage() = 0;
};
//架构类
class Computer
{
public:
Computer(CPU *cpu, Card *card, Memory* memory)
{
this->cpu = cpu;
this->card = card;
this->memory = memory;
}
void work()
{
this->cpu->calculate();
this->card->display();
this->memory->storage();
}
private:
CPU * cpu;
Card * card;
Memory * memory;
};
//----------------------------------
//---------实现层-----------------------
//具体的IntelCPU
class IntelCPU: public CPU
{
public:
virtual void calculate()
{
cout<<"Intel CPU 开始计算了"<<endl;
}
};
class IntelCard: public Card
{
public:
virtual void display()
{
cout<<"Intel 显卡 开始显示了"<<endl;
}
};
class IntelMemory: public Memory
{
public:
virtual void storage()
{
cout<<"Intel 内存 开始存储了"<<endl;
}
};
class NvidiaCard: public Card
{
public:
virtual void display()
{
cout<<"Nvidia 显卡 开始显示了"<<endl;
}
};
class KingstonMemory: public Memory
{
public:
virtual void storage()
{
cout<<"Kingston 内存 开始存储了"<<endl;
}
};
//----------------------------------------
//----------业务层------------------------
int main()
{
//组装第一台intel 系列的电脑
CPU* intelcpu = new IntelCPU;
Card* intelcard = new IntelCard;
Memory* intelmem = new IntelMemory;
Computer * com1 = new Computer(intelcpu,intelcard,intelmem);
com1->work();
//组装第二台电脑
Card* nvidiaCard = new NvidiaCard;
Memory* kingstonMemory = new KingstonMemory;
Computer *com2 = new Computer(intelcpu,nvidiaCard,kingstonMemory);
com2->work();
return 0;
}