實驗四 類的繼承
阿新 • • 發佈:2021-11-28
#ifndef __Car_HPP__ #define __Car_HPP__ #include<iostream> #include<string> #include<iomanip> using namespace std; class Car { public: Car(){} Car(string ma,string mo,int n):maker(ma),model(mo),year(n),odometers(0){} ~Car() = default; void update_odometers(int x);void info(); private: string maker; int year; string model; int odometers; }; void Car::update_odometers(int x) { if (x >= odometers) odometers = x; else cout << "更新數值有誤,請重新更新!" << endl; } void Car::info() { string a, b; int c,d; a = maker; b= model; c = year; d = odometers; cout <<left<< "maker:" << a << endl; cout <<left<<"model:" << b << endl; cout <<left<<"year:" << c << endl; cout <<left<<"odometers:" << d << endl; }#endif // !__Car_HPP__
#ifndef __battery_HPP__ #define __battery_HPP__ #include<iostream> class Battery { public: Battery() { capacity = 70; } ~Battery() = default; int get_capacity(); private: int capacity; }; int Battery::get_capacity() { return capacity; } #endif // !__battery_HPP__
#ifndef __electricCar_HPP__ #define __electricCar_HPP__ #include<iostream> #include<typeinfo> #include"car.hpp" #include"battery.hpp" class ElectricCar:virtual public Car { public: ElectricCar(){} ElectricCar(string m,string mo,int y):maker(m),model(mo),year(y),odometers(0),battery(){} ~ElectricCar() = default; void update_odometers(int x) { if (x >= odometers) odometers = x; else cout << "更新數值有誤,請重新更新!" << endl; } void info() { string a, b ; int c,d,e; a = maker; b = model; c = year; d = odometers; e = battery.get_capacity(); cout << left <<"maker:" << a << endl; cout << left <<"model:" << b << endl; cout << left << "year:" << c << endl; cout << left<<"odometers:" << d << endl; cout << left<<"capacity:" << e <<"-KWh" <<endl; } private: Battery battery; string maker; int year; string model; int odometers; }; #endif // !__electricCar_HPP__
#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(); }
#ifndef __pets_HPP__ #define __pets_HPP__ #include<iostream> #include<string> using namespace std; class MachinePets { public: MachinePets() {} MachinePets(const string s):nickname(s){} ~MachinePets() = default; virtual string string_talk(); virtual string get_nickname() { return nickname; } private: string nickname; }; string MachinePets::string_talk() { return "papapapa"; } class PetCats : virtual public MachinePets { public: PetCats(){} PetCats(const string s):nickname(s){} ~PetCats() = default; string string_talk() { return "miao miao"; } string get_nickname() { return nickname; } private: string nickname; }; class PetDogs :virtual public MachinePets { public: PetDogs(){} PetDogs(const string s):nickname(s){} ~PetDogs() = default; string string_talk() { return "wang wang"; } string get_nickname() { return nickname; } private: string nickname; }; #endif // !__pets_HPP__
#include <iostream> #include "pets.hpp" void play(MachinePets* ptr) { std::cout << ptr->get_nickname() << " says " << ptr->string_talk() << std::endl; } int main() { PetCats cat("miku"); PetDogs dog("da huang"); play(&cat); play(&dog); }