实验四

 

1、

#ifndef CAR_H
#define CAR_H
#include <string>
using std::string;

class Car{
    public:
        Car(string newma,string newmo,int newy,int newo=0);
        friend std::ostream &operator <<(std::ostream &out,Car &c);
        int updateOdometer(int newo);//更新里程数 
        string getMaker();
        string getModel();
        int getYear();
        int getOdometer();
    private:
        string maker;
        string model;
        int year;
        int odometer;
};

#endif
car.h
#include "car.h"
#include 
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
Car::Car(string newma,string newmo,int newy,int newo):maker(newma),model(newmo),year(newy),odometer(newo){
};
string Car::getMaker(){
    return maker;
}
string Car::getModel(){
    return model;
}
int Car::getYear(){
    return year;
}
int Car::getOdometer(){
    return odometer;
}
int Car::updateOdometer(int newo){
    if(odometer<=newo)
        odometer=newo;
    else
    {
        cout<<"wrong    !"<<endl;
        cin>>odometer;
    }
    return odometer;
}
std :: ostream & operator << (std::ostream &out,Car &c){
    out<<"maker: "<endl
    <<"model: "<endl
    <<"year: "<endl
    <<"odometer: "<endl;
    return out;
}
car.cpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include "car.h"
#include "battery.h"

class ElectricCar:public Car,public Battery {
    public:
        ElectricCar(string newma,string newmo,int newy,int newo=0);
        friend std::ostream & operator<<(std::ostream &out,ElectricCar &c);
};

#endif
electriccar.h
#include "car.h"
#include "battery.h"
#include "electricCar.h"
#include 
using namespace std;

ElectricCar::ElectricCar(string newma,string newmo,int newy,int newo):Car(newma,newmo,newy,newo){
}
std::ostream & operator<<(std::ostream &out,ElectricCar &c){
    out<<"maker: "<endl
    <<"model: "<endl
    <<"year: "<endl
    <<"odomter: "<endl
    <<"batterySize: "<"-KWh"<<endl;
    return out;
}
electriccar.cpp
#ifndef BATTERY_H
#define BATTERY_H

class battery{
    public:
        battery(int newb=70);
        int getbattery();
    private:
        int batterySize;
};

#endif
battery.h
#include "battery.h"
#include 
using namespace std;

battery::battery(int newb):batterySize(newb){
};
int battery::getbattery(){
    return batterySize;
}
battery.cpp
#include 
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

结果:

实验四_第1张图片

 

 

2、

#ifndef ARRAYINT_H
#define ARRAYINT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        int & operator [](int k);
        void print(); 
    private:
        int *p;
        int size;
};

#endif
arrayint.h
#include "arrayint.h"
#include 
#include 
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)
        p[i] = value;
}
ArrayInt::~ArrayInt() {
    delete[] p;
}
void ArrayInt::print() {
    for(int i=0; i)
        cout << p[i] << " ";
    cout << endl;
}
int &ArrayInt :: operator [] (int k){
    return p[k];
}
arrayint.cpp
#include 
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

结果:

实验四_第2张图片

 

 

实验小结:

1、<<的重载不太熟练,反复看了书;

2、p==nullptr或者p==NULL或者!p都可以。

你可能感兴趣的:(实验四)