码迷,mamicode.com
首页 > 其他好文 > 详细

实验四

时间:2019-05-20 16:46:34      阅读:109      评论:0      收藏:0      [点我收藏+]

标签: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

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