首页 > 其他 > 详细

实验四

时间:2019-05-21 00:44:41      阅读:38      评论:0      收藏:0      [点我收藏+]

标签:delet   name   info   技术分享   let   成员   tor   nbsp   model   

#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;
}
#ifndef BATTERY_H
#define BATTERY_H

class Battery
{
    public:
        Battery(int batterSize0=70);
        int getbatterysize();
       
    private:
        int batterySize;    
};

#endif
#include"Battery.h"

Battery::Battery(int batterySize0):batterySize(batterySize0)
{}

int Battery::getbatterysize()
{
    return batterySize;
}

Battery::~Battery()
{}
#ifndef CAR_H
#define CAR_H
#include<iostream>
using std::string;
using std::ostream;
class Car
{
    public:
        Car(string maker0,string model0,int year0);
        friend ostream &operator<<(ostream &out,const Car &c);
        void updateOdometer(int updatemeter);
        const string getmaker() ;
        const string getmodel() ;
        const int getyear() ;
        const int getodometer() ;
    private:
        string maker;
        string model;
        int year;
        int odometer;
};

#endif
#include<iostream>
#include"Car.h"
using namespace std;

Car::Car(string maker0,string model0,int year0):maker(maker0),model(model0),year(year0)
{
    odometer=0;
}

ostream &operator<<(ostream &out,const Car &c)
{
    out<<"maker:\t\t"<<c.maker<<endl
       <<"model:\t\t"<<c.model<<endl
       <<"year:\t\t"<<c.year<<endl
       <<"odometer:\t"<<c.odometer<<endl;
    return out;            
}

void Car::updateOdometer(int updatemeter)
{
    if(updatemeter<odometer)
    {
        cout<<"Warning:Wrong!"<<endl;
    }
    else odometer=updatemeter;
}

string Car::getmaker() const
{
    return maker;
}

string Car::getmodel() const
{
    return model;
}

int Car::getyear() const
{
    return year;
}

int Car::getodometer() const
{
    return odometer;
}

Car::~Car()
{ }
#ifndef ElectricCar_H
#define ElectricCar_H
#include<iostream>
#include"Car.h"
#include"Battery.h"
using std::string;
using std::ostream;

class ElectricCar:public Car
{
    public:
        ElectricCar(string maker0,string model0,int year0);
        friend ostream &operator<<(ostream &out,const ElectricCar &e);
        ~ElectricCar();
    private:
        Battery battery;
        int batterySize;
};

#endif
#include"ElectricCar.h"
#include"Car.h"
#include"Battery.h"
using namespace std;

ElectricCar::ElectricCar(string maker0,string model0,int year0):Car(maker0,model0,year0)
{
    batterySize=battery.getbatterysize();
}

ostream &operator<<(ostream &out,const ElectricCar &e)
{
    out<<"maker"<<":"<<e.getmaker()<<endl
       <<"model"<<":"<<e.getmodel()<<endl
       <<"year"<<":"<<e.getyear()<<endl
       <<"odometer"<<":"<<e.getodometer()<<endl
       <<"batterySize"<<":"<<e.batterySize<<"-kWh"<<endl;
    return out;
}

ElectricCar::~ElectricCar()
{ }


技术分享图片




#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];
    
}// 补足:将运算符[]重载为成员函数的实现

技术分享图片

1.第一个程序写得挺不容易的,一开始错了很多地方,一直编译错误,也不知道错在哪里,后来在同学的帮助下修改正确,这个程序里运算符重载用了友元函数,其他比较值得注意的地方就是使用了输出流。

2.第二个程序总体来说不难,主要知识点如下:运算符重载为成员函数一般语法形式为:返回值类型 operator 运算符(形参表){函数体}。

运算符重载为非成员函数一般语法形式为:返回值类型 operator 运算符(形参表){函数体}。




 

实验四

标签:delet   name   info   技术分享   let   成员   tor   nbsp   model   

原文:https://www.cnblogs.com/agsjg/p/10897517.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号