zoukankan      html  css  js  c++  java
  • 11.24

    #include<iostream>
    #include<iomanip>
    #include<string>
    
    using namespace std;
    
    class Car
    {
    public:
        Car(string maker, string model, int year)
        {
            this->maker = maker;
            this->model = model;
            this->year = year;
            odometers = 0; 
        }
        void info();
        void update_odometers(int a);
        
    private:
        string maker;
        string model;
        int year;
        int odometers;    
    };
    
    void Car::info()
    {
        cout << left << setw(15) << "maker:" << maker << endl;
        cout << left << setw(15) << "model:" << model << endl;
        cout << left << setw(15) << "year:" << year << endl;
        cout << left << setw(15) << "odometers:" << odometers << endl;
    }
    
    void Car::update_odometers(int a)
    {
        if(a < 0)
        {
            cout << "更新数值有误" << endl; 
        }
        else
        {
            odometers += a;
        }
        
    }
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    class Battery
    {
    public:
        Battery(int input = 70)
        {
            capacity = input;
        }
        int get_capacity(); 
    private:
        int capacity;    
    };
    
    int Battery::get_capacity()
    {
        return capacity;
    }
    #include<iostream>
    #include<string>
    #include<iomanip>
    #include "Car.hpp"
    #include "battery.hpp"
    
    using namespace std;
    
    class ElectricCar: public Car
    {
        
    private:
        Battery battery;
    public:
        ElectricCar(string maker, string model, int year,int batter = 70):Car(maker,model,year),battery(batter){}
        void info();
    };
    
    void ElectricCar::info()
    {
        Car::info();
        cout << left << setw(15) << "capacity:" << battery.get_capacity() << "-kWh" << endl;
     }
    #include <iostream>
    #include "electricCar.hpp"
    
    int main()
    {
        using namespace std;
    
        // test class of Car
        Car oldcar("Audi", "a4", 2016);
        cout << "--------oldcar's info--------" << endl;
        oldcar.update_odometers(25000);
        oldcar.info();
    
        cout << endl;
    
        // test class of ElectricCar
        ElectricCar newcar("Tesla", "model s", 2016);
        newcar.update_odometers(2500);
        cout << "\n--------newcar's info--------\n";
        newcar.info();
    }

    #include<iostream>
    #include<string>
     
    using namespace std;
    
    class MachinePets
    {
    public:
        MachinePets( const string nic)
        {
            nickname = nic;
        }
        virtual string talk()
        {;
        }
        
        string get_nickname();
    private:
        string nickname;
    }; 
    
    string MachinePets::get_nickname()
    {
        return nickname;
    }
    
    class PetCats:public MachinePets
    {
    public:
        PetCats(const string s):MachinePets(s){}
        string talk()
        {
            string a = "miao wu~";
            return a;
        }    
        
    };
    
    class PetDogs:public MachinePets
    {
    public:
        PetDogs(const string s):MachinePets(s){}
        string talk()
        {
            string a = "wang wang~";
            return a;
        }
        
    };
      
    #include <iostream>
    #include "pets.hpp"
    
    void play(MachinePets *ptr)
    {
        std::cout << ptr->get_nickname() << " says " << ptr->talk() << std::endl;
    }
    
    int main()
    {
        PetCats cat("miku");
        PetDogs dog("da huang");
    
        play(&cat);
        play(&dog);
    }

     实验总结:

    这次实验复习类的组成,学习了虚函数的使用方法,算是很有收获的!!

  • 相关阅读:
    [HTML]HTML5实现可编辑表格
    [xcode]instruments来检验你的app
    [xcode]Xcode查找函数(方法)调用及被调用
    [工具][windows][visualStudio][充电]番茄助手vaassist常见用法
    [算法]判断一个数是不是2的N次方
    [Ogre]纹理设置
    C++中的类型重定义
    [STL]set/multiset用法详解[自从VS2010开始,set的iterator类型自动就是const的引用类型]
    [Ogre][地形][原创]基于OgreTerrain的地形实现
    PHP模板引擎Smarty内建函数section,sectionelse用法详解
  • 原文地址:https://www.cnblogs.com/2967271912lala/p/15626819.html
Copyright © 2011-2022 走看看