cpp 9.10
来源:互联网 发布:手机验钞灯软件2.0 编辑:程序博客网 时间:2024/05/16 11:52
9.10
#include<iostream>#include<new>const int BUF = 512;const int N = 5;char buffer[BUF];int main(){using namespace std;double *pd1, *pd2;int i;cout << "Calling new and placement new:\n";pd1 = new double[N];pd2 = new (buffer) double[N];for (i = 0; i < N; i++)pd2[i] = pd1[i] = 1000 + 20.0*i;cout << "Memory address:\n" << " heap: " << pd1<< " static: " << (void *)buffer << endl;cout << "Memory contents:\n";for (i = 0; i < N; i++){cout << pd1[i] << " at " << &pd1[i] << "; ";cout << pd2[i] << " at " << &pd2[i] << endl;}cout << "\nCalling new and placement new a second time:\n";double *pd3, *pd4;pd3 = new double[N];pd4 = new (buffer) double[N];for (i = 0; i < N; i++)pd4[i] = pd3[i] = 1000 + 40.0*i;cout << "Memory contents:\n";for (i = 0; i < N; i++){cout << pd3[i] << " at " << &pd3[i] << "; ";cout << pd4[i] << " at " << &pd4[i] << endl;}cout << "\nCalling new and placement new a third time:\n";delete[] pd1;pd1 = new double[N];pd2 = new(buffer + N * sizeof(double)) double[N];for (i =0; i < N; i++)pd2[i] = pd1[i] = 1000 + 60.0*i;cout << "Memory contents:\n";for (i = 0; i < N; i++){cout << pd1[i] << " at " << &pd1[i] << "; ";cout << pd2[i] << " at " << &pd2[i] << endl;}delete[] pd1;delete[] pd3;system("pause");return 0;}
namesp.h
#include<string>namespace pers{struct Person{std::string fname;std::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<iostream>#include"namesp.h"namespace pers{using std::cout;using std::cin;void getPerson(Person & rp){cout << "Enter first name: ";cin >> rp.fname;cout << "Enter last name: ";cin >> rp.lname;}void showPerson(const Person &rp){std::cout << rp.lname << ", " << rp.fname;}}namespace debts{void getDebt(Debt & rd){getPerson(rd.name);std::cout << "Enter debt: ";std::cin >> rd.amount;}void showDebt(const Debt & rd){showPerson(rd.name);std::cout << ": $" << rd.amount << std::endl;}double sumDebts(const Debt ar[], int n){double total = 0;for (int i = 0; i < n; i++)total += ar[i].amount;system("pause");return 0;}}
namessp.cpp
#include<iostream>#include"namesp.h"void other(void);void another(void);int main(void){using debts::Debt;using debts::showDebt;Debt golf = { {"Benny", "Goatsniff"}, 120.0 };showDebt(golf);other();another();system("pause");return 0;}void other(void){using std::cout;using std::endl;using namespace debts;Person dg = { "Doodles", "Glister" };showPerson(dg);cout << endl;Debt zippy[3];int i;for (i = 0; i < 3; i++)getDebt(zippy[i]);for (i = 0; i < 3; i++)showDebt(zippy[i]);cout << "Total debt: $" << sumDebts(zippy, 3) << endl;system("pause");return;}void another(void){using pers::Person;Person collector = { "Milo", "Rightshift" };pers::showPerson(collector);std::cout << std::endl;}
0 0
- cpp 9.10
- cpp
- CPP
- CPP
- cpp
- cpp
- CPP
- CPP
- ClientSockThread.cpp&&ClientUrlThread.cpp
- cpp调用其他cpp
- ex34a.cpp&&ex34aDoc.cpp&&ex34aView.cpp
- MainFrm.cpp&&ServerThread.cpp&&SheetConfig.cpp
- CPP thread
- cpp->html
- CFaderWnd.cpp
- PasswordReminder.cpp
- object.cpp
- CmdTarget.cpp
- maven 环境搭建 Myeclipse配置
- Spring框架知识点总结
- hive 环境搭建
- 程序控制静音c#
- Android 蓝牙自动配对
- cpp 9.10
- Linux中Weblogic启动异常
- Mybatis源码分析--返回值ResultType和ResultMap
- 神经网络程序设计学习心得
- 代码获取device是否有root权限
- mvc框架封装(二)路由类篇
- mysql数据类型详解
- XML
- JavaScript继承详解(二)