C++友元类实现
C++中的友元既可以实现友元函数,也可以实现友元类,也就是说一个类也可以作为另外一个类的友元。当作为一个类的友元时,它的所有成员函数都是另一个类的友元函数,都可以访问另一个类的私有或者公有成员。
请看实例:
#include <iostream>
#include <cstring>
using namespace std ;
//声明教师类
class Techer ;
//学生类
class Student
{
private:
string name ;
int age ;
char sex ;
int score ;
public :
Student(string name , int age , char sex , int score);
void stu_print(Techer &tech);
};
//教师类
class Techer
{
private:
string name ;
int age ;
char sex ;
int score ;
public :
Techer(string name , int age , char sex , int score);
//声明一个友元类
friend Student ;
};
//Student类的构造函数的实现
Student::Student(string name , int age , char sex , int score)
{
this->name = name ;
this->age = age ;
this->sex = sex ;
this->score = score ;
}
//Techer类的构造函数的实现
Techer::Techer(string name , int age , char sex , int score)
{
this->name = name ;
this->age = age ;
this->sex = sex ;
this->score = score ;
}
//打印Student类中的私有成员和Techer的私有成员
void Student::stu_print(Techer &tech)
{
//用this指针访问本类的成员
cout << this->name << endl ;
cout << this->age << endl ;
cout << this->sex << endl ;
cout << this->score << endl ;
//访问Techer类的成员
cout << tech.name << endl ;
cout << tech.age << endl ;
cout << tech.sex << endl ;
cout << tech.score << endl ;
}
int main(void)
{
Student stu1("YYX",24,'N',86);
Techer t1("hou",40,'N',99);
stu1.stu_print(t1);
return 0 ;
}
运行结果:
YYX
24
N
86
hou
40
N
99
C++友元类实现的更多相关文章
- C++的友元类和友元函数实例
#include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...
- 不可或缺 Windows Native (20) - C++: 友元函数, 友元类
[源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...
- C++学习12 友元函数和友元类
友元函数和友元类在实际开发中较少使用,想快速学习C++的读者可以跳过本节. 一个类中可以有 public.protected.private 三种属性的成员,通过对象可以访问 public 成员,只有 ...
- YTU 2619: B 友元类-计算两点间距离
2619: B 友元类-计算两点间距离 时间限制: 1 Sec 内存限制: 128 MB 提交: 469 解决: 252 题目描述 类Distance定义为类Point的友元类来实现计算两点之间距 ...
- C++:友元(非成员友元函数、成员友元函数、友元类)
3.8 友元:友元函数和友元类 友元函数 :既可以是不属于任何类的非成员函数,也可以是另一个类的成员函数,统称为友元函数.友元函数不是当前类的成员函数,而是独立于类的外部函数,但它可以访问该类所有的 ...
- c++ 友元类
一.友元类相关概念 要将私有成员数据或函数暴露给另一个类,必须将后者声明为友元类. 注意三点: (1)友元关系不能传递 (2)友元关系不能继承 (3)友元关系不能互通
- C++学习之友元类和友元函数
C++学习之友元类和友元函数 模板类声明也可以有友元,模板的友元可以分为以下几类: 1.非模板友元: 2.约束模板友元,即就是友元的类型取决于类被实例化的时候的 ...
- C++ 习题 输出日期时间--友元类
Description 设计一个日期类和时间类,编写display函数用于显示日期和时间.要求:将Time类声明为Date类的友元类,通过Time类中的display函数引用Date类对象的私有数据, ...
- C/C++:C++友元类
C++友元类: 正常情况下,一个类是訪问不到还有一个类的私有成员的. 就像以下这个: #include <iostream> class A{ private: int a; public ...
- 友元(友元函数、友元类和友元成员函数) C++
有些情况下,允许特定的非成员函数访问一个类的私有成员,同时仍阻止一般的访问,这是很方便做到的.例如被重载的操作符,如输入或输出操作符,经常需要访问类的私有数据成员. 友元(frend)机制允许一个类将 ...
随机推荐
- 快速排序算法C语言版
快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. R. Hoare在1962年提出.它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比 ...
- BLOG总结
1.登录:http://www.cnblogs.com/shaojiafeng/p/7868195.html 2.注册 - urls -前端页面中写 username ,password,passwo ...
- Mootools遮罩层练习(原为网上的jquery写法)
<html > <head> <meta charset="utf-8" /> <title>mootools 遮罩层</ti ...
- s5_day14作业
import re # 1. 匹配一段文本中的每行的邮箱 # ret=re.findall('\w+@\w+\.com','10000@qq.com,qwe48645313@163.com') # p ...
- Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary
地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...
- 《Python学习手册》(三)
string 字符串,和列表.元组,都适用序列操作. 关于python转义字符 迭代 for x in S: print(x) [c * 2 for c in S] Comparasion: > ...
- Mac & Xcode 技巧
1.Xcode 删除过期的Provisioning Profile文件 解决:Xcode 中所有的Provisioning Profile文件路径,都在 ~/Library/MobileDevice/ ...
- Oracle数据库的数据导入导出
--备份数据库--数据库系统用户账号system/adminuser --查看oracle数据库的用户select * from all_users;--查看oracle数据库的版本号select * ...
- angularjs Dom方式访问疑似可以访问ifame结构项目
一.定位需要访问控制器元素 var currObj = document.querySelector('[ng-controller="munuListCtrl"]'); 或者 v ...
- get the request body of all quests before handle it
https://stackoverflow.com/questions/23660340/need-to-log-asp-net-webapi-2-request-and-response-body- ...