NS2中energy.h与energy.cc代码分析。


前言

对mit/rca目录下energy.h与energy.cc文件实现的功能做一个简单记录,以防之后使用


一、energy.h

 
头文件提供类的声明,使得类和其成员函数在其他文件中可见.

#ifndef energy_resource_h
#define energy_resource_h

#include <mit/rca/resource.h>

class EnergyResource ;

class EnergyResource : public Resource {
   
 private:
  int command(int argc, const char*const* argv);
  char *resulttostring(int result);
  double energy_level_;
  double alarm_level_;
  double expended_;

 public:
  EnergyResource();
  void add(double amount);
  int remove(double amount);
  int acquire(double amount);
  double query() {
    return energy_level_;}

};

#endif

上述代码定义了 EnergyResource 类,并声明它继承自 Resource 类。
定义私有成员:

  1. command(int argc, const charconst argv):处理命令的成员函数。
  2. resulttostring(int result):将结果转换为字符串的成员函数。
  3. energy_level_:当前能量水平。
  4. alarm_level_:报警能量水平。
  5. expended_:已消耗的能量。

定义共有成员:

  1. EnergyResource():构造函数,用于初始化类的实例。
  2. void add(double amount):增加指定数量的能量。
  3. int remove(double amount):移除指定数量的能量,并返回操作结果。
  4. int acquire(double amount):获取指定数量的能量,并返回操作结果。
  5. double query():查询当前的能量水平,返回 energy_level_。

二、energy.cc

 
对上述定义函数的具体实现。


#include <stdio.h>
#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值