实验四 继承
作者:互联网
实验三
Car.hpp
#pragma once #include<iostream> #include<string> #include<iomanip> using namespace std; class Car { public: Car(string _maker, string _model, int _year, double _oddmeters = 0 ); Car(Car& c); void info(); void update_oddmeters(double newoddmeters); private: string maker, model; int year; double oddmeters; }; Car::Car(string _maker, string _model, int _year, double _oddmeters ) { maker = _maker; model = _model; year = _year; oddmeters = _oddmeters; } Car::Car(Car& c) { maker = c.maker; model = c.model; year = c.year; oddmeters = c.oddmeters; } void Car::info() { cout << setiosflags(ios::left); cout << setw(15) << "maker:" << maker << endl; cout << setw(15) << "model:" << model << endl; cout << setw(15) << "year:" << year << endl; cout << setw(15) << "oddmeters:" << oddmeters << endl; } void Car::update_oddmeters(double newoddmeters) { if (newoddmeters < oddmeters) { cout << "error" << endl; } else { oddmeters = newoddmeters; } }
ElectricCar.hpp
#pragma once #include"Car.hpp" #include"Battery.hpp" class ElectricCar :public Car { public: ElectricCar(string _maker,string _model,int _year,double _oddmeters = 0, int _capacity = 70); void info(); private: Battery battery; }; ElectricCar::ElectricCar(string _maker, string _model, int _year, double _oddmeters, int _capacity) : Car(_maker, _model, _year, _oddmeters), battery(_capacity) { } void ElectricCar::info() { Car::info(); cout << setw(15) << "capacity:" << battery.get_capacity() << "-kwh" << endl; }
Battery.hpp
#pragma once #include<iostream> using namespace std; class Battery { public: Battery(int _capacity = 70); Battery(Battery& b); int get_capacity() { return capacity; } private: int capacity; }; Battery::Battery(int _capacity) :capacity{ _capacity } { } Battery::Battery(Battery& b) { capacity = b.capacity; }
task3.cpp
#include"ElectricCar.hpp" int main() { //test class of Car Car oldcar("Audi", "a4", 2016); cout << "---------oldercar's info---------" << endl; oldcar.update_oddmeters(25000); oldcar.info(); cout << endl; //test class of ElectricCar ElectricCar newcar("Tesla", "model s", 2016); newcar.update_oddmeters(25000); cout << "\n---------newcar's info--------\n"; newcar.info(); }
task4
pets.hpp
#pragma once #include<iostream> #include<string> using namespace std; class MachinePets { private: string nickname; public: MachinePets(const string _nickname):nickname(_nickname){} MachinePets(const MachinePets& m) { nickname = m.nickname; } virtual string talk() { return " "; } string get_nickname() { return nickname; } }; class PetCats:public MachinePets { public: PetCats(const string s):MachinePets(s){} string talk(); }; string PetCats::talk() { return "mioa wu~"; } class PetDogs :public MachinePets { public: PetDogs(const string s):MachinePets(s){} string talk(); }; string PetDogs::talk() { return "wang wang~"; }
task4.hpp
#include"pets.hpp" void play(MachinePets* ptr) { cout << ptr->get_nickname() << " says " << ptr->talk() << endl; } int main() { PetCats cat("miku"); PetDogs dog("da huang"); play(&cat); play(&dog); }
标签:oddmeters,capacity,string,继承,Car,Battery,int,实验 来源: https://www.cnblogs.com/remakeG/p/15601151.html