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

实验4

时间:2019-05-20 23:04:44      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:ref   log   函数   col   用法   形参   amp   http   car   

一、Car

 1 #ifndef CAR_H
 2 #define CAR_H
 3 #include<string>
 4 using namespace std;
 5 class Car
 6 {
 7 public:
 8     Car(string mymaker, string mymodel, int myyear, int myodometer = 0);
 9     friend ostream& operator << (ostream&out, const Car &t);
10     void updateOdometer(int sum);
11 private:
12     string maker;
13     string model;
14     int year;
15     int odometer;
16 };
17 #endif
 1 #ifndef BATTERY_H
 2 #define BATTERY_H
 3 class Battery
 4 {
 5     public:
 6         Battery(int mybatterysize = 70);
 7         void myshow();
 8     private:
 9         int batterysize;
10 
11 
12 };
13 #endif
 1 #ifndef ELECTRIC_H
 2 #define ELECTRIC_H
 3 #include"car.h"
 4 #include"battery.h"
 5 #include<string>
 6 using namespace std;
 7 class ElectricCar :public Car
 8 {
 9 public:
10     ElectricCar(string mymaker, string mymodel, int myyear, int myodometer=0,int mybatterysize=70) :Car(mymaker, mymodel, myyear, myodometer), a(mybatterysize){}
11     friend ostream& operator << (ostream&out, ElectricCar &t);
12 private:
13     Battery a;
14 };
15 #endif
 1 #include<iostream>
 2 #include<string>
 3 #include"car.h"
 4 using namespace std;
 5 Car::Car(string mymaker, string mymodel, int myyear, int myodometer)
 6 {
 7     maker = mymaker;
 8     model = mymodel;
 9     year = myyear;
10     odometer = myodometer;
11 }
12 ostream& operator<<(ostream&out, const Car &t)
13 {
14     out << "Maker:" << t.maker << endl << "Model:" << t.model << endl << "Year:" << t.year << endl << "Odometer:" << t.odometer << endl;
15     return out;
16 }
17 void Car::updateOdometer(int sum)
18 {
19     if (sum < odometer)
20         cout << "Warning!" << endl;
21     else
22     {
23         odometer = sum;
24     }
25 }
 1 #include<iostream>
 2 #include"battery.h"
 3 using namespace std;
 4 Battery::Battery(int mybatterysize)
 5 {
 6     batterysize = mybatterysize;
 7 
 8 }
 9 void Battery::myshow()
10 {
11     cout << "Batterysize:" << batterysize<<"-kWh" << endl;
12 
13 }
 1 #include<iostream>
 2 #include<string>
 3 #include"electricCar.h"
 4 ostream& operator << (ostream&out, ElectricCar &t)
 5 {
 6     Car m=t; 
 7     out<<m;
 8     t.a.myshow();
 9     return out;
10 }

技术图片

二、arrey

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        // 补足:将运算符[]重载为成员函数的声明
        // ×××
        int& operator[](int h);
        void print(); 
    private:
        int *p;
        int size;
};

#endif
 1 #include "arrayInt.h"
 2 #include <iostream>
 3 #include <cstdlib>
 4 using std::cout;
 5 using std::endl;
 6 
 7 ArrayInt::ArrayInt(int n, int value): size(n) {
 8     p = new int[size];
 9     
10     if (p == nullptr) {
11         cout << "fail to mallocate memory" << endl;
12         exit(0); 
13     } 
14     
15     for(int i=0; i<size; i++)
16         p[i] = value;
17 }
18 
19 ArrayInt::~ArrayInt() {
20     delete[] p;
21 }
22 
23 void ArrayInt::print() {
24     for(int i=0; i<size; i++)
25         cout << p[i] << " ";
26     cout << endl;
27 }
28 
29 // 补足:将运算符[]重载为成员函数的实现
30 // ×××
31 int& ArrayInt:: operator[](int h)
32 {
33 
34     return p[h];
35 }

总结:

1、第一题在课上做的时候,不知道为什么头文件打不开。

2、课后完成第一题时,默认形参值漏给形参赋初始值,导致错误。

3、operator的用法还需要巩固,还需要对重载进一步了解。

https://www.cnblogs.com/wsggwsc/p/10758995.html

https://www.cnblogs.com/xuexinyu/p/10743640.html

 

实验4

标签:ref   log   函数   col   用法   形参   amp   http   car   

原文地址:https://www.cnblogs.com/0122Frank/p/10897000.html

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