C++命名空间使用代码
2024-09-25 04:22:22
namesp.h
#pragma once
#include <string> namespace pers
{
using namespace std;
struct Person
{
string fname;
string lname;
};
void getPerson(Person &);
void showPerson(const Person &);
} namespace debts
{
using namespace pers;
struct Debt
{
Person name;
double amount;
};
void getDebt(Debt &);
void showDebt(const Debt &);
double sumDebts(const Debt ar[], int n);
}
namesp.cpp
#include "namesp.h"
#include <iostream> using namespace std; void pers::getPerson(Person & rp)
{
cout << "Enter first name: ";
cin >> rp.fname;
cout << "Enter last name: ";
cin >> rp.lname;
} void pers::showPerson(const Person & rp)
{
cout << rp.fname << ", " << rp.lname;
} void debts::getDebt(Debt & rd)
{
getPerson(rd.name);
cout << "Enter debt: ";
cin >> rd.amount;
} void debts::showDebt(const Debt & rd)
{
showPerson(rd.name);
cout << ": $" << rd.amount << endl;
} double debts::sumDebts(const Debt ar[], int n)
{
double total = ;
for (int i = ; i < n; i++)
{
total += ar[i].amount;
}
return total;
}
namessp.cpp
#include <iostream>
#include "namesp.h" void other(void);
void another(void); int main()
{
using debts::Debt;
using debts::showDebt; Debt golf = { {"Benny","Goatsniff"},120.0 };
showDebt(golf);
other();
another(); std::cin.get();
return ;
} void other(void)
{
using namespace std;
using namespace debts; Person dg = { "Doodles","Glister" };
showPerson(dg);
cout << endl; Debt zippy[];
int i;
for (int i = ; i < ; i++)
{
getDebt(zippy[i]);
}
for (i = ; i < ; i++)
{
showDebt(zippy[i]);
}
cout << "Total debt: $" << sumDebts(zippy, ) << endl;
return;
} void another(void)
{
using pers::Person;
Person collector = { "Milo","Rightshift" };
pers::showPerson(collector);
std::cout << std::endl;
}
最新文章
- AJAX POST&;跨域 解决方案 - CORS
- web前端工程师在移动互联网时代里的地位问题
- java session 详解
- B450黑苹果之路(1)
- Atitit.HTTP&#160;代理原理及实现&#160;正向代理与反向代理attilax总结
- 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏
- jenkins忘记管理员账号密码的补救方法-转
- mysql学习--mysql必知必会1
- iOS之pch文件的正确使用
- CSS多行文字截断
- js 数字,金额 用逗号 隔开。数字格式化
- poj 2553 The Bottom of a Graph【强连通分量求汇点个数】
- TextView总结
- Replace不区分大小写
- Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs)
- [leetcode-593-Valid Square]
- Android-TextView 控件常用属性以及基本用法
- iptables防火墙规则的添加、删除、修改、保存
- Orleans学习总结(四)--集群配置篇
- Android -- Handling back button press Inside Fragments
热门文章
- Dubbo与Hadoop RPC的区别
- 为什么在 js在 function($) 前面加分号
- ELK-Filebeat使用
- django数据模型中关于on_delete的使用
- CVE-2012-2122-Mysql身份认证漏洞及利用
- UIView-frame-VS-bounds
- Determining the Size of a Class Object---sizeof(class)---By Girish Shetty
- Azure File挂载报错--System Error 1231
- P2319 [HNOI2006]超级英雄 题解
- Linx 的组管理和权限管理