首页 > 其他 > 详细

实验4

时间:2019-05-21 22:54:33      阅读:120      评论:0      收藏:0      [点我收藏+]
 
技术分享图片
#ifndef CAR_H
#define CAR_H
#include<iostream>
#include<string>
using namespace std;
class Car {
public:
    Car(string maker0, string model0, int year0, int odometer0 = 0) :maker(maker0), model(model0), year(year0), odometer(odometer0) {}
    friend ostream& operator<<(ostream &out, const Car &c);
    void updateOdometer(int o2);
    string maker, model;
    int year, odometer;
};
#endif
car.h
技术分享图片
#include"car.h"
#include<string>
#include<iostream>
using namespace std;

ostream& operator<<(ostream &out, const Car &c) {
    out << "maker:" << c.maker << endl << "moder:" << c.model << endl << "year:" << c.year << endl << "odometer:" << c.odometer << endl;
    return out;
}
void Car::updateOdometer(int odometer2) {
    if (odometer2 < odometer) 
cout << "Wrong" << endl;
    else odometer = odometer2;
}
car.cpp
技术分享图片
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include"battery.h"
#include"car.h"
#include<iostream>
#include<string>

class ElectricCar:public Car,public Battery{
    public:
        ElectricCar(string maker1,string model1,int year1,int odometer1=0,int batterySize1=70):Car(maker1,model1,year1,odometer1),Battery(batterySize1){}
        friend ostream& operator<<(ostream &outCar, const ElectricCar &e);
        void updateOdometer(int odometer1);
};
#endif
electriccCar.h
技术分享图片
#include"electriccar.h"
#include<iostream>
#include"car.h"
#include"battery.h"
using namespace std;

ostream& operator<<(ostream &out, const ElectricCar &e) {
    out << "maker:" << e.maker << endl << "model:" << e.model << endl << "year:" << e.year << endl << "odometer:" << e.odometer << endl << "batterySize:" << e.batterySize << endl;
    return out;
}

void ElectricCar::updateOdometer(int odometer1) {
    if(odometer1<odometer) 
    cout<<"warning:wrong number"<<endl;
    else 
    odometer=odometer1;
}
electricCar.cpp
技术分享图片
#ifndef BATTERY_H
#define BATTERY_H

class Battery {
public:
    Battery(int size = 70);
    int rebattery();
    int batterySize;
};

#endif
battery.h
技术分享图片
#include"battery.h"

Battery::Battery(int size):batterySize(size)
{
}
int Battery::rebattery() {
    return batterySize;
}
battery.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

(第一行曾定义了代码,后面删除了,但是这个代码行不知道怎么删,只能丑点了)

技术分享图片

 

技术分享图片
#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 == nullptr) {
        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 x)
{
    return p[x];
}
arrayInt.cpp
技术分享图片
#ifndef ARRAY_INT_H 
#define ARRAY_INT_H

class ArrayInt {
public:
    ArrayInt(int n, int value = 0);
    ~ArrayInt();
    int &operator [ ](int a);    
    void print();
private:
    int *p;
    int size;
};
#endif
arrayInt.h
技术分享图片
#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

技术分享图片

 

实验4

原文:https://www.cnblogs.com/yidaoyigexiaopenyou/p/10902587.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!