实验四 继承
#ifndef BATTER_HPP #define BATTER_HPP #include <iostream> using namespace std; class Batter { public: Batter(int capacity = 70) : capacity(capacity) {} int get_capacity(); int capacity; }; int Batter::get_capacity() { return capacity; } #endif
#ifndef CAR_HPP #define CAR_HPP #include <iostream> #include <string> #include <iomanip> using namespace std; class Car { public: Car(){}; Car(string maker, string model, int year,int odometers = 0); void info(); void update_odometers(int meters); protected: string maker; string model; int year; int odometers; }; Car::Car(string maker, string model, int year,int odometers) { this->maker = maker; this->model = model; this->year = year; this->odometers = odometers; } void Car::info() { cout <<left<< setw(15) << "makers: " << maker << endl; cout << setw(15) << "model: " << model << endl; cout << setw(15) << "year: " << year << endl; cout << setw(15) << "odometers: " << odometers << endl; } void Car::update_odometers(int meters) { if (meters < 0) { cout << "输入的数值有误"; } else { odometers += meters; } } #endif
#ifndef ELECTRICCAR_HPP #define ELECTRICCAR_HPP #include "Car.hpp" #include "batter.hpp" class ElectricCar : public Car { public: ElectricCar(string maker,string model,int year,int odometers = 0); void info(); protected: Batter battery; }; ElectricCar::ElectricCar(string maker,string model,int year,int odometers) { this->maker=maker; this->model=model; this->year=year; this->odometers = odometers; } void ElectricCar::info() { cout << setw(15) << "makers: " << maker <<endl; cout << setw(15) << "model: " << model <<endl; cout << setw(15) << "year: " << year <<endl; cout << setw(15) << "odometers: " << odometers <<endl; cout << setw(15) << "capacity: " << battery.get_capacity() <<"-kWh"<<endl; } #endif
#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); virtual string talk(); string get_nickname() { return nickname; } string nickname; }; MachinePets::MachinePets(const string s) { nickname = s; } string MachinePets::talk() { return "Ao~"; } class PetCats : public MachinePets { public: PetCats(){} PetCats(string const s); string talk(); }; PetCats::PetCats(string const s) { nickname = s; } string PetCats::talk() { return "miao wu~"; } class PetDogs:public MachinePets { public: PetDogs(){} PetDogs(string const s); string talk(); }; PetDogs::PetDogs(string const s) { nickname = s; } string PetDogs::talk() { return "wang wang~"; } #endif
#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); }