SRM Vision 暑期培训

by Leozheng @ SRMVision

§8 抽象接口/模板

#include <iostream>
using namespace std;
class Controller { // 抽象类
public:
    Controller(int id);
    virtual bool Initialize() = 0;
    // 初始化控制器
    virtual void Run() = 0; // 控制器运行
    virtual ~Controller();
protected:
    void ReceiveData(); // 接收数据
    void ProcessData();
    // 处理数据
    void SendData() const; // 发送数据
    int robot_id_;
    // 机器人编号
    double send_data_;
    // 控制器接受到的数据
    double* receive_data_;
    // 控制器发送的数据
};
Controller::Controller(int id) {
    cout << "调用的Controller的有参构造函数" << endl;
    robot_id_ = id;
    send_data_ = 0;
    receive_data_ = new double[4];
    for (int i = 0; i < 4; i++)
        receive_data_[i] = 0;
    cout << robot_id_ << "号机器人构造成功!" << endl;
}
void Controller::ProcessData() {
    cout << "加工数据中..." << endl;
    send_data_ = 0;
    for (int i = 0; i < 4; i++)
        send_data_ += receive_data_[i];
}
void Controller::ReceiveData() {
    cout << "输入接收的4个数据:" << endl;
    for (int i = 0; i < 4; i++)
        cin >> receive_data_[i];
}
void Controller::SendData() const {
    cout << "发送数据:" << send_data_ << endl;
}
Controller::~Controller() {
    cout << "调用Controller析构函数\n";
    delete[] receive_data_;
}
class InfantryController :public Controller { // 子类步兵控制器
public:
    InfantryController(int id);
    virtual bool Initialize(); // 重写虚函数
    virtual void Run();
    virtual ~InfantryController();
private:
    void DetectRune(); // 步兵特有的操作识别能量机关
    };
InfantryController::InfantryController(int id):Controller(id){
    cout << "调用了InfantryController的构造函数\n";
}
void InfantryController::Run() {
    ReceiveData();
    DetectRune();
    ProcessData();
    SendData();
}
int main() {
    InfantryController infantry(4);
    Run(&infantry);
    return 0;
}
template <class T, unsigned int BufSize>
class Buffer
{
public:
    void Push(const T& t) {
        tail_ = (tail_ + 1) % BufSize;
        if (head_ == tail_) ++head_;
        buf_[tail_] = t;
    }
    bool Pop(T& t) {
        if (head_ == tail_)
            return false;
        head_ = (head_ + 1) % BufSize;
        t = buf_[head_];
        return true;
    }
private:
    T buf_[BufSize];
    unsigned int head_ = 0, tail_ = 0;
};

int main(){
    Buffer<int,10> b;
    b.Push(10);
    b.Pop(10);
    return 0;
}

第八讲 - 作业:

#include <iostream>
using namespace std;
template <typename T> class Point
{
public:
    Point(const T &x=0, const T &y=0);
    Point(const Point<T> &p);
    ~Point();
    T & operator[](int index);
    template <typename TYPE> friend 
    ostream & operator<<(ostream &out, const Point<TYPE> &p);
protected:
    T _x, _y;
};

template <typename T>
Point<T>::Point(const T &x, const T &y) : _x(x), _y(y)
{
    cout << "构造对象 " << *this << endl;
}
template <typename T>
Point<T>::Point(const Point<T> &p) : _x(p._x), _y(p._y)
{
    cout << "拷贝构造对象 " << *this << endl;
}
template <typename T>
Point<T>::~Point()
{
    cout << "析构对象 " << *this << endl;
}
template <typename T>
T & Point<T>::operator[](int index)
{
    if(index==0) return _x;
    else return _y;
}
template <typename TYPE>
ostream & operator<<(ostream &out, const Point<TYPE> &p)
{
    out << "(" << p._x << ", " << p._y << ")";
    return out;
}
int main()
{	
    Point<int> a(-1, 2), b(a);
    Point<unsigned int> u(1, 2);
    Point<char> c('a', 'b');
    Point<double> d(3.5, 5.5);

    a[0] = 10;
    b[0] = 20;
    c[1] = 'B';
    d[1] = 0;
    u[0] = 2;
    return 0;
}

填空:

请写出程序输出内容

感 谢

@ SRMVision