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

实验四

时间:2019-05-19 17:02:44      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:cout   new   nod   []   color   编写程序   mic   open   ctr   

1

技术图片
#ifndef CAR_H
#define CAR_H
#include<string> 
#include<iostream>
using namespace std;

class Car{
         public:
             Car(string maker0,string model0,int year0,int odometer0=0);
             friend ostream& operator<<(ostream &out,const Car &t);
             int updateOdometer(int nodometer);
             string getmaker();
             string getmodel();
             int getyear();
             int getodometer();
         private:
                 string maker,model;
                 int year,odometer;
};


#endif
car.h
技术图片
#include "car.h"
#include<iostream>
#include<string> 
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,const Car &t){
    out<<"maker:"<<t.maker<<endl
       <<"model:"<<t.model<<endl
       <<"year:"<<t.year<<endl
       <<"odometer:"<<t.odometer<<endl;

    return out;
};

int Car::updateOdometer(int nodometer){
    if(nodometer<odometer)
        cout<<"更新数值有误"<<endl;
    else
        odometer=nodometer;
    
    return odometer;
};

string Car::getmaker() {
    return maker;
}
string Car::getmodel() {
    return model;
}
int Car::getyear() {
    return year;
}
int Car::getodometer() {
    return odometer;
}
Car.cpp
技术图片
#ifndef BATTERY_H
#define BATTERY_H

class Battery{
             public:
                   Battery(int batterySize0=70);
                   int getbatterySize();
             private:
                    int batterySize;
};

#endif
battery.h
技术图片
#include"battery.h"
#include<iostream>
using namespace std;

Battery::Battery(int batterySize0):batterySize(batterySize0){}

int Battery::getbatterySize(){
    return batterySize;
}
battery.cpp
技术图片
#ifndef ELECTRICAR_H
#define ELECTRICAR_H
#include "electricCar.h"
#include "car.h"
#include "battery.h"
#include<iostream>
#include<string> 
using namespace std;

class ElectricCar:public Car,public Battery{
     
      public:
             ElectricCar(string maker0,string model0,int year0,int odometer0=0);
             friend ostream& operator<<(ostream &out,const  ElectricCar &t);
      private:
              Battery battery;
};

#endif
electricCar.h
技术图片
#include "electricCar.h"
#include "car.h"
#include "battery.h"
#include<iostream>
#include<string> 
using namespace std;

ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer0):Car(maker0,model0,year0,odometer0){}

ostream & operator<<(ostream &out, ElectricCar &t){
    out<<"maker:"<<t.getmaker()<<endl
       <<"model:"<<t.getmodel()<<endl
       <<"year:"<<t.getyear()<<endl
       <<"odometer:"<<t.getodometer()<<endl
       <<"batterySize:"<<t.getbatterySize()<<"-kWh"<<endl;

    return out;
}
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

技术图片

 

技术图片
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        // 补足:将运算符[]重载为成员函数的声明
        int &operator[](int t);
        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 == 0) {
        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 t){
    return p[t];
}
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

技术图片

 

 

总结:

1.第一题没有结果,模块计算机与目标计算机冲突,我查了之后设置了项目属性和配置管理器,但还是没有用。

2.第一题编写程序的时候大概思路没什么问题,就是总是要查资料。编写出的程序调试的时候有许多的小问题,

实验四

标签:cout   new   nod   []   color   编写程序   mic   open   ctr   

原文地址:https://www.cnblogs.com/xuexinyu/p/10889782.html

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