码迷,mamicode.com
首页 > 其他好文 > 详细

实验4

时间:2019-05-18 15:59:48      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:pre   mes   this   else   div   需要   注意   lse   image   

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

技术图片
#ifndef CAR_H
#define CAR_H
#include<string>
#include<iostream>
using namespace std;
class Car {
public:
    Car();
    Car(string m, string mo, int y, int o=0 ) :maker(m), model(mo), year(y) ,odometer(o){}
    friend ostream &operator<<(ostream &out,Car &t);
    string getm();
    string getmo();
    int gety();
    int geto();
    void updateOdometer(int m);
private:
    string maker;
    string model;
    int year;
    int odometer;
};

#endif 
car.h
技术图片
#ifndef BATTERY_H
#define BATTERY_H
using namespace std;
class Battery {
public:
    Battery();
    Battery(int s);
    int getsize();
private:
    int size;
};
#endif
battery.h
技术图片
#ifndef ELECTRICCAT_H
#define ELECTRICCAR_H
#include"car.h"
#include"battery.h"
#include<string>
using namespace std;
class ElectricCar:public Car {
public:
    ElectricCar(string a, string b, int c, int d=0);
    void updateOdometer(int m);
    friend ostream &operator<<(ostream &out, ElectricCar &q);
private:
    Battery battery;
};

#endif
electricCar.h
技术图片
#include"car.h"
#include<iostream>
using namespace std;
ostream &operator<<(ostream &out, Car &t) {
    out << "maker:" << t.maker << endl << "model:" << t.model << endl << "year:" << t.year << endl << "odometer:" << t.odometer;
    return out;
}
Car::Car() {
}
void Car::updateOdometer(int m) {
    int &n = m;
    if (n < odometer)
    {
        cout << "Warning,Update is wrong " << endl;
    }
   else
        odometer = n;
}
string Car::getm() {
    return maker;
}
string Car::getmo() {
    return model;
}
int Car::gety(){
    return year;
}
int Car::geto() {
    return odometer;
}
car.cpp
技术图片
#include"battery.h"
Battery::Battery() {
    size=70;
}
Battery::Battery(int s) {
    size = s;
}
int Battery::getsize() {
    return size;
}
battery.cpp
技术图片
#include"electricCar.h"
#include"car.h"
#include"battery.h"
#include<iostream>
using namespace std;
ElectricCar::ElectricCar(string a, string b, int c, int d):Car(a, b, c, d){

}
ostream &operator<<(ostream &out, ElectricCar &q) {
    out << "maker:" << q.getm() << endl << "model:" << q.getmo() << endl << "year:" << q.gety() << endl << "odometer:" << q.geto()<< endl;
    out << "battery:" << q.battery.getsize() << "-kWh" ;
    return out;
}
void ElectricCar::updateOdometer(int m) {
    this->Car::updateOdometer(m);
}
electricCar.cpp
技术图片
#include <iostream>
using namespace std;

#include "car.h"
#include "electricCar.h" 

int main() {
    // 测试Car类 
    Car oldcar("Audi","a4",2016);
    cout << "--------oldcar‘s info--------" << endl;
    oldcar.updateOdometer(25000);
    cout << oldcar << endl;

    // 测试ElectricCar类 
    ElectricCar newcar("Tesla","model s",2016);
    newcar.updateOdometer(2500);
    cout << "\n--------newcar‘s info--------\n"; 
    cout << newcar << endl;

    system("pause");
    
    return 0;
}
main.cpp

 

技术图片

 

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

技术图片
#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
技术图片
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];
}
arrayInt.cpp
技术图片
#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();

    system("pause");

    return 0;
}
main.cpp

技术图片

1.第一题我的问题在于派生类如何使用继承类的函数和构造函数,如果函数同名的话需要用到指针。但是我始终不懂为什么不能将

ElectricCar::ElectricCar(string a, string b, int c, int d):Car(a, b, c, d){
}
写成
ElectricCar::ElectricCar(string a, string b, int c, int d){
Car(a, b, c, d);
}这种形式。两种似乎差不多。
2.第二题只要添加一两行就可以,但是要注意返回值问题。我一直没注意返回值的类型,懵了十分钟最后问了一下老师才醒悟。
3.这两题可以看出我对最近学习的知识的细节掌握的不够好。只知道一个大概。
 

 

实验4

标签:pre   mes   this   else   div   需要   注意   lse   image   

原文地址:https://www.cnblogs.com/jiyuanxiangzhouziying/p/10883667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!