C++ primer plus 学习笔记(第11章)

第11章 使用类
11.1 运算符重载
C++允许将运算符重载扩展到用户定义的类型中。在C++中,可以定义一个表示数组的类,并重载+运算符,就有如下语句:

evening=sam+janet;

要重载运算符,需要使用被称为运算符函数的特殊函数形式。运算符的格式如下:

operatorop(argument - list)

例,operator+( )重载+运算符,operator*( )重载*运算符。
代码示例:
头文件:

#ifndef MYTIME1_H_
#define MYTIME1_H_
class Time
{
	int hours;
	int minutes;
public:
	Time();
	Time(int h,int m=0);
	void AddMin(int m);
	void AddHr(int h);
	void Reset(int h=0,int m=0);
	Time operator+(const Time &t)const;
	void show()const;
};
#endif
mytime1.cpp
#include<iostream>
#include"mytime1.h"
Time::Time()
{
	hours=minutes=0;
}
Time::Time(int h,int m)
{
	hours=h;
	minutes=m;
}
void Time::AddMin(int m)
{
	minutes+=m;
	hours+=minutes/60;
	minutes%=60;
}
void Time::AddHr(int h)
{
	hours+=h;
}
void Time::Reset(int h,int m)
{
	hours=h;
	minutes=m;
}
Time Time::operator+(const Time &t)const
{
	Time sum;
	sum.minutes=minutes+t.minutes;
	sum.hours=hours+t.hours+sum.minutes/60;
	sum.minutes%=60;
	return sum;
}
void Time::show()const
{
	std::cout<<hours<<"hours, "<<minutes<<"minutes";
}
usetime1.cpp
#include<iostream>
#include"mytime1.h"
int main()
{
	using std::cout;
	using std::endl;
	Time planning;
	Time coding(5,55);
	Time fixing(5,55);
	Time total;
	cout<<"coding time=";
	coding.show();
	cout<<endl;
	cout<<"fixing time=";
	fixing.show();
	cout<<endl;
	total=coding+fixing;
    cout<<"total time=";
	total.show();
	cout<<endl;
	Time morefixing(3,28);
	total=morefixing.operator+(total);
    cout<<"new total time=";
	total.show();
	cout<<endl;
	return 0;
}

总之,operator+( )函数的名称使得可以使用函数表示法或运算符表示法来调用它。

11.2.2 重载限制
1)重载后的运算符必须至少有一个操作数是用户定义的类型。
2)使用运算符时不能违反运算符原来的句法规则
3)不能创建新运算符
4)不能重载下面的运算符:
sizeof:sizeof运算符
.:成员运算符
.*:成员指针运算符
:::作用域解析运算符
?::条件运算符
typeid:一个RTTI运算符

5)大多数其他的运算符都可以通过成员或非成员函数进行重载,但下面的运算符只能通过成员函数进行重载。
=:赋值运算符;
():函数调用运算符
[ ]:下标运算符
->:通过指针访问类成员的运算符

11.3 友元
C++控制对类对象私有部分的访问。通常,共有类方法提供唯一的访问途径。C++中提供了另外一种 形式的访问权限:友元。友元有三种:
友元函数;
友元类
友元成员函数

11.3.1 创建友元:
创建友元函数的第一步是将其原型放在类声明中,并在原型声明前加上关键字friend:
friend Time operator*(double m,const Time &t); //goes in class declaration
该原型意味着以下两点:
虽然operator*( )函数是在类声明中声明的,但它不是成员函数,因此不能使用成员运算符来调用;
虽然operator*( )函数不是成员函数,但它与成员函数的访问权限相同。
第二步是编写函数定义。它不是成员函数,所以不需要使用Time::限定符,另外也不需要使用关键字friend,定义应该如下:

Time operator*(double m,const Time &t)
{
  Time result;
  long totalminutes=t.hours*mult*60+t.minutes*mult;
  result.hours=totalminutes/60;
  result.minutes=totalminutes%60;
  return result;
}

总之,类的友元函数是非成员函数,其访问权限与成员函数相同。

11.3.2 常用的友元:重载<<运算符
1)<<的第一种重载版本
要使Time类知道使用cout,必须使用友元函数。
通过使用友元函数,可以像下面这样重载运算符:

void operator<<(ostream & os,const Time &t)
{
   os<<t.hours<<"hours. "<<t.minutes<<"minutes";
}

这样就可以使用下面的语句:
cout<<trip;
2) <<的第二种重载版本
可以对友元函数采用相同的方法。只要修改operator<<( )函数,让它返回ostream对象的引用即可:

