C++_静态成员_友员函数

C++_静态成员_友员函数

1、静态成员和静态成员函数
注意:
①静态成员函数中不能够访问非静态变量
下面例子是用来统计出一个类总共被创建了多少实例化对象

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

using namespace std;

class Person {
private:
	static int cnt;  //表示这个cnt的值是属于Person这个类的(实例化再多也只有一份)
	char *name;
	int age;
	char *work;

public:

	static int getCount(void)   //静态成员函数不能访问非静态变量
	{ 
		return cnt; 
	}

	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 setName(char *n)
	{
		name = n;
	}
	int setAge(int a)
	{
		if (a < 0 || a > 150)
		{
			age = 0;
			return -1;
		}
		age = a;
		return 0;
	}
	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; /* 定义和初始化(分配空间) */

int main(int argc, char **argv)
{
	Person per1;
	Person per2;
	Person per3;
	Person per4;
	
	Person *per5 = new Person[10];
	cout << "person number = "<<Person::getCount()<<endl; 

	return 0;
}

2、对上述代码进行改进,把函数实现放在类外面

#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); 

	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 setName(char *n)
	{
		name = n;
	}
	int setAge(int a)
	{
		if (a < 0 || a > 150)
		{
			age = 0;
			return -1;
		}
		age = a;
		return 0;
	}
	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; /* 定义和初始化 */

int Person::getCount(void) 
{ 
	return cnt; 
}

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;
}

3、友员函数的定义(提高代码运行效率)
注意:
①提高效率也可以直接把类的私有属性的变量变成公有的,这样可以直接获取和设置变量的值,但是这样有违类的封装属性,会让类里面的成员变得不安全。
下面例子是讲两个对象的坐标点相加

#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;
}

4、下面既不违反的类的权限,又可以提个效率(关键字:friend)
注意:可以设置友员函数,来访问私有属性

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值