目录

  • 1 C++与面向对象程序设计概述
    • 1.1 C++到底有没有必要学
    • 1.2 面向过程PK面向对象
    • 1.3 C++的历史
    • 1.4 面向对象编程方法
    • 1.5 第一个C++程序
    • 1.6 浮点数精度控制
    • 1.7 辅导答疑
    • 1.8 直播课
    • 1.9 辅导答疑2_部分实验题参考代码
  • 2 C++程序设计基础
    • 2.1 2.1 基本结构
    • 2.2 2.2数组 2.3 string类
    • 2.3 2.4 函数
    • 2.4 2.5 指针与引用
    • 2.5 2.6 结构体与共用体
    • 2.6 辅导答疑——实验题讲解视频
    • 2.7 辅导答疑——实验2中存在的问题
  • 3 类和对象
    • 3.1 面向对象程序设计基本特点
    • 3.2 类的定义和对象
    • 3.3 类的构造函数
    • 3.4 类的析构函数
    • 3.5 标识符的作用域与可见性
    • 3.6 常对象
    • 3.7 辅导答疑——封装的好处
    • 3.8 辅导答疑——实验题讲解视频和参考代码
    • 3.9 辅导答疑——实验3存在问题
    • 3.10 实验题:2533:设计Rectangle类
    • 3.11 辅导答疑——实验题:第几天
    • 3.12 辅导答疑——部分实验题代码
  • 4 第4章 类的知识进阶
    • 4.1 静态数据成员
    • 4.2 友元
    • 4.3 运算符重载
    • 4.4 辅导答疑1——构造函数和析构函数对比分析
    • 4.5 辅导答疑2——实验题讲解视频
    • 4.6 辅导答疑3——矩阵求和
    • 4.7 辅导答疑4——时间排序(运算符重载)
    • 4.8 优秀作业赏析
  • 5 第5章 继承和派生
    • 5.1 类的继承和派生(1)
    • 5.2 类的继承和派生(2)
    • 5.3 类的继承和派生(3)
    • 5.4 类的继承和派生(4)
    • 5.5 辅导答疑
    • 5.6 实验题参考代码
  • 6 多态性
    • 6.1 虚函数
    • 6.2 辅导答疑1_实验题讲解
    • 6.3 辅导答疑2_神仙打架
  • 7 模板和异常
    • 7.1 异常处理
    • 7.2 辅导答疑
    • 7.3 辅导答疑:类模板和函数模板综合应用
实验题参考代码

#include<iostream>

#include<string>

using namespace std;

class Vehicle{

protected:

int maxspeed,weight;

public:

Vehicle(int m=0,int w=0):maxspeed(m),weight(w){

}

void Running();

void Stop();

void Input();

void Show();

}; 

void Vehicle::Show()

{

cout<<"maxspeed:"<<maxspeed<<endl;

cout<<"weight:"<<weight<<endl;

}

void Vehicle::Input()

{

cout<<"Input maxspeed:Input weight:";

cin>>maxspeed>>weight;

}

void Vehicle::Running()

{

cout<<"Vehicle is running maxspeed is:"<<maxspeed<<endl;

}

void Vehicle::Stop()

{

cout<<"Vehicle is stop."<<endl;

}

class Bicycle:public Vehicle{

private:

int high;//车高

public:

Bicycle(int m=0,int w=0,int h=0):Vehicle(m,w),high(h){

}

void Running();

void Stop();

void Input();

void Show();

};

void Bicycle::Running()

{

cout<<"Bicycle is running maxspeed is:"<<maxspeed<<endl;

}

void Bicycle::Stop()

{

cout<<"Bicycle is stop."<<endl;

}

void Bicycle::Input()

{

cout<<"Input maxspeed:Input weight:Input high:"<<endl;

cin>>maxspeed>>weight>>high;

}

void Bicycle::Show()

{

Vehicle::Show();

cout<<"high:"<<high<<endl;

}

class Car:public Vehicle{

private:

int seatNum;//座位数

public:

Car(int m=0,int w=0,int s=5):Vehicle(m,w),seatNum(s){

}

void Running();

void Stop();

void Input();

void Show();

};

void Car::Running()

{

cout<<"Car is running maxspeed is:"<<maxspeed<<endl;

}

void Car::Stop()

{

cout<<"Car is stop."<<endl;

}

void Car::Input()

{

cout<<"Input maxspeed:Input weight:Input SeatNum:"<<endl;

cin>>maxspeed>>weight>>seatNum;

}

void Car::Show()

{

Vehicle::Show();

cout<<"seatnum:"<<seatNum<<endl;

}

int main()

{

    Bicycle b1(25,30,16),b2;    

    b1.Show();

    cout<<"-----------"<<endl;

    b1.Running();

    cout<<"-----------"<<endl;

    

    b2.Input();  

    b2.Show();

    cout<<"-----------"<<endl;

    b2.Running();

    cout<<"-----------"<<endl;

    b2.Stop();

    cout<<"-----------"<<endl;

    

    Car c1;

    c1.Input();

    c1.Show();

    cout<<"-----------"<<endl;

    c1.Running();

    cout<<"-----------"<<endl;

    c1.Stop();

    cout<<"-----------"<<endl;

    return 0;

}