本文主要是介绍C++ Car类的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目描述:
英文:Implement a Car class that includes data members to represent a car’s make (e.g., Honda), model (e.g., Civic), production year, and price. The class interface includes methods that provide appropriate access to the data members (e. g., a method to set the car’s model or to get its price)
翻译:
实现包含数据成员的Car类,这些数据成员表示汽车制造商(例如,本田)、模型(例如,Civic)、生产年份和价格。类接口包括提供对数据成员适当访问的方法(例如,设置汽车模型或获取价格的方法)
代码如下:
#include<iostream>
#include<cstring>
using namespace std;class vehicle // 定义汽车类{protected:char producer[10]; //汽车制造商char model[10]; //汽车模型char date[10]; //生产年份float price; //价格 public:void setCar(char Cproducer[],char Cmodel[],char Cdate[],float Cprice);void showCar();}; void vehicle::setCar(char Cproducer[],char Cmodel[],char Cdate[],float Cprice){for(int i=0;i<10;i++){producer[i]=Cproducer[i]; model[i]=Cmodel[i];date[i]=Cdate[i];}price=Cprice;}inline void vehicle::showCar(){cout<<"制造商:"<<producer<<endl;cout<<"模型:"<<model<<endl;cout<<"出厂日期:"<<date<<endl;cout<<"价格:"<<price<<endl;}int main(){vehicle v1; v1.setCar("Honda","Civic","2010.10.10",12.0);v1.showCar();return 0; }
最后的结果如下:
这篇关于C++ Car类的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!