实验四

#include <iostream>
using namespace std;
#include "Car.h"
#include "elsctricCar.h"
int main(){
    Car oldcar("Audi","a4",2016);
    cout <<"----------oldcar's info-----------"<<endl;
    oldcar.updateOdometer(25000);
    cout <<oldcar<< endl;
    ElectricCar newcar("Tesla"."model s",2016);
    oldcar.updateOdometer(25000);
    cout <<"\n----------oldcar's info-----------\n";
    cout << newcar << endl;
    system("pause");
    return 0;
} 
main
#include "battery.h"
Battery::Battery(int bat){
    batterySize=bat; 

} 
int Battery::size(){
    return batterySize;
}
battery.cpp
#ifndef BATTARY_H
#define BATTERY_H
class Battery{
    public:
        Battery(int bat=70);
        int batterySize;
        int size();
}; 
#endif
battery.h
#include "car.h"
#include <iostream>
#include <string>
using namespace std;
void Car::updateOdometer(int odometer1){
    if(odometer1<odometer){
        cout<<"the odometer is wrong"<<endl;
        else
           odometer=odometer1;
    }
}
ostream & operator<<(ostream &out,Car &c){
    out << "maker:" << c.maker << endl << "model:" << c.model << endl << "year:" << c.year << endl << "odometer:" << c.odometer << endl;
    return out;
}
car.cpp
#ifndef CAR_H
#define CAR_H
#include <iostream>
#include <string>
using namespace std;
class Car{
    public:
        Car::Car(string maker1,string model1,int year1,int odometer1):market(market1),model(model1),year(year1),odometer(odometer1){
}       
        friend ostream& operator<<(ostream &out,Car &c);
        void updateOdometer (int odometer1);
        string maker;
        string model;
        int odometer;
        int year;    
};
#endif
car.h
#include"electricCar.h"
#include"battery.h"
#include"car.h"
#include<iostream>
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<<"the number is wrong"<<endl;
    else
    odometer=odometer1;
}
electricCar.cpp
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include"battery.h"
#include"car.h"
#include<iostream>
#include<string>
class ElectricCar:public Car,public Battery {
    piblic:
        ElectricCar(string maker2,string model2,int year2,int odometer2=0,int size2=70):Car(maker2,model2,year2,odometer2),Battery(size2){}
        friend ostream& operator<<(ostream &outCar, const ElectricCar &e);
        void updateOdometer(int odometer1);
        
};
#endif
electricCar.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
#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 "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) {
        cout << "fail to mallocate memory" << endl;
        exit(0); 
    } 
    
    for(int i=0; i<size; i++)
        p[i] = value;
}

ArrayInt::~ArrayInt() {
}

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

int & ArrayInt::operator[](int a){
    return p[a];
}
arrayInt.cpp

小结:对于这些题型还是需要借助书籍和网上别人写的程序,模仿写出来,掌握的还是不够熟练,希望自己能多加练习。

 

 

https://www.cnblogs.com/qiuqiuwr/p/10883165.html

https://www.cnblogs.com/jyf13/p/10888786.html

https://www.cnblogs.com/yfwg/p/10886264.html

 

posted @ 2019-05-19 14:40  小眉毛  阅读(324)  评论(2编辑  收藏  举报