名称空间

This commit is contained in:
朱毅骏 2021-08-16 18:28:23 +08:00
parent 5624ce6ddb
commit 50d4066c2a
6 changed files with 187 additions and 1 deletions

View File

@ -45,4 +45,6 @@ add_executable(test10001 namespacetest/test10001.cpp)
add_executable(auto namespacetest/auto.cpp)
add_executable(external namespacetest/external.cpp)
add_executable(support namespacetest/support.cpp)
add_executable(ststic namespacetest/ststic.cpp)
add_executable(ststic namespacetest/ststic.cpp)
add_executable(newplace namespacetest/newplace.cpp)
add_executable(namessp namespacetest/namessp.cpp)

View File

@ -0,0 +1,44 @@
//
// Created by zhuyijun on 2021/8/16.
//
#include <iostream>
#include "namesp.h"
namespace pers {
using std::cout;
using std::cin;
using std::endl;
void getPerson(Person &rp) {
cout << "Enter first name: ";
cin >> rp.fname;
cout << "Enter left name: ";
cin >> rp.lname;
}
void showPerson(const Person &rp) {
cout << rp.lname << ", " << rp.fname << endl;
}
}
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;
}
return total;
}
}

View File

@ -0,0 +1,32 @@
//
// Created by zhuyijun on 2021/8/16.
//
#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);
}

View File

@ -0,0 +1,48 @@
//
// Created by zhuyijun on 2021/8/16.
//
#include <iostream>
#include "namesp.h"
void other(void);
void another(void);
/**
*
* g++ .\namesp.cpp .\namessp.cpp -o test
* @return
*/
int main() {
using debts::Debt;
using debts::showDebt;
Debt golf = {{"Benny", "Goatsniff"}, 120.0};
showDebt(golf);
other();
another();
return 0;
}
void other(void) {
using std::cout;
using std::endl;
using namespace debts;
Person dg = {"Doodles", "Glister"};
showPerson(dg);
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;
return;
}
void another(void) {
using pers::Person;
Person collector = {"Milo", "Rightshift"};
pers::showPerson(collector);
std::cout << std::endl;
}

View File

@ -0,0 +1,60 @@
//
// Created by zhuyijun on 2021/8/16.
//
#include <iostream>
#include <new>
using namespace std;
const int BUF = 512;
const int N = 5;
char buffer[BUF];
int main() {
double *pd1, *pd2;
int i;
cout << "Calling new and placement new:\n";
pd1 = new double[N];
pd2 = new(buffer) double[N];
for (int i = 0; i < N; ++i) {
pd2[i] = pd1[i] = 1000 + 20.0 * i;
}
cout << "Memory addresses:\n" << " heap: " << pd1 << " static: " << (void *) buffer << endl;
cout << "Memory contents:\n";
for (int i = 0; i < N; ++i) {
cout << pd1[i] << " at" << &pd1[i] << "; ";
cout << pd2[i] << " at" << &pd2[i] << endl;
}
cout << "\n Calling new and placement new a second time:\n";
double *pd3, *pd4;
pd3 = new double[N];
pd4 = new(buffer) double[N];
for (int i = 0; i < N; ++i) {
pd4[i] = pd3[i] = 1000 + 40.0 * i;
}
cout << "Memory contents:\n";
for (int i = 0; i < N; ++i) {
cout << pd3[i] << " at" << &pd3[i] << "; ";
cout << pd4[i] << " at" << &pd4[i] << endl;
}
cout << "\n Calling new and placement new a second time:\n";
delete[]pd1;
pd1 = new double[N];
pd2 = new(buffer + N * sizeof(double)) double[N];
for (int i = 0; i < N; ++i) {
pd2[i] = pd1[i] = 1000 + 60.0 * i;
}
cout << "Memory contents:\n";
for (int i = 0; i < N; ++i) {
cout << pd1[i] << " at" << &pd1[i] << "; ";
cout << pd2[i] << " at" << &pd2[i] << endl;
}
delete[]pd1;
delete[]pd3;
// delete[]pd2;
// delete[]pd4;
return 0;
}

BIN
base/namespacetest/test.exe Normal file

Binary file not shown.