class类成员 友元 2011.04.25
2011-04-25 16:41
155 查看
今天看了两章《think in C++》
简单的写了一下 类成员的几种分类:public , private, protected
还有就是友元,虽然很不鼓励用friend 但他毕竟是C++的一部分,了解一下
#include <iostream>
using namespace std;
#include <string>
class Person
{
string name;
string play;
protected:
int year;
public:
friend class pn;
friend void show(Person & obj);
void func(string & nn, string & play);
void print();
};
class persong : public Person
{
public:
void timelong(int & y)
{
year = y;
}
void print()
{
cout<<"he do it "<<year<<" years"<<endl;
}
};
void show(Person & obj)
{
cout<<obj.name<<" can play "<<obj.play<<endl;
}
void Person::func(string & nn, string & play)
{
name = nn;
this->play = play;
}
void Person::print()
{
cout<<name<<" can play "<<play<<endl;
}
class pn
{
public:
void show(Person & obj)
{
obj.print();
}
};
int main()
{
Person p;
string name = "micheal";
string play = "basketball";
p.func(name, play);
p.print();
persong q;
int i = 9;
q.timelong(i);
q.print();
cout<<"-------------------------"<<endl;
show(p);
cout<<"---------------------------"<<endl;
pn hh;
hh.show(p);
return 0;
}
输出:
micheal can play basketball
he do it 9 years
-------------------------
micheal can play basketball
---------------------------
micheal can play basketball
下面贴一篇文章是叙述友元的:
在说明什么是友元之前,我们先说明一下为什么需要友元与友元的缺点:
通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为public(共用的),然而这做带来的问题遍是任何外部函数都可以毫无约束的访问它操作它,c++利用friend修饰符,可以让一些你设定的函数能够对这些保护数据进行操作,避免把类成员全部设置成public,最大限度的保护数据成员的安全。 友元能够使得普通函数直接访问类的保护数据,避免了类成员函数的频繁调用,可以节约处理器开销,提高程序的效率,但矛盾的是,即使是最大限度的保护,同样也破坏了类的封装特性,这即是友元的缺点,在现在cpu速度越来越快的今天我们并不推荐使用它,但它作为c++一个必要的知识点,一个完整的组成部分,我们还是需要讨论一下的。
在类里声明一个普通函数,在前面加上friend修饰,那么这个函数就成了该类的友元,可以访问该类的一切成员。
下面我们来看一段代码,看看我们是如何利用友元来访问类的一切成员的。
#include <iostream>
using namespace std;
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj);//友元函数的声明
public:
char name[20];
char address[20];
};
void ShowN(Internet &obj)//函数定义,不能写成,void Internet::ShowN(Internet &obj)
{
cout<<obj.name<<endl;
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
ShowN(a);
cin.get();
}
上面的代码通过友元函数的定义,我们成功的访问到了a对象的保护成员name,友元函数并不能看做是类的成员函数,它只是个被声明为类友元的普通函数,所以在类外部函数的定义部分不能够写成void Internet::ShowN(Internet &obj),这一点要注意。
一个普通函数可以是多个类的友元函数,对上面的代码我们进行修改,注意观察变化:
#include <iostream>
using namespace std;
class Country;
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj,Country &cn);//注意这里
public:
char name[20];
char address[20];
};
class Country
{
public:
Country()
{
strcpy(cname,"中国");
}
friend void ShowN(Internet &obj,Country &cn);//注意这里
protected:
char cname[30];
};
void ShowN(Internet &obj,Country &cn)
{
cout<<cn.cname<<"|"<<obj.name<<endl;
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
ShowN(a,b);
cin.get();
}
一个类的成员函数函数也可以是另一个类的友元,从而可以使得一个类的成员函数可以操作另一个类的数据成员,我们在下面的代码中增加一类Country,注意观察:
#include <iostream>
using namespace std;
class Internet;
class Country
{
public:
Country()
{
strcpy(cname,"中国");
}
void Editurl(Internet &temp);//成员函数的声明
protected:
char cname[30];
};
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void Country::Editurl(Internet &temp);//友元函数的声明
protected:
char name[20];
char address[20];
};
void Country::Editurl(Internet &temp)//成员函数的外部定义
{
strcpy(temp.address,"edu.cndev-lab.com");
cout<<temp.name<<"|"<<temp.address<<endl;
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
b.Editurl(a);
cin.get();
}
整个类也可以是另一个类的友元,该友元也可以称做为友类。友类的每个成员函数都可以访问另一个类的所有成员。
示例代码如下:
#include <iostream>
using namespace std;
class Internet;
class Country
{
public:
Country()
{
strcpy(cname,"中国");
}
friend class Internet;//友类的声明
protected:
char cname[30];
};
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
void Editcname(Country &temp);
protected:
char name[20];
char address[20];
};
void Internet::Editcname(Country &temp)
{
strcpy(temp.cname,"中华人民共和国");
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
a.Editcname(b);
cin.get();
}
在上面的代码中我们成功的通过Internet类Editcname成员函数操作了Country类的保护成员cname。
在编程中,我们使用友元的另外一个重要原因是为了方便重载操作符的使用
此文内容转自连接:http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/571623.html
简单的写了一下 类成员的几种分类:public , private, protected
还有就是友元,虽然很不鼓励用friend 但他毕竟是C++的一部分,了解一下
#include <iostream>
using namespace std;
#include <string>
class Person
{
string name;
string play;
protected:
int year;
public:
friend class pn;
friend void show(Person & obj);
void func(string & nn, string & play);
void print();
};
class persong : public Person
{
public:
void timelong(int & y)
{
year = y;
}
void print()
{
cout<<"he do it "<<year<<" years"<<endl;
}
};
void show(Person & obj)
{
cout<<obj.name<<" can play "<<obj.play<<endl;
}
void Person::func(string & nn, string & play)
{
name = nn;
this->play = play;
}
void Person::print()
{
cout<<name<<" can play "<<play<<endl;
}
class pn
{
public:
void show(Person & obj)
{
obj.print();
}
};
int main()
{
Person p;
string name = "micheal";
string play = "basketball";
p.func(name, play);
p.print();
persong q;
int i = 9;
q.timelong(i);
q.print();
cout<<"-------------------------"<<endl;
show(p);
cout<<"---------------------------"<<endl;
pn hh;
hh.show(p);
return 0;
}
输出:
micheal can play basketball
he do it 9 years
-------------------------
micheal can play basketball
---------------------------
micheal can play basketball
下面贴一篇文章是叙述友元的:
在说明什么是友元之前,我们先说明一下为什么需要友元与友元的缺点:
通常对于普通函数来说,要访问类的保护成员是不可能的,如果想这么做那么必须把类的成员都生命成为public(共用的),然而这做带来的问题遍是任何外部函数都可以毫无约束的访问它操作它,c++利用friend修饰符,可以让一些你设定的函数能够对这些保护数据进行操作,避免把类成员全部设置成public,最大限度的保护数据成员的安全。 友元能够使得普通函数直接访问类的保护数据,避免了类成员函数的频繁调用,可以节约处理器开销,提高程序的效率,但矛盾的是,即使是最大限度的保护,同样也破坏了类的封装特性,这即是友元的缺点,在现在cpu速度越来越快的今天我们并不推荐使用它,但它作为c++一个必要的知识点,一个完整的组成部分,我们还是需要讨论一下的。
在类里声明一个普通函数,在前面加上friend修饰,那么这个函数就成了该类的友元,可以访问该类的一切成员。
下面我们来看一段代码,看看我们是如何利用友元来访问类的一切成员的。
#include <iostream>
using namespace std;
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj);//友元函数的声明
public:
char name[20];
char address[20];
};
void ShowN(Internet &obj)//函数定义,不能写成,void Internet::ShowN(Internet &obj)
{
cout<<obj.name<<endl;
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
ShowN(a);
cin.get();
}
上面的代码通过友元函数的定义,我们成功的访问到了a对象的保护成员name,友元函数并不能看做是类的成员函数,它只是个被声明为类友元的普通函数,所以在类外部函数的定义部分不能够写成void Internet::ShowN(Internet &obj),这一点要注意。
一个普通函数可以是多个类的友元函数,对上面的代码我们进行修改,注意观察变化:
#include <iostream>
using namespace std;
class Country;
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void ShowN(Internet &obj,Country &cn);//注意这里
public:
char name[20];
char address[20];
};
class Country
{
public:
Country()
{
strcpy(cname,"中国");
}
friend void ShowN(Internet &obj,Country &cn);//注意这里
protected:
char cname[30];
};
void ShowN(Internet &obj,Country &cn)
{
cout<<cn.cname<<"|"<<obj.name<<endl;
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
ShowN(a,b);
cin.get();
}
一个类的成员函数函数也可以是另一个类的友元,从而可以使得一个类的成员函数可以操作另一个类的数据成员,我们在下面的代码中增加一类Country,注意观察:
#include <iostream>
using namespace std;
class Internet;
class Country
{
public:
Country()
{
strcpy(cname,"中国");
}
void Editurl(Internet &temp);//成员函数的声明
protected:
char cname[30];
};
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
friend void Country::Editurl(Internet &temp);//友元函数的声明
protected:
char name[20];
char address[20];
};
void Country::Editurl(Internet &temp)//成员函数的外部定义
{
strcpy(temp.address,"edu.cndev-lab.com");
cout<<temp.name<<"|"<<temp.address<<endl;
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
b.Editurl(a);
cin.get();
}
整个类也可以是另一个类的友元,该友元也可以称做为友类。友类的每个成员函数都可以访问另一个类的所有成员。
示例代码如下:
#include <iostream>
using namespace std;
class Internet;
class Country
{
public:
Country()
{
strcpy(cname,"中国");
}
friend class Internet;//友类的声明
protected:
char cname[30];
};
class Internet
{
public:
Internet(char *name,char *address)
{
strcpy(Internet::name,name);
strcpy(Internet::address,address);
}
void Editcname(Country &temp);
protected:
char name[20];
char address[20];
};
void Internet::Editcname(Country &temp)
{
strcpy(temp.cname,"中华人民共和国");
}
void main()
{
Internet a("中国软件开发实验室","www.cndev-lab.com");
Country b;
a.Editcname(b);
cin.get();
}
在上面的代码中我们成功的通过Internet类Editcname成员函数操作了Country类的保护成员cname。
在编程中,我们使用友元的另外一个重要原因是为了方便重载操作符的使用
此文内容转自连接:http://www.pconline.com.cn/pcedu/empolder/gj/c/0503/571623.html
相关文章推荐
- 友元函数、友元类、访问私有数据成员、友元关系[C++]
- 类成员函数声明为另外一个类的友元
- 静态成员,内联,友元,const成员函数
- const成员函数,内联函数,友元与static成员
- 友元与继承、静态成员与继承
- c++的类成员权限以及友元
- C++之静态成员和友元
- C++静态成员与友元
- 友元函数、友元类、访问私有数据成员、友元关系
- C++知识文档六_对象常量_静态成员_友元_抽象类
- 不用友元,访问类的私有成员变量的方法
- 友元和static类成员
- c++友元成员函数
- 友元和继承访问类成员的方式的区别
- C++ 入门 (四) 私有数据成员和友元
- 私有数据成员和友元
- 【C++】友元与静态成员
- 类中静态成员与友元
- 二十五、继承(二) 不能自动继承的成员函数、继承与构造函数、友元关系与继承、静态成员与继承
- 友元函数、友元类、访问私有数据成员、友元关系