11.24
2021/12/1 6:06:36
本文主要是介绍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); }
实验总结:
这次实验复习类的组成,学习了虚函数的使用方法,算是很有收获的!!
这篇关于11.24的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?