实验4

task3

Battery.hpp

#ifndef BATTERY_HPP
#define BATTERY_HPP
#include<bits/stdc++.h>
using namespace std;

class Battery
{
    public:
        Battery(int a=70):capacity(a){}
        int get_capacity(){
            return this->capacity;
        }
    private:
        int capacity;
};

#endif

Car.hpp

#ifndef CAR_HPP
#define CAR_HPP
#include<bits/stdc++.h>
using namespace std;
class Car {
    public:
        Car(string Maker,string Model,int Year):maker(Maker),model(Model),year(Year) {this->odometers=0;}
        void info() {
            cout<<"maker:        "<<maker<<endl;
            cout<<"model:        "<<model<<endl;
            cout<<"year:        "<<year<<endl;
            cout<<"odometers:    "<<odometers<<endl;
        }
        void update_odometers(double a){
            if(a<this->odometers){
                cout<<"输入信息有误"<<endl;; 
            }
            else{    
                this->odometers=a;
            }
        }
        
        
    private:
        string maker;
        string model;
        int year;
        double odometers;
};

#endif

electricCar.hpp

#ifndef ELECTRICCAR_HPP
#define ELECTRICCAR_HPP
#include<bits/stdc++.h>
#include"Battery.hpp"
#include"Car.hpp"
using namespace std;
class ElectricCar:public Car
{
    public:
        ElectricCar(string maker,string model,int year,double battery=70):Car{maker,model,year},battery(battery){}
        void info(){
            Car::info();
            cout<<"capacity:    "<<this->battery.get_capacity()<<"kwh"<<endl;
        }
        
    private:
        Battery battery;
};

#endif

main.cpp

#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();
}

 

 

-----------------------------------------------------------------------------------------

task4

MachinePets.hpp

#ifndef MACHINEPETS_HPP
#define MACHINEPETS_HPP
#include<bits/stdc++.h>
using namespace std;

class MachinePets
{
    public:
        MachinePets(const string s):nickname(s){} 
        virtual string talk(){
            return "A";
        }
        string get_nickname(){
            return this->nickname;
        }
    private:
        string nickname;
};

#endif

Pets.hpp

#ifndef PETDOGS_HPP
#define PETDOGS_HPP
#include"MachinePets.hpp"
#include<bits/stdc++.h>
using namespace std;

class PetCats:public MachinePets{
    public:
        PetCats(const string s):MachinePets(s) {}
        string talk() {
            return "wangwang~";
        }
    private:

};
class PetDogs:public MachinePets {
    public:
        PetDogs(const string s):MachinePets(s) {}
        string talk() {
            return "miaowu~";
        }
    private:

};
#endif

main.cpp

#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);
}

 

posted @ 2021-12-01 10:49  黄金派大星  阅读(18)  评论(3编辑  收藏  举报