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

实验四

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

标签:参数传递   null   pause   update   names   wro   font   show   val   

实验结论:

(1)

技术图片
#ifndef BATTERY_H
#define BATTERY_H
class Battery
{
public:
    Battery(int x=70);
    int showbattery();
    int batterySize;

};
#endif
battery.h
技术图片
#ifndef CAR_H
#define CAR_H
#include <string>
#include <iostream>
using namespace std;
class Car {
public:
    Car(string x, string y, int a, int b = 0);
    friend ostream&operator<<(ostream &out, Car &a);
    void updateOdometer(int x);
    string maker, model;
    int year, odometer;

};
#endif
car.h
技术图片
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include "battery.h"
#include "car.h"
class ElectricCar :public Car, public Battery {
public:
    ElectricCar(string x, string y, int a, int b = 0,int c=70);
    friend ostream&operator<<(ostream &out, ElectricCar &a);
private:
    Battery battery;

};
#endif 
electricCar.h
技术图片
#include "battery.h"
Battery::Battery(int x):batterySize(x)
{
}
int Battery::showbattery() {
    return batterySize;
}
battery.cpp
技术图片
#include <iostream>
using namespace std;
#include <string>
#include "car.h"
Car::Car(string x, string y, int a, int b ) :maker(x), model(y), year(a), odometer(b) {

}
ostream &operator<<(ostream &out, Car &a) {
    out << "maker:" << a.maker << endl
        << "model:" << a.model << endl
        << "year:" << a.year << endl
        << "odometer:" << a.odometer << endl;
    return out;
}
void Car::updateOdometer(int x) {
    if ( x < odometer)
        cout << "the new odometer is wrong!" << endl;
    else
        odometer = x;
    odometer = x;
}
car.cpp
技术图片
#include "electricCar.h"
#include "car.h"
#include "battery.h"
#include <iostream>
using namespace std;
ElectricCar::ElectricCar(string x, string y, int a, int b, int c) :Car(x, y, a, b),Battery(c){

}
ostream &operator<<(ostream &out, ElectricCar &a) {
    out << "maker:" << a.maker << endl
        << "model:" << a.model << endl
        << "year:" << a.year << endl
        << "odometer:" << a.odometer << endl
        << "batterysize:" << a.batterySize <<"-kwh"<< endl;
        return out;
}
elecrtricCar.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)

技术图片
#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        int &operator[](int x);
        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 == 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
技术图片
#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、类的派生与继承,编程过程中纠正很多细节性错误,头文件类后的分号,参数传递,初始化方式。

实验四

标签:参数传递   null   pause   update   names   wro   font   show   val   

原文地址:https://www.cnblogs.com/zxz2425405395/p/10885689.html

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