实验四 类的继承、派生和多态(1)

一、实验内容

1、车辆基本信息管理

基于Car类派生出ElectricCar类、派生类ElectricCar中新增数据成员Battery类对象。

#ifndef BATTERY_H
#define BATTERY_H

class Battery {
public:
    Battery(int batterySize0=70);
    int getSize();
private:
    int batterySize;
};

#endif

battery.h

#ifndef CAR_H
#define CAR_H

#include<string>
#include<iostream>
using std::string;
using std::ostream;

class Car {
public:
    Car(string maker0, string model0, int year0, int odometer0=0);
    void updateOdometer(int odometerN);
    friend ostream & operator<<(ostream &out, const Car &c);
private:
    string maker;
    string model;
    int year;
    int odometer;
};

#endif

car.h

#ifndef ELECTRIC_H
#define ELECTRIC_H

#include"battery.h"
#include"car.h"

class ElectricCar:virtual public Car{
public:
    ElectricCar(string maker0,string model0,int year0,int odometer0=0);
    friend ostream & operator<<(ostream &out, const ElectricCar &c);
private:
    Battery battery;
};

#endif

electriccar.h

#include"battery.h"
#include<iostream>

using std::cout;
using std::endl;

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

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

battery.cpp

#include"car.h"
#include<iostream>

using std::cout;
using std::endl;

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

void Car::updateOdometer(int odometerN) {
    if (odometerN < odometer) {
        cout << "WRONG DATA!";
        exit(0);
    }
    else odometer = odometerN;
}
ostream & operator<<(ostream &out, const Car &c) {
    cout << "maker:" << c.maker << endl;
    cout << "model:" << c.model << endl;
    cout << "year:" << c.year << endl;
    cout << "odometer:" << c.odometer << endl;
    return out;
}

car.cpp

#include"electriccar.h"
#include<iostream>

using std::cout;
using std::endl;

ElectricCar::ElectricCar(string maker0, string model0, int year0, int odometer0 ):Car(maker0,model0,year0,odometer0) {
}

ostream & operator<<(ostream &out, const ElectricCar &c) {
    Car a(c);
    cout<<a<<endl;
    Battery m=c.battery;
    cout << "batterySize:" <<m.getSize()<<"-kWh"<< endl;
    return out;
}

electriccar.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;
}

main.cpp

效果如下:

2、补足程序

重载运算符[]为一维动态整形数组类ArrayInt的成员函数,使得通过动态整形数组对象名和下标可以 访问对象中具体元素。

#ifndef ARRAY_INT_H
#define ARRAY_INT_H

class ArrayInt{
    public:
        ArrayInt(int n, int value=0);
        ~ArrayInt();
        // 补足:将运算符[]重载为成员函数的声明
        // ×××
        int& operator[](int i);
        void print();
    private:
        int *p;
        int size;
};

#endif

arrayInt.h

#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 == nullptr) {
        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 i){
    return p[i];
}

arrayInt.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;
}

main.cpp

效果如下:

二、实验小结

虽然测试出结果,有几点还是不太懂:

  • electriccar.cpp中,Car a(c); 我将ElectricCar类c赋值给Car类a,这是反复试探的结果,虽然意外成功,还是不太理解;
  • 什么时候用&也不是很清楚。不对了,就加上&试试,偶尔可以猜对,却不是理解后的结果。

希望大佬不吝赐教,万分感激。

原文地址:https://www.cnblogs.com/zuiyankh/p/10884276.html

时间: 2024-08-14 15:36:33

实验四 类的继承、派生和多态(1)的相关文章

c++实验四 类的继承、派生和多态

实验目的 1. 理解类的继承和派生机制 2. 掌握派生类的定义和使用 3. 理解和掌握派生类成员的标识和访问中同名覆盖原则.二元作用域分辨符和虚基类的用法 4. 掌握派生类构造函数和析构函数的定义及调用次序 5. 理解运算符重载的目的,掌握运算符重载函数的编写方法 实验内容 一.ElectricCar类 #ifndef BATTERY_H #define BATTERY_H class Battery { public: Battery(int nb = 70); int showbattery

实验五--类的继承派生多态二次练习

