实验4 继承

实验任务2

程序源码

 

#include <iostream>
#include <typeinfo>

// definitation of Graph
class Graph
{
public:
    void draw() { std::cout << "Graph::draw() : just as an interface\n"; }
};


// definition of Rectangle, derived from Graph
class Rectangle : public Graph
{
public:
    void draw() { std::cout << "Rectangle::draw(): programs of draw a rectangle\n"; }
};


// definition of Circle, derived from Graph
class Circle : public Graph
{
public:
    void draw() { std::cout << "Circle::draw(): programs of draw a circle\n"; }
};


// definitaion of fun(): as a call interface
void fun(Graph *ptr)
{
    std::cout << "pointer type: " << typeid(ptr).name() << "\n";
    std::cout << "RTTI type: " << typeid(*ptr).name() << "\n";
    ptr -> draw();
}

// test 
int main()
{
    Graph g1;
    Rectangle r1;
    Circle c1;

    // call by object name
    g1.draw();
    r1.draw();
    c1.draw();

    std::cout << "\n";

    // call by object name, and using the scope resolution operator::
    r1.Graph::draw();
    c1.Graph::draw();

    std::cout << "\n";

    // call by pointer to Base class
    fun(&g1);
    fun(&r1);
    fun(&c1);
}

 

运行结果截图

 

 

 

归纳总结

1.同名覆盖原则:派生类中同名函数覆盖掉基类同名函数。

2.二元作用域分辨符:如果想调用基类的同名函数,必须用类作用域符::来进行区分。

3.类型兼容原则:在需要基类对象的任何地方,都可以使用公有派生类的对象来替代。

代码微调后运行结果截图

 

 

 

与之前的不同

 ptr实际所指从基类对象变成派生类对象

实验任务3

Battery.hpp源代码

#ifndef BATTERY_HPP_INCLUDED
#define BATTERY_HPP_INCLUDED
#include <iostream>
#include<string>
using namespace std;
class Battery{
private:
    string capacity;
public:
    Battery(string a="70-KWh"):capacity(a){}
    string get_capacity(){return capacity;}
};
#endif // BATTERY_HPP_INCLUDED

Car.hpp源代码

#ifndef CAR_HPP_INCLUDED
#define CAR_HPP_INCLUDED
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class Car{
private:
    string maker;
    string model;
    int year;
    int odometers;
public:
    Car(string a,string b,int c):maker(a),model(b),year(c),odometers(0){}
    void info();
    void update_odometers(int a);
    string get_maker(){return maker;}
    string get_model(){return model;}
    int get_year(){return year;}
    int get_odometers(){return odometers;}
};
void Car::info(){
    cout<<left<<setw(20)<<"maker:"<<maker<<endl;
    cout<<left<<setw(20)<<"model:"<<model<<endl;
    cout<<left<<setw(20)<<"year:"<<year<<endl;
    cout<<left<<setw(20)<<"odometers:"<<odometers;
}
void Car::update_odometers(int a){
    if(a<odometers)
    cout<<"Invalid input.update odometer must exceed the last one.";
    else odometers=a;
}
#endif // CAR_HPP_INCLUDED

ElectricCar.hpp源代码

#ifndef ELECTRICCAR_HPP_INCLUDED
#define ELECTRICCAR_HPP_INCLUDED
#include<iostream>
#include<string>
#include<iomanip>
#include"Car.hpp"
using namespace std;
class ElectricCar:public Car
{
private:
    Battery battery;
public:
    ElectricCar(string a,string b,int c,string d="70-KWh"):Car(a,b,c),battery(d){}
    void info();
};
void ElectricCar::info(){
    cout<<left<<setw(20)<<"maker:"<<get_maker()<<endl;
    cout<<left<<setw(20)<<"model:"<<get_model()<<endl;
    cout<<left<<setw(20)<<"year:"<<get_year()<<endl;
    cout<<left<<setw(20)<<"odometers:"<<get_odometers()<<endl;
    cout<<left<<setw(20)<<"capacity:"<<battery.get_capacity();
}
#endif // ELECTRICCAR_HPP_INCLUDED

task3.cpp源代码

#include <iostream>
#include"Car.hpp"
#include"Battery.hpp"
#include"ElectricCar.hpp"
using namespace std;

int main()
{
    using namespace std;

    // test class of Car
    Car oldcar("WuLing", "light", 2008);
    cout << "--------oldcar's info--------" << endl;
    oldcar.update_odometers(6000);
    oldcar.info();

    cout << endl;

    // test class of ElectricCar
    ElectricCar newcar("Future", "hay", 2021);
    newcar.update_odometers(8999);
    cout << "\n--------newcar's info--------\n";
    newcar.info();
}

运行测试结果截图

 

 实验任务4

pets.hpp源代码

 

#include <iostream>
#include<string>
#include<conio.h>
#include<windows.h>
#include<mmsystem.h>
#pragma comment(lib,"Winmm.lib") using std::string; class MachinePets{ private: string nickname; public: MachinePets(const string a):nickname(a){} virtual void talk();string get_nickname(){return nickname;} };
void MachinePets::talk()
{
PlaySound(TEXT("宠物叫声.wav"),NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
getch():
}
class PetCats:public MachinePets { public: PetCats(const string a):MachinePets(a){} void talk(); };
void PetCats::talk()
{
PlaySound(TEXT("猫叫声.wav"),NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
getch();
}
class PetDogs:public MachinePets { public: PetDogs(const string a):MachinePets(a){} void talk(); };
void PetDogs::talk()
{
PlatSound(TEXT("狗叫声.wav"),NULL,SND_FILENAME|SND_ASYNC|SND_LOOP);
getch():
}

 

task4.cpp源代码

 

#include <iostream>
#include "pets.hpp"

void play(MachinePets *ptr)
{
    std::cout << ptr->get_nickname() << " says " ;
ptr->talk();
std::cout<< std::endl; } int main() { PetCats cat("round orange"); PetDogs dog("little black"); play(&cat); play(&dog); }

运行测试结果截图

 

 实验总结

在code::blocks编译器中只能打开.wav文件,用PlaySound()函数打开.wav文件,可以实现循环播放,用getch()实现结束播放。

posted @ 2021-11-23 22:10  bjfbfelkbuif  阅读(84)  评论(1)    收藏  举报