码迷,mamicode.com
首页 > 编程语言 > 详细

c++第四次实验

时间:2019-05-12 19:38:15      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:注意   cstring   第四次   std   成员   opera   define   引用类型   sla   

1.车辆基本信息管理

battery.h

 1 #ifndef BATTERY_H
 2 #define BATTERY_H
 3 class Battery
 4 {
 5 public:
 6     Battery(int p=70):batterySize(p){}
 7     int showbatterySize();
 8 private:
 9     int batterySize;
10 };
11 #endif // BATTERY_H

battery.cpp

1 #include "battery.h"
2 
3 int Battery::showbatterySize()
4 {
5     return batterySize;
6 }

car.h

 1 #ifndef CAR_H
 2 #define CAR_H
 3 #include <cstring>
 4 #include <iostream>
 5 using namespace std;
 6 class Car
 7 {
 8 public:
 9     Car (string maker1,string model1,int year1,int odometer1=0);
10     void updateOdometer(int update);
11     string showMaker();
12     string showModel();
13     int showYear();
14     int showOdometer();
15     friend ostream & operator<<(ostream &output,const Car &car1);
16 private:
17     string maker;
18     string model;
19     int year;
20     int odometer;
21 };
22 #endif // CAR_H

car.cpp

 1 #include "car.h"
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 
 6 Car::Car(string maker1,string model1,int year1,int odometer1):
 7     maker(maker1),model(model1),year(year1),odometer(odometer1)
 8 {
 9 
10 }
11 void Car::updateOdometer(int update)
12 {
13     if(update<odometer)
14         cout<<"wrong updateodometer"<<endl;
15     else
16     {
17         odometer=update;
18     }
19 }
20 string Car::showMaker()
21 {
22     return maker;
23 }
24 string Car::showModel()
25 {
26     return model;
27 }
28 int Car::showYear()
29 {
30     return year;
31 }
32 int Car::showOdometer()
33 {
34     return odometer;
35 }
36 ostream &operator<<(ostream &output,const Car &car1)
37 {
38     output<<"maker"<<":"<<car1.maker<<endl
39     <<"model"<<":"<<car1.model<<endl
40     <<"year"<<":"<<car1.year<<endl
41     <<"odometer"<<":"<<car1.odometer<<endl;
42     return output;
43 }

electricCar.h

 1 #ifndef ELECTRICCAR_H
 2 #define ELECTRICCAR_H
 3 #include <iostream>
 4 #include "car.h"
 5 #include "battery.h"
 6 using namespace std;
 7 class electricCar:public Car,public Battery
 8 {
 9 public:
10     electricCar(string maker1,string model1,int year1,int odometer1=0);
11     friend ostream & operator<<(ostream &output,electricCar &electriccar1);
12 private:
13     Battery battery;
14 
15 };
16 #endif

electricCar.cpp

 1 #include "electricCar.h"
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 electricCar::electricCar(string maker1,string model1,int year1,int odometer1):
 6    Car(maker1,model1,year1,odometer1),battery(70)
 7 {
 8 
 9 }
10 ostream &operator<<(ostream &output,electricCar &electriccar1)
11 {
12      output<<"maker"<<":"<<electriccar1.showMaker()<<endl
13     <<"model"<<":"<<electriccar1.showModel()<<endl
14     <<"year"<<":"<<electriccar1.showYear()<<endl
15     <<"odometer"<<":"<<electriccar1.showOdometer()<<endl
16     <<"batterySize"<<":"<<electriccar1.showbatterySize()<<"-kWh"<<endl;
17     return output;
18 }

main.cpp

 1 #include <iostream>
 2 #include <stdlib.h>
 3 using namespace std;
 4 
 5 #include "car.h"
 6 #include "electricCar.h"
 7 
 8 int main() {
 9     // 测试Car类
10     Car oldcar("Cheng nuo","knight4",2019);
11     cout << "--------oldcar‘s info--------" << endl;
12     oldcar.updateOdometer(25000);
13     cout << oldcar << endl;
14 
15     // 测试ElectricCar类
16     electricCar newcar("Tesla","model s",2016);
17     newcar.updateOdometer(2500);
18     cout << "\n--------newcar‘s info--------\n";
19     cout << newcar << endl;
20     newcar.updateOdometer(1500);
21     cout << "\n--------newcar‘s info--------\n";
22     cout << newcar << endl;
23 
24     system("pause");
25 
26     return 0;
27 }

技术图片

更改了一些信息,并且在里程数小于之前时会报错

2.补足程序

arrayInt.h

 1 #ifndef ARRAY_INT_H
 2 #define ARRAY_INT_H
 3 
 4 class ArrayInt{
 5     public:
 6         ArrayInt(int n, int value=0);
 7         ~ArrayInt();
 8         // 补足:将运算符[]重载为成员函数的声明
 9         // ×××
10         int &operator[](int point);
11         void print();
12     private:
13         int *p;
14         int size;
15 };
16 
17 #endif

arrayInt.cpp

 1 #ifndef ARRAY_INT_H
 2 #define ARRAY_INT_H
 3 
 4 class ArrayInt{
 5     public:
 6         ArrayInt(int n, int value=0);
 7         ~ArrayInt();
 8         // 补足:将运算符[]重载为成员函数的声明
 9         // ×××
10         int &operator[](int point);
11         void print();
12     private:
13         int *p;
14         int size;
15 };
16 
17 #endif

main.cpp

 1 #include <iostream>
 2 #include <stdlib.h>
 3 using namespace std;
 4 
 5 #include "arrayInt.h"
 6 
 7 int main() {
 8     // 定义动态整型数组对象a,包含2个元素,初始值为0
 9     ArrayInt a(3);
10     a.print();
11 
12     // 定义动态整型数组对象b,包含3个元素,初始值为6
13     ArrayInt b(5, 4);
14     b.print();
15 
16     // 通过对象名和下标方式访问并修改对象元素
17     b[3] = 5;
18     cout << b[3] << endl;
19     b.print();
20 
21     system("pause");
22 
23     return 0;
24 }

技术图片

对main.cpp做了一些修改

 

实验总结:

1.派生类的构造函数对派生类进行初始化时,要先将基类初始化,然后对成员对象初始化

2.重载<<运算符时,要注意引用符号的使用,并且要返回输出流对象

3.由于继承后的派生类在打印时无法调用基类的私有成员,所以要在基类中设置接口,使得能够在派生类中输出基类中私有成员的值

4.一个问题,友元函数operator<<中使用electriccar1.battery.showbatterySize()时,编译器显示battery为私有成员无法调用,但是友元函数不是可以访问私有成员?

5.有关<<运算符重载时要注意头文件iostream和using namespace std;要写全

6.system("pause")要在前面加上#include<stdlib.h>

7.运算符[]重载时,返回值必须为引用类型,因为它作为左值不能为常量

c++第四次实验

标签:注意   cstring   第四次   std   成员   opera   define   引用类型   sla   

原文地址:https://www.cnblogs.com/knight04/p/10853219.html

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