友元函数无法访问私有成员,一个可以一个不行
#include <iostream>
using namespace std;
class Cat
{
public:
Cat(double w) :weight(w)
{
numOfCats++;
cout << "Cat Object is constructing, the constructed Number is " << numOfCats << ", and the lived Number is" << numOfCats - numOfCatsGo << endl;
}
static int getNumOfCats(bool IsConstruted);
friend double getTotalWeight(Cat&,Boat&);
~Cat()
{
numOfCatsGo++;
cout << "Cat Object is deconstructing, and the lived Number is " << numOfCats - numOfCatsGo << endl;
}
private:
static int numOfCats, numOfCatsGo;
double weight;
};
int Cat::numOfCats = 0;
int Cat::numOfCatsGo = 0;
class Boat
{
public:
Boat(double w) :weight(w){}
friend double getTotalWeight(Cat&,Boat&);
private:
double weight;
};
int Cat::getNumOfCats(bool IsConstruted)
{
if (IsConstruted)
return numOfCats;
else
return numOfCatsGo;
}
double getTotalWeight(Cat&c,Boat&b)
{
return (c.weight + b.weight);
}
int main()
{
int n;
double w0, w1;
cin >> n >> w0 >> w1;
for (int i = 0; i < n; i++)
{
Cat* c = new Cat(w0);
delete c;
}
cout << "Cat Object is constructing, the constructed Number is " << Cat::getNumOfCats(true) << ", and the lived Number is " << Cat::getNumOfCats(true) - Cat::getNumOfCats(false) << endl;
Cat c(w0);
Boat b(w1);
cout << getTotalWeight(c, b) << endl;
cout << "Cat object is deconstructing, and the lived Number is " << Cat::getNumOfCats(true) - Cat::getNumOfCats(false) << endl;
std::cout << "Hello World!\n";
}
这样看不出问题,看看完整代码
由定理3可得:有向完全图必然存在欧拉回路。
对于k个结点的有向完全图,可以从1号结点出发走到k号结点,
然后遍历2-(k-1)号结点,每次从k号结点走到该结点,然后再返回k号结点。
最后从k号结点出发走到1号结点。
剩下的未经过的边恰好就是k-1个节点的有向完全图,递归执行该过程即可。
void getou(vector<int>&q,int k)///获取k个节点的有向完全图的欧拉回路,默认起始点为1,如果有自环加上注释即可。
{
if(k==1)
{
//q.push_back(1);
return ;
}
q.push_back(k);
for(int i=2;i<k;i++)
{
q.push_back(i);
q.push_back(k);
}
//q.push_back(k);
q.push_back(1);
getou(q,k-1);
}