九、C++ 静态成员以及友元

1.静态数据成员和静态成员函数

cnt:记录用Person类实例化了多少个对象,在构造函数里面进行 cnt++

这个值 cnt 应该属于整个 Person类,只有一份,而不是属于某一个对象,怎么体现这一点???

用static

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Person {
private:
	static int cnt;    //只是声明了有这个数据成员,没有分配内存空间
	char *name;
	int age;
	char *work;

public:

	static int getCount(void) 
	{ 
		return cnt;   //静态成员函数不能访问非静态变量name,
                        //调用这个函数时,不一定实例化对象了
	}

	Person() {//cout <<"Pserson()"<<endl;
		name = NULL;
		work = NULL;
		cnt++;
	}
	Person(char *name) 
	{
		//cout <<"Pserson(char *)"<<endl;
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
		this->work = NULL;
		cnt++;
	}

	Person(char *name, int age, char *work = "none") 
	{
		cout <<"Pserson(char*, int), name = "<<name<<", age= "<<age<<endl;
		this->age = age;

		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);

		this->work = new char[strlen(work) + 1];
		strcpy(this->work, work);
		cnt++;
	}

	Person(Person &per) 
	{
		cout <<"Pserson(Person &)"<<endl;
		this->age = per.age;

		this->name = new char[strlen(per.name) + 1];
		strcpy(this->name, per.name);

		this->work = new char[strlen(per.work) + 1];
		strcpy(this->work, per.work);
		cnt++;
	}

	~Person()
	{
		cout << "~Person()"<<endl;
		if (this->name) {
			cout << "name = "<<name<<endl;
			delete this->name;
		}
		if (this->work) {
			cout << "work = "<<work<<endl;
			delete this->work;
		}
	}

	void printInfo(void)
	{
		//printf("name = %s, age = %d, work = %s\n", name, age, work); 
		cout<<"name = "<<name<<", age = "<<age<<", work = "<<work<<endl;
	}
};

int Person::cnt = 0; /* 在类外面定义和初始化,不需要加static */
                        放在类里面会出错

int main(int argc, char **argv)
{

	cout << "person number = "<<Person::getCount()<<endl;
    
    //Person::cnt;  可以这样访问这个cnt
    //Person::getCount();  可以这样调用这个方法

	return 0;
}

 在main函数之外定义和初始化静态数据成员,为什么?

因为我们想在所有对象创建之前,就定义和初始化静态数据成员

int getCount(void)   // 这个函数只能用per1.getCount来调用,不属于整个类



static int getCount(void)   //不用实例化对象也可以调用,属于整个类的

静态成员函数可以在类里面声明并定义

也可以在类里面声明,在外面定义

class Person {
private: 
	static int cnt;   //声明
	char *name;
	int age;
	char *work;

public:

	static int getCount(void);   //声明

.......
}



int Person::cnt = 0;   /*定义*/ 

int Person::getCount(void)   /*定义*/
{   
	return cnt; 
}

 可以通过对象来调用getCount( )

int main(int argc, char **argv)
{
	Person p[100];
	cout << "person number = "<<Person::getCount()<<endl;
	cout << "person number = "<<p[0].getCount()<<endl;
	cout << "person number = "<<p[1].getCount()<<endl;

	return 0;
}

2.友元

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() {}
	Point(int x, int y) : x(x), y(y) {}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.setX(p1.getX()+p2.getX());
	n.setY(p1.getY()+p2.getY());
	return n;
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point p2(2, 3);

	Point sum = add(p1, p2);
	sum.printInfo();

	return 0;
}

缺点:相加需要调用6个函数,效率低

改进:把add设置成class的朋友,可以直接访问private

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Point {
private:
	int x;
	int y;

public:
	Point() {}
	Point(int x, int y) : x(x), y(y) {}

	int getX(){ return x; }
	int getY(){ return y; }
	void setX(int x){ this->x = x; }
	void setY(int y){ this->y = y; }
	void printInfo()
	{
		cout<<"("<<x<<", "<<y<<")"<<endl;
	}
	friend Point add(Point &p1, Point &p2);  //声明
};

Point add(Point &p1, Point &p2)
{
	Point n;
	n.x = p1.x+p2.x;
	n.y = p1.y+p2.y;
	return n;
}

int main(int argc, char **argv)
{
	Point p1(1, 2);
	Point p2(2, 3);

	Point sum = add(p1, p2);
	sum.printInfo();

	return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值