第四次实验
一car
main.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; }
battery.h
#ifndef BATTERY_H #define BATTERY_H class battery{ public: battery(int bs=70); int ret()const; private: int batterySize; }; #endif
battery.cpp
#include<iostream> #include"battery.h" using namespace std; battery::battery(int bs):batterySize(bs){ } int battery::ret()const{ return batterySize; }
car.h
#ifndef CAR_H #define CAR_H #include<string> #include<iostream> using namespace std; class car{ public: car(string mer,string mel,int yar); friend ostream &operator<<(ostream &out,const car &c); void updateOdometer(int a); private: string maker; string model; int year; int odometer; }; #endif
car.cpp
#include<iostream> #include<cstring> #include"car.h" car::car(string mer,string mel,int yar):maker(mer),model(mel),year(yar),odometer(0){ } ostream &operator<<(ostream &out,const car &c){ out<<"maker:"<<c.maker<<endl <<"model:"<<c.model<<endl <<"year:"<<c.year<<endl <<"odometer:"<<c.odometer<<endl; return out; } void car::updateOdometer(int a){ if(a>=odometer) odometer=a; else cout<<"警告,更新数据有误"<<endl; }
electriccar.h
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include"car.h" #include"battery.h" #include<iostream> #include<string> class ElectricCar:public car{ public: ElectricCar(string mer,string mel,int yar,int bs=70); friend ostream &operator<<(ostream &out,const ElectricCar &c); private: battery a; }; #endif
electriccar.cpp
#include<iostream> #include<string> #include"electricCar.h" ElectricCar::ElectricCar(string mer,string mel,int yar,int bs):car(mer,mel,yar),a(bs){ } ostream &operator<<(ostream &out,const ElectricCar &c){ car b=c; out<<b; out<<c.a.ret()<<endl; return out; }
在main改变了几个字母
二arraylnt
arraylnt.h
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); int &operator[](int i); // 补足:将运算符[]重载为成员函数的声明 // ××× void print(); private: int *p; int size; }; #endif
arraylnt.cpp
#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]; 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 i){ return p[i]; }
main.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; }