2017C++基础——网课笔记【下】(10到14)

十. 面向抽象类编程——大哥看场子

2017C++基础——网课笔记【下】(10到14)_第1张图片

#include 

using namespace std;

//大哥的虚衔
class BigBrother
{
public:
    //会打人
    virtual void fightPeople() = 0;
};

//东方不败
class MissDongFang :public BigBrother
{
public:
    virtual void fightPeople()
    {
        cout<<"使用了葵花宝典打人"<fightPeople();

    delete bigbrother;
    return 0;
}

十一. 纯虚函数和多继承

#include 

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)..."<func1(10,20);
    if1->func3(100,300);

    Interface2 * if2 = new Child;

    if2->func2(10);


    return 0;
}

十二. 面向抽象类编程——动物园案例

2017C++基础——网课笔记【下】(10到14)_第2张图片


这里总共有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 

using namespace std;

Animal::Animal()
{
    cout<<"Animal::Animal()..."<voice();

    if(animal != NULL)
    {
        delete animal;
    }
}

第五个文件Cat.cpp

#include
#include "Cat.h"

using namespace std;

Cat::Cat()
{
    cout<<"Cat::Cat()..."<

第六个文件Dog.cpp

#include "Dog.h"
#include 

using namespace std;

Dog::Dog()
{
    cout<<"Dog::Dog()..."<

第七个文件main.cpp

#include 
#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

2017C++基础——网课笔记【下】(10到14)_第3张图片


#include 

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 开始计算了"<work();

    //组装第二台电脑
    Card* nvidiaCard = new NvidiaCard;
    Memory* kingstonMemory = new KingstonMemory;

    Computer *com2 = new Computer(intelcpu,nvidiaCard,kingstonMemory);
    com2->work();


    return 0;
}

你可能感兴趣的:(C++,学习笔记)