实验四

1. 车辆基本信息管理 问题场景描述如下: 为了对车量基本信息进行管理,对现实世界车量基本信息  抽象后,抽象出Car类、ElectricCar类、Battery类, 它们之间的关系描述如下:基于Car类派生出ElectricCar类,派生类ElectricCar中新增数据成员为Battery类 对象。

#include <iostream>
#include "battery.h"
#include <string>
#include "car.h"
#include "electricCar.h" 
using namespace std;

int main() {
    battery s;
    // 测试Car类 
    car oldcar("Audi","a4",2016);
    cout << "--------oldcar's info--------" << endl;
    oldcar.updateOdometer(25000);
    cout << oldcar << endl;
    oldcar.updateOdometer(0);
    cout << oldcar << endl;
    
    // 测试ElectricCar类 
    electricCar newcar("Tesla","model s",2016);
    newcar.updateOdometer(2500);
    cout << "\n--------newcar's info--------\n"; 
    cout << newcar << endl;
    return 0;
}
main.cpp
#ifndef BETTERY_H
#define BETTERY_H

class battery{
   public:
       battery(int size0=70);
       int batterySlze;
};

#endif
battery.h
#include "battery.h"
#include <iostream>
using namespace std;

battery::battery(int size0):batterySlze(size0){
    
}
battery.cpp
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
using namespace std;

class car{
    public:
        car(string maker0,string model0,int year,int odometer0=0);
        friend ostream &operator<<(ostream &out,car &ca);
        void updateOdometer(int newodometer);
        string maker;
        string model;
        int year;
        int odometer;
    
    
    
};
#endif
car.h
#include "car.h"
#include <string>
#include <iostream>
using namespace std;

car::car(string maker0,string model0,int year0,int odometer0):maker(maker0),model(model0),year(year0),odometer(odometer0){
}


ostream &operator<<(ostream &out,car &ca){
  out<<"maker:"<<ca.maker<<endl
     <<"model:"<<ca.model<<endl
     <<"year:"<<ca.year<<endl
     <<"odometer:"<<ca.odometer<<endl;
  return out;
}

void car::updateOdometer(int newodometer){
  if(newodometer<=odometer)
  { int n;
    cout<<"It is wrong.Input again ,please:";cin>>n;
    odometer=n;
 }
  
  else
    odometer=newodometer;
}
car.cpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include "car.h"
#include "battery.h"
#include <string>
#include <iostream>
using namespace std;
 
class electricCar:public car,public battery{
    public:
        electricCar(string maker0,string model0,int year,int odometer0=0);
        friend ostream &operator<<(ostream &out,electricCar &ca);
    private:
        battery batt;
};
#endif
electriccar.h
#include "electricCar.h"
#include <string>
#include <iostream>
using namespace std;

electricCar::electricCar(string maker0,string model0,int year0,int odometer0):car(maker0,model0,year,odometer0){
}


ostream &operator<<(ostream &out,electricCar &ca){
  out<<"maker:"<<ca.maker<<endl
     <<"model:"<<ca.model<<endl
     <<"year:"<<ca.year<<endl
     <<"odometer:"<<ca.odometer<<endl;
   out<<"batterySize:"<<ca.batt.batterySlze<<"-kWh"<<endl;
  return out;
}
electriccar.cpp

 

2.补足程序,重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以 访问对象中具体元素。 

#include <iostream>
using namespace std;

#include "arrayInt.h"

int main() {
    // 定义动态整型数组对象a,包含2个元素,初始值为0
    ArrayInt a(2);
    a.print();
    
    // 定义动态整型数组对象b,包含3个元素,初始值为6
    ArrayInt b(3, 6);
    b.print();

    // 通过对象名和下标方式访问并修改对象元素
    b[0] = 2;
    cout << b[0] << endl;
    b.print();
    return 0;
}
main.cpp
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        int &operator[](int i); // 补足:将运算符[]重载为成员函数的声明
        // ×××
        void print(); 
    private:
        int *p;
        int size;
};

#endif
arrayInt.h
#include "arrayInt.h"
#include <iostream>
#include <cstdlib>
using std::cout;
using std::endl;

ArrayInt::ArrayInt(int n, int value): size(n) {
    p = new int[size];
    
    if (p == NULL) {
        cout << "fail to mallocate memory" << endl;
        exit(0); 
    } 
    
    for(int i=0; i<size; i++)
        p[i] = value;
}

ArrayInt::~ArrayInt() {
    delete[] p;
}

void ArrayInt::print() {
    for(int i=0; i<size; i++)
        cout << p[i] << " ";
    cout << endl;
}

 int &ArrayInt::operator[](int i){
     return p[i];
 }// 补足:将运算符[]重载为成员函数的实现
// ×××
arryInt.cpp

 

小结

  1. 又是一次困难痛苦的过程,本次实验就是在学习大佬的程序中过来的,第一题中electriccar完全无思路,在翻阅书籍和查看大佬的代码无数次后终于摸索出了一二,还是有好多模糊的地方,继承和派生到底是什么呢?重载运算符时什么时候operator前要用"&"呢?
  2. 第二题要是老师课上不讲解,我又是交白卷了,在实践过程中在同学的帮助下把“nullptr”改成了“NULL”,之所以知道在null这里出了编译问题是多亏了一位大佬(有趣的灵魂)的作业啊,真忍不住给他(她)点推荐。

评论的地址

  1. https://www.cnblogs.com/joey-yan/p/10856607.html
  2. https://www.cnblogs.com/nnn13579/p/10863575.html
  3. https://www.cnblogs.com/21savage-code/p/10877527.html

 

posted @ 2019-05-19 16:25  Ann_88  阅读(113)  评论(1编辑  收藏  举报