实验目的 1. 理解运行时多态 2. 掌握虚函数和抽象类 实验准备 1. 概念 多态,运行时多态 虚函数, 纯虚函数, 抽象类 2. 语法和应用 虚函数的定义和使用纯虚函数.抽象类的定义和使用 实验内容 设计并实现一个机器宠物类MachinePets. 每个机器宠物有如下信息:昵称(nickname) 每个机器宠物有如下成员函数: 带参数的构造函数MachinePets(const string s) ,为机器宠物初始化昵称. 纯虚函数string talk()为机器宠物派生类提供宠物叫声的统一

c++ 类的继承派生

作者:张煜 这周上的c++课上老师讲了类的继承和派生,总觉得他讲的相当的不完整. 类的继承是利用已有的类来建立专用类的编程技术.从另一个角度来说,从已有的类(父类)产生一个新的子类,称为类的派生. 继承是面向程序设计中最重要的机制,它支持层次分类的观点.继承使得程序员可以在一个较一般的类的基础上很快地建立一个新类,而不必从零开始设计每个类,在现实世界中,许多实体或概念不是孤立的,它们具有共同的特征,但也有细微的差别,人们使用层次分类的方法来描述这些实体或概念之间的相似点和不同点.如下图:    

实验4 类的继承、派生和多态(1)

实验目的 1. 理解类的继承和派生机制 2. 掌握派生类的定义和使用 3. 理解和掌握派生类成员的标识和访问中同名覆盖原则.二元作用域分辨符和虚基类的用法 4. 掌握派生类构造函数和析构函数的定义及调用次序 5. 理解运算符重载的目的,掌握运算符重载函数的编写方法 实验准备 1. 类的继承和派生 引入继承和派生机制的目的 基本概念:继承.派生.基类.直接基类.间接基类 .派生类 语法 派生类定义的语法格式(单重继承和多重继承) 派生类构造函数及其初始化列表书写形式 派生类成员的标识与访问 同名覆

实验五 类的继承、派生和多态(2)

一.验证性实验 1. 在多层继承中,派生类中出现与基类同名成员时,通过对象名.成员名的方式,访问的成员什么? 对象名.成员名:派生类对象 2. 通过基类指针访问派生类对象时,基类中成员函数有无关键字virtual,访问的成员分别是什么? 基类指针:有关键字时访问派生类,无关键字访问基类对象 二.编程练习 1.设计并实现一个机器宠物类MachinePets #include<iostream> #include<string> using namespace std; class M

PHP类(四)-类的继承

类的继承就是从已经定义的类中继承数据,也可以重新定义或者加入一些数据. 被继承的类称为父类,基类,超类 继承的类称为子类,派生类 在PHP中只能使用单继承,也就是一个类只能从一个类中继承数据,但是一个类可以有多个子类 <?php class Person{ var $name; var $age; var $sex; function __construct($name="Alex",$age=12,$sex="Male"){ $this->name =

实验6 类的继承和多态

四.实验结论 1.实验内容1 Base.h #ifndef Base_h #define Base_h using namespace std; class Base { public: Base (int m0 ,int n0):m(m0),n(n0){} int add (){return m+n;}; int getm() const {return m;}; int getn() const {return n;}; private: int m, n; }; #endif /*Base

第十周项目四 类的继承教师兼干部类

/* *Copyright(c) 2016,烟台大学计算机学院 *作 者:刘金石 *完成日期:2016年5月9日 *问题描述:教师兼干部类 *要求:分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部).要求: *(1)在两个基类中都包含姓名.年龄.性别.地址.电话等数据成员. *(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中

python 类 四 : 类的继承与覆盖父类方法

python 继承,子类可以覆盖父类的各种方法,包括__init__方法. 如果要覆盖父类的__init__方法,且希望在覆盖的方法中引用父类的__init__方法,比如在父类的__init__方法的基础上增加一些属性的设置或者其他. 则需要显示的引用父类的方法,否则,父类的方法不会被直接调用到的. 比如: #coding:utf-8 # c继承了p,并实现了自己的__init__方法,也就是重新实现了__init__方法,但为了减少代码冗余 # c在自己的__init__方法里面引用了p的__