6.C++模版篇
6.1友元函数(但破坏封装性,所以一般不怎么用)
关键字:friend
前面已经得到:函数参数传入引用或者指针,传输速度更快
class Coordinate
{
friend void printXY(Coordinate &c);
public:
Coordinate(int x,int y);
private:
int m_iX;
int m_iY;
};
可以直接访问私有成员
void printXY(Coordinate &c)
{
cout<<c.m_iX<<c.m_iY<<endl;
}
传入对象名而非地址
int main()
{
Coordinate coor(3,5);
printXY(coor);
return 0;
}
6.2友元成员函数
class Coordinate
{
friend void Circle::printXY(Coordinate &c);
public:
Coordinate(int x,int y);
private:
int m_iX;
int m_iY;
};
class Circle
{
public:
void printXY(Coordinate &c)
{
cout<<c.m_iX<<c.m_iY<<endl;
}
};
int main()
{
Coordinate coor(3,5);
Circle circle;
circle.printXY(coor);
return 0;
}
6.3友元类
在使用友元类的前面得先声明一下友元类
class Circle;
class Coordinate
{
friend Circle;
public:
Coordinate(int x,int Y);
private:
int m_iX;
int m_iY;
};
class Circle
{
pubilc:
void printXY()
{
cout<<m_coor
.m_iX
<<m_coor
.
m_iY<<endl;
}
private:
Corrdinate m_coor;
};
友元类的使用注意事项:
1.友元关系不可传递
2.友元关系的单向性
3.友元声明的形式和数量不受限制
注:友元只是封装的补充,并不是很好的语法
6.3 静态 关键字:static
静态的数据成员和静态的成员函数是依赖于
类的,而
普通的数据成员和成员函数则是依赖于
对象的
注意事项:
1.静态数据成员必须单独初始化
2.静态成员函数不能调用非静态成员函数和非静态数据成员
3.静态数据成员只有一份,不依赖于对象而存在
4.静态的也不能用const修饰(const是this指针恒定,而静态没有this指针)
#include<iostream>
#include<stdlib.h>
#include<string>
#include"Tank.h"
using namespace std;
/*静态数据成员与静态成员函数
要求:定义Tank类,数据成员:坦克编号:m_cCode,坦克数量:s_iCount
成员函数:构造函数、析构函数、fire、getCount */
int main()
{
Tank *p=new Tank('A');
cout << Tank::getCount() << endl;
Tank *q = new Tank('B');
delete p;
delete q;
cout << Tank::getCount() << endl;
system("pause");
return 0;
}
Tank.h
#include <iostream>
#ifndef TANK_H
#define TANK_H
class Tank
{
public:
Tank(char code);
~Tank();
void fire();
static int getCount();
private:
char m_cCode;
static int s_iCount;
};
#endif
Tank.cpp
#include"Tank.h"
using namespace std;
int Tank::s_iCount = 10;
Tank::Tank(char code)
{
m_cCode = code;
s_iCount++;
cout << "Tank" << endl;
}
Tank::~Tank()
{
s_iCount--;
cout << "~Tank" << endl;
}
void Tank::fire()
{
cout << "Tank-fire" << endl;
}
int Tank::getCount()
{
return s_iCount;
}
6.4一元运算符重载(一个符号只与一个操作数运算)
概念:给原有运算符赋予新功能
本质:函数重载
关键字:operator
举例:负号运算符重载,++运算符重载
#include<string>
#include"Coordinate.h"
using namespace std;
/*运算符重载——一元运算符重载
要求:定义Coordinate类,数据成员:m_iX,m_iY
成员函数:构造函数、getX,getY */
int main()
{
Coordinate coor1(1, 3);
cout << coor1.getX()<< "," << coor1.getY() << endl;
/*-coor1;
cout << coor1.getX() << endl;
operator - (coor1);
cout << coor1.getY() << endl;*/
//coor1++;
cout << coor1++.getX() << ",";
cout<<coor1++.getY()<< endl;
system("pause");
return 0;
}
Coordinate.h
#ifndef COORDINATE_H
#define COORDINAETE_H
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
class Coordinate
{
friend Coordinate & operator - (Coordinate &c);
public:
Coordinate(int x,int y);
Coordinate &operator++();
Coordinate operator++(int);
//Coordinate & operator-();
//~Coordinate();
int getX();
int getY();
private:
int m_iX;
int m_iY;
};
#endif
Coordinate.cpp
#include<iostream>
#include<stdlib.h>
#include"Coordinate.h"
using namespace std;
Coordinate::Coordinate(int x,int y)
{
m_iX = x;
m_iY = y;
cout << "Coordinate() " << m_iX<<","<<m_iY <<endl;
}
//Coordinate::~Coordinate()
//{
// cout << "~Coordinate() " << m_iX << "," << m_iY << endl;
//}
Coordinate & operator - (Coordinate &c)
{
c.m_iX = -c.m_iX;
c.m_iY = -c.m_iY;
return c;
}
int Coordinate::getX()
{
return m_iX;
}
int Coordinate::getY()
{
return m_iY;
}
Coordinate &Coordinate::operator++()
{
m_iX++;
m_iY++;
return *this;
}
Coordinate Coordinate::operator++(int)
{
Coordinate old(*this);
m_iX++;
m_iY++;
return old; 这里要注意它的返回值
}