ostream & operator<<(ostream & os,const Time & t)
{
   os<<t.hours<<"hours, "<<t.minutes<<"minutes";
   return os;
}

注意:返回类型是ostream&。这意味着该函数返回ostream对象的引用。
此时,下面的语句可以正常使用:
cout<<“Trip time: “<<trip<<”(TUSEDAY)\n”; //can do
提示:一般来说,要重载<<运算符来显示c_name的对象,可使用一个友元函数,其定义如下:

ostream & operator<<(ostream & os,const c_name & obj)
{
   os<<...;
   return os;
}

范例code:

// mytime3.h -- Time class with friends
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include <iostream>
class Time
{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h, int m = 0);
    void AddMin(int m);
    void AddHr(int h);
    void Reset(int h = 0, int m = 0);
    Time operator+(const Time & t) const;
    Time operator-(const Time & t) const;
    Time operator*(double n) const;
    friend Time operator*(double m, const Time & t)
        { return t * m; }   // inline definition
    friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif

// mytime3.cpp  -- implementing Time methods
#include "mytime3.h"
Time::Time()
{
    hours = minutes = 0;
}
Time::Time(int h, int m )
{
    hours = h;
    minutes = m;
}
void Time::AddMin(int m)
{
    minutes += m;
    hours += minutes / 60;
    minutes %= 60;
}
void Time::AddHr(int h)
{
    hours += h;
}
void Time::Reset(int h, int m)
{
    hours = h;
    minutes = m;
}
Time Time::operator+(const Time & t) const
{
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}
Time Time::operator-(const Time & t) const
{
    Time diff;
    int tot1, tot2;
    tot1 = t.minutes + 60 * t.hours;
    tot2 = minutes + 60 * hours;
    diff.minutes = (tot2 - tot1) % 60;
    diff.hours = (tot2 - tot1) / 60;
    return diff;
}
Time Time::operator*(double mult) const
{
    Time result;
    long totalminutes = hours * mult * 60 + minutes * mult;
    result.hours = totalminutes / 60;
    result.minutes = totalminutes % 60;
    return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
    os << t.hours << " hours, " << t.minutes << " minutes";
    return os; 
}

//usetime3.cpp -- using the fourth draft of the Time class
// compile usetime3.cpp and mytime3.cpp together
#include <iostream>
#include "mytime3.h"
int main()
{
    using std::cout;
    using std::endl;
    Time aida(3, 35);
    Time tosca(2, 48);
    Time temp;
    cout << "Aida and Tosca:\n";
    cout << aida<<"; " << tosca << endl;
    temp = aida + tosca;     // operator+()
    cout << "Aida + Tosca: " << temp << endl;
    temp = aida* 1.17;  // member operator*()
    cout << "Aida * 1.17: " << temp << endl;
    cout << "10.0 * Tosca: " << 10.0 * tosca << endl;
	// std::cin.get();
    return 0; 
}

C语言-光伏MPPT算法:电导增量法扰动观察法+自动全局搜索Plecs最大功率跟踪算法仿真内容概要:本文档主要介绍了一种基于C语言实现的光伏最大功率点跟踪(MPPT)算法,结合电导增量法与扰动观察法,并引入自动全局搜索策略,利用Plecs仿真工具对算法进行建模与仿真验证。文档重点阐述了两种经典MPPT算法的原理、优缺点及其在不同光照和温度条件下的动态响应特性,同时提出一种改进的复合控制策略以提升系统在复杂环境下的跟踪精度与稳定性。通过仿真结果对比分析,验证了所提方法在快速性和准确性方面的优势,适用于光伏发电系统的高效能量转换控制。; 适合人群:具备一定C语言编程基础和电力电子知识背景,从事光伏系统开发、嵌入式控制或新能源技术研发的工程师及高校研究人员;工作年限1-3年的初级至中级研发人员尤为适合。; 使用场景及目标:①掌握电导增量法与扰动观察法在实际光伏系统中的实现机制与切换逻辑;②学习如何在Plecs中搭建MPPT控制系统仿真模型;③实现自动全局搜索以避免传统算法陷入局部峰值问题,提升复杂工况下的最大功率追踪效率;④为光伏逆变器或太阳能充电控制器的算法开发提供技术参考与实现范例。; 阅读建议:建议读者结合文中提供的C语言算法逻辑与Plecs仿真模型同步学习,重点关注算法判断条件、步长调节策略及仿真参数设置。在理解基本原理的基础上,可通过修改光照强度、温度变化曲线等外部扰动因素,进一步测试算法鲁棒性,并尝试将其移植到实际嵌入式平台进行实验验证。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值