c++实验四
Posted csl-40
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++实验四相关的知识,希望对你有一定的参考价值。
battery.h:
#ifndef BATTERY_H #define BATTERY_H using namespace std; class Battery{ public: Battery(int a=70); int getbatterysize(); int batterysize; }; #endif
battery.cpp:
#include "battery.h" #include<iostream> using namespace std; Battery::Battery(int a):batterysize(a){ } int Battery::getbatterysize(){ return batterysize; }
car.h:
#ifndef CAR_H #define CAR_H #include<iostream> using std::string; using std::ostream; class Car{ public: Car(string a,string b,int c,int d=0); friend ostream&operator<<(ostream &out,Car &c1); void updateOdometer(int e); string maker,model; int year,odometer; }; #endif
car.cpp;
#include "car.h" #include<iostream> #include<string> using namespace std; Car::Car(string a,string b,int c,int d){ maker=a; model=b; year=c; odometer=d; } ostream &operator<<(ostream &out,Car &c1){ out<<"maker:"<<c1.maker<<endl <<"model:"<<c1.model<<endl <<"year:"<<c1.year<<endl <<"odometer:"<<c1.odometer<<endl; return out; } void Car::updateOdometer(int e){ if(e<odometer){ cout<<"updatemeter is wrong"<<endl; } else{ odometer=e; } }
electriccar.h:
#ifndef ELECTRICCAR_H #define ELECTRICCAR_H #include "battery.h" #include "car.h" class ElectricCar :public Car, public Battery { public: ElectricCar(string a, string b, int c, int d=0,int e=70); friend ostream&operator<<(ostream &out, ElectricCar &c1); private: Battery battery; }; #endif
electriccar.cpp:
#include "electricCar.h" #include "car.h" #include "battery.h" #include <iostream> using namespace std; ElectricCar::ElectricCar(string a, string b, int c, int d, int e) :Car(a, b, c, d),Battery(e){ } ostream &operator<<(ostream &out, ElectricCar &c1) { out << "maker:" << c1.maker << endl << "model:" << c1.model << endl << "year:" << c1.year << endl << "odometer:" << c1.odometer << endl << "batterysize:" << c1.batterysize <<"-kwh"<< endl; return out; }
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; }
运行截图:
arrayInt.h:
#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.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]; 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 x){ return p[x]; }
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; }
运行截图:
以上是关于c++实验四的主要内容,如果未能解决你的问题,请参考以下文章