友元函数、友元类 关键字--friend
友元函数:通过友元函数可以直接拿到 类里面定义的私有的变量
/**
* 定义Coordinate类
* 友元函数:display
* 数据成员:m_iX、m_iY
*/
class Coordinate
{
// 友元函数
friend void display(Coordinate &coor);
public:
Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
}
public:
int m_iX;
int m_iY;
};
/**
* display函数用于显示m_iX、m_iY的值
*/
void display(Coordinate &coor)
{
cout << "m_iX:" << coor.m_iX << endl;
cout << "m_iY:" << coor.m_iY << endl;
}
// 实例化Coordinate对象
Coordinate coor(6, 8);
// 调用display函数
display(coor);
通过友原函数 访问到Coordinate对象里面的所有变脸
/*
注意事项:
友元关系不可传递
友元关系的单向性
友元声明的形式和数量是不受限制的,可以在类中任意位置,不过一般都是放在最前面。
*/
//友元只是封装的补充
友元类:
//友元类:定义友元类后,可以直接通过对象访问这里面私有的函数和变量
//class Watch;
//friend class 类名 --friend class Watch
#include <iostream>
using namespace std;
class Watch;//告诉该类 只是临时用一下
/**
* 定义Time类
* 数据成员:m_iHour, m_iMinute,m_iSecond
* 成员函数:构造函数
* 友元类:Watch
*/
class Time
{
// 友元类
friend class Watch;
//告诉该类 只是临时用一下
public:Time(int hour, int min, int sec){m_iHour = hour;m_iMinute = min;m_iSecond = sec;}public:int m_iHour;int m_iMinute;int m_iSecond;};/** * 定义Watch类 * 数据成员:m_tTime * 成员函数:构造函数 * display用于显示时间 */class Watch{public:Watch(Time &t):m_tTime(t){}void display(){cout << m_tTime.m_iHour << endl;cout << m_tTime.m_iMinute << endl;cout << m_tTime.m_iSecond << endl;}public:Time m_tTime;};
static:静态---函数和变量都是用static来修饰
static 注意事项:
静态数据成员必须单独初始化
静态成员函数不能调用非静态成员函数和非静态数据成员
静态数据成员只有一份,并且不依赖对象而存在
运算符重载:关键字operator
#include <iostream>
using namespace std;
/**
* 定义Coordinate类
* 数据成员:m_iX,m_iY
* 成员函数:构造函数
* 重载--运算符,重载+运算符
*/
class Coordinate
{
public:
Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
}
// 前置--运算符重载
Coordinate &operator--()
{
m_iX--;
m_iY--;
return *this;
}
// 后置--运算符重载
Coordinate &operator--(int)
{
Coordinate old(*this);
this->m_iX--;
this->m_iY--;
return *this;
}
// +号运算符重载
Coordinate operator+( Coordinate c)
{
Coordinate coor4(1,2);
coor4.m_iX = this->m_iX+c.m_iX;
coor4.m_iY = this->m_iY+c.m_iY;
return coor4;
}
public:
int m_iX;
int m_iY;
};
int main(void)
{
Coordinate coor1(1, 3);
Coordinate coor2(2, 4);
Coordinate coor3(0, 0);
coor1--;
--coor2;
coor3 = coor1 + coor2;
cout << coor3.m_iX << endl;
cout << coor3.m_iY << endl;
return 0;
}
函数模版:(类似于java中的抽取公共方法)
关键字:template typename class
单个参数:
template <typename T>
void display(T t){}
多个参数:
template<typename T, class s>
void display(T t, S s){}
例:函数模版:
template <class T>
T max(T a,T b){
return (a>b)?a:b;
}
模版函数:
int max = max(100,99);
char ch = max<char>('a','b');
#include <iostream>
using namespace std;
/**
* 定义模板函数swapNum
* 实现功能:交换两个数的位置
*/
template <typename T>
void swapNam(T &a,T &b)
{
T temp = a;
a = b;
b = temp;
}
int main(void)
{
int x = 10;
int y = 20;
// 调用模板函数
swapNam(x,y);
cout << "x = " << x << endl;
cout << "y = " << y << endl;
return 0;
}
模版类和模版函数都是用template来修饰(在类的前面一行加template <typename T...>)
类中每个函数前面都需要加上template <typename T...>
#include <iostream>
using namespace std;
/**
* 定义一个矩形类模板Rect
* 成员函数:calcArea()、calePerimeter()
* 数据成员:m_length、m_height
*/
template <typename T>
class Rect
{
public:
Rect(T length,T height);
T calcArea();
T calePerimeter();
public:
T m_length;
T m_height;
};
/**
* 类属性赋值
*/
template <typename T>
Rect<T>::Rect(T length,T height)
{
m_length = length;
m_height = height;
}
/**
* 面积方法实现
*/
template <typename T>
T Rect<T>::calcArea()
{
return m_length * m_height;
}
/**
* 周长方法实现
*/
template <typename T>
T Rect<T>::calePerimeter()
{
return ( m_length + m_height) * 2;
}
int main(void)
{
Rect<int> rect(3, 6);
cout << rect.calcArea() << endl;
cout << rect.calePerimeter() << endl;
return 0;
}
标准模版库:STL(Standard Template Lib)
vector 向量:对数组的封装
特点:读数能在常量时间完成;
vector<T> v1;vector保存类型为T的对象,默认构造韩式v1为空;
vector<T> v2(v1);v2是v1的副本;
vector<T> v3(n,i);v3包含n个值为i的元素;
vector<T> v4(n);v4包含有值初始化元素的n个副本;
vector常用函数:
empty();//判断向量是否为空;
begin();//返回向量迭代器首元素;
end();//返回向量迭代器末元素的下一个元素;
clear();//清空向量;
front();//第一个数据;
back();//最后一个数据;
size();//获得向量中数据大小;
push_back(elem);//将数据插入向量尾;
pop_back();//删除向量尾部数据;
迭代器:iterator
链表:list
特点:数据插入速度快
映射:map
#include <vector>
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main(void)
{
// 使用vector存储数字:3、4、8、4
vector<int> vec;
vec.push_back(3);
vec.push_back(2);
vec.push_back(8);
vec.push_back(4);
//循环打印数字
vector<int>::iterator itor = vec.begin();
for(;itor!=vec.end();itor++){
cout<<*itor<<endl;
}
// 使用map来存储字符串键值对
map<string, string> m;
pair<string,string> p1("S","S-Shang Hai");
pair<string,string> p2("B"," B-Bei Jing");
pair<string,string> p3("G"," G-Guang Zhou");
m.insert(p1);
m.insert(p2);
m.insert(p3);
map<string,string>::iterator itm = m.begin();
// 打印map中数据
for(;itm!=m.end();itm++){
cout<<itm->first<<endl;
cout<<itm->second<<endl;
}
return 0;
}