C++类,构造函数,利用友元判断2个类数据

来源:互联网 发布:nginx搭建wss环境 编辑:程序博客网 时间:2024/06/08 18:41
// stdafx.h : 标准系统包含文件的包含文件,// 或是经常使用但不常更改的// 特定于项目的包含文件//#pragma once#include "targetver.h"#include <stdio.h>#include <tchar.h>#include "MarryH.h"// TODO: 在此处引用程序需要的其他头文件
声明:
#ifndef __MARRYH_H__#define __MARRYH_H__#include <iostream>#include <string>#include <vector>using namespace std;class Marry{public://传入年龄/ 姓名 / 性别 / 婚否 //以类名作为函数名 无返回类型 构造函数Marry(int Age = 0 , string Name = NULL, string Sex = NULL, string Ismarried = NULL);//赋值构造函数Marry(const Marry &s);//输出void Show();//判断是否能结婚//拥有2个类复制,需要friend友元,operator重载操作符号friend bool operator==( Marry const &a, Marry const &s );private://定义变量string sex;string  ismarried;int age;string name;};#endif
完成函数:
#include "stdafx.h"#include "MarryH.h"//如果定义Marry那应该以实体参数传入Name->name = "小朋友";//数据初始化Marry::Marry(int Age, string Name, string Sex, string Ismarried ){ age = Age; name = Name; sex = Sex; ismarried = Ismarried;}//复制构造函数Marry::Marry(const Marry &s){age = s.age;name = s.age;sex = s.sex;ismarried = s.ismarried;}//输出数据void Marry::Show(){cout << " Data: " <<endl;cout << "姓名:" << name << "\t年龄:" << age<< "\t性别:" << sex << "\t婚否:" << ismarried << endl;/*return this->name;*/}//判断是否能成婚bool operator==( const Marry &a, const Marry &s ){if (a.age >= 22 && s.age >= 22 && a.sex != s.sex && a.ismarried == s.ismarried){//if (a.ismarried == s.ismarried){return true;}else{return false;}}
执行文件:
项目名称:判断能否结婚项目负责人:猴子项目运行环境:vs2010学习目标:类,构造函数关键字:friend operator== ::*/#include "stdafx.h"#include "MarryH.h"#include <iostream>int _tmain(int argc, _TCHAR* argv[]){//Marry xm;Marry xm(17,"小飞","男","未婚");Marry dm(18,"飞姐","女","未婚");xm.Show();dm.Show();cout << "成婚判断: " << endl;if ( xm == dm){cout << "能凑成一对!" << endl;}else{cout <<  "对不起,但怕你要棒打鸳鸯才的行,需要成长一段时间!"  << endl;}getchar();return 0;}



 
原创粉丝点击