标签:date vat 访问 下标 c++ public amp stream int
#ifndef CAR_H #define CAR_H #include <iostream> #include <string> using namespace std; class Car{ public: Car(string maker0,string model0,int year0,int odometer0=0); friend ostream & operator<< (ostream &out,const Car &c); int updateOdometer(int newodometer); string getmaker(){return maker;} string getmodel(){return model;} int getyear(){return year;} int getodometer(){return odometer;} private: string maker; string model; int year; int odometer; }; #endif
#include "car.h" #include <iostream> #include <string> using namespace std; Car::Car(string maker0,string model0,int year0,int odometer0):maker(maker0),model(model0),year(year0),odometer(odometer0){ } ostream & operator<< (ostream &out,const Car &c){ out<<" maker:"<<c.maker<<endl; out<<"model:"<<c.model<<endl; out<<"year:"<<c.year<<endl; out<<"odometer:"<<c.odometer<<endl; return out; } int Car::updateOdometer(int newodometer){ if(newodometer<odometer) cout<<"error!"<<endl; else odometer=newodometer; return odometer; }
#ifndef BATTERY_H #define BATTERY_H class Battery{ public: Battery(int batterySize0=70); int getbatterySize(); friend class ElectricCar; private: int batterySize; }; #endif
#include "battery.h" Battery::Battery(int batterySize0):batterySize(batterySize0){ } int Battery::getbatterySize(){ return batterySize; }
#ifndef ELECTRICAR_H #define ELECTRICAR_H #include "battery.h" #include "car.h" #include <string> #include <iostream> using namespace std; class Battery; class ElectricCar:public Car{ public: ElectricCar(string maker0,string model0,int year0,int odometer0=0,int battery0=70); friend ostream & operator<< (ostream &out,ElectricCar &c1); friend class Battery; private: Battery b; int batterysize; }; #endif
#include "electricCar.h" using namespace std; ElectricCar::ElectricCar(string maker0,string model0,int year0,int odometer0,int battery0):Car(maker0,model0,year0,odometer0){ b.batterySize=battery0; batterysize=b.getbatterySize(); } ostream & operator<< (ostream &out,ElectricCar &c1){ out<<"maker:"<<c1.getmaker()<<endl; out<<"model:"<<c1.getmodel()<<endl; out<<"year:"<<c1.getyear()<<endl; out<<"odometer:"<<c1.getodometer()<<endl; out<<"batterySize:"<<c1.batterysize<<"-kWh"<<endl; return out; }
#include <iostream> using namespace std; #include "car.h" #include "electricCar.h" int main() { Car oldcar("Audi","a4",2016); cout << "--------oldcar‘s info--------" << endl; oldcar.updateOdometer(25000); cout << oldcar << endl; ElectricCar newcar("Tesla","model s",2016); newcar.updateOdometer(2500); cout << "\n--------newcar‘s info--------\n"; cout << newcar << endl; system("pause"); return 0; }
结果截图:
#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; }
#ifndef ARRAY_INT_H #define ARRAY_INT_H class ArrayInt{ public: ArrayInt(int n, int value=0); ~ArrayInt(); int& operator[](int n);// 补足:将运算符[]重载为成员函数的声明 void print(); private: int *p; int size; }; #endif
#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 n){ return p[n]; }// 补足:将运算符[]重载为成员函数的实现
结果截图:
标签:date vat 访问 下标 c++ public amp stream int
原文地址:https://www.cnblogs.com/cjj1/p/10899106.html