标签:include == 声明 put int() cout nbsp 图片 alt
1.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;
}
car.h
#ifndef CAR_H
#define CAR_H
#include <cstring>
#include <iostream>
using namespace std;
class Car
{
public:
Car(string maker1, string model1, int year1, int odometer1 = 0);
void updateOdometer(int update);
string showMaker();
string showModel();
int showYear();
int showOdometer();
friend ostream & operator<<(ostream &output, const Car &car1);
private:
string maker;
string model;
int year;
int odometer;
};
#endif // CAR_H
car.cpp
#include "car.h"
#include <cstring>
#include <iostream>
using namespace std;
Car::Car(string maker1, string model1, int year1, int odometer1) :
maker(maker1), model(model1), year(year1), odometer(odometer1)
{
}
void Car::updateOdometer(int update)
{
if (update < odometer)
cout << "wrong updateodometer" << endl;
else
{
odometer = update;
}
}
string Car::showMaker()
{
return maker;
}
string Car::showModel()
{
return model;
}
int Car::showYear()
{
return year;
}
int Car::showOdometer()
{
return odometer;
}
ostream &operator<<(ostream &output, const Car &car1)
{
output << "maker" << ":" << car1.maker << endl
<< "model" << ":" << car1.model << endl
<< "year" << ":" << car1.year << endl
<< "odometer" << ":" << car1.odometer << endl;
return output;
}
battery.h
#ifndef BATTERY_H
#define BATTERY_H
class Battery
{
public:
Battery(int p = 70) :batterySize(p) {}
int showbatterySize();
private:
int batterySize;
};
#endif // BATTERY_H
battery.cpp
#include "battery.h"
int Battery::showbatterySize()
{
return batterySize;
}
electricCar.h
#ifndef ELECTRICCAR_H
#define ELECTRICCAR_H
#include <iostream>
#include "car.h"
#include "battery.h"
using namespace std;
class electricCar :public Car, public Battery
{
public:
electricCar(string maker1, string model1, int year1, int odometer1 = 0);
friend ostream & operator<<(ostream &output, electricCar &electriccar1);
private:
Battery battery;
};
#endif#pragma once
electricCar.cpp
#include "electricCar.h"
#include <cstring>
#include <iostream>
using namespace std;
electricCar::electricCar(string maker1, string model1, int year1, int odometer1) :
Car(maker1, model1, year1, odometer1), battery(70)
{
}
ostream &operator<<(ostream &output, electricCar &electriccar1)
{
output << "maker" << ":" << electriccar1.showMaker() << endl
<< "model" << ":" << electriccar1.showModel() << endl
<< "year" << ":" << electriccar1.showYear() << endl
<< "odometer" << ":" << electriccar1.showOdometer() << endl
<< "batterySize" << ":" << electriccar1.showbatterySize() << "-kWh" << endl;
return output;
}

2.arraylnt类
arrayint.h
#ifndef ARRAY_INT_H
#define ARRAY_INT_H
class ArrayInt {
public:
ArrayInt(int n, int value = 0);
~ArrayInt();
// 补足:将运算符[]重载为成员函数的声明
// ×××
int &operator[](int point);
void print();
private:
int *p;
int size;
};
#endif
arraylnt.cpp
#include "arrayInt.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
using std::cout;
using std::endl;
ArrayInt::ArrayInt(int n, int value) : size(n) {
p = new int[size];
if (p == NULL) {
cout << "fail to mallocate memory" << endl;
exit(0);
}
for (int i = 0; i < size; i++)
p[i] = value;
}
electriccar1.showbatterySize()
ArrayInt::~ArrayInt() {
delete[] p;
}
void ArrayInt::print() {
for (int i = 0; i < size; i++)
cout << p[i] << " ";
cout << endl;
}
// 补足:将运算符[]重载为成员函数的实现
// ×××
int &ArrayInt::operator[](int point)
{
assert(point >= 0 && point < size);
return p[point];
}
main
#include <iostream>
#include <stdlib.h>
using namespace std;
#include "arrayInt.h"
int main() {
// 定义动态整型数组对象a,包含2个元素,初始值为0
ArrayInt a(3);
a.print();
// 定义动态整型数组对象b,包含3个元素,初始值为6
ArrayInt b(5, 4);
b.print();
// 通过对象名和下标方式访问并修改对象元素
b[3] = 5;
cout << b[3] << endl;
b.print();
system("pause");
return 0;
}

注意,实验1在vs系统运行时要加上include<string.h>
标签:include == 声明 put int() cout nbsp 图片 alt
原文地址:https://www.cnblogs.com/ggwdcs/p/10894790.html