文章目录
9.1 2 3
代码
//头文件coordin.h
#pragma once
#ifndef COORDIN_H_
#define COORDIN_H_
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
polar rect_to_polar(rect xypos);
void show_polar(polar dapos);
#endif // !COORDIN_H_
//主函数file1.cpp
#include <iostream>
#include "coordin.h"
using namespace std;
int main()
{
rect rplace;
polar pplace;
cout << "Enter the x and y values: ";
while (cin>>rplace.x>>rplace.y)
{
pplace = rect_to_polar(rplace);
show_polar(pplace);
cout << "Next two numbers (q to quit): ";
}
cout << "Bye!\n";
return 0;
}
//被调函数file2.cpp
#include <iostream>
#include <cmath>
#include "coordin.h"
using namespace std;
polar rect_to_polar(rect xypos)
{
polar answer;
answer.distance =
sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
answer.angle = atan2(xypos.y, xypos.x);
return answer;
}
void show_polar(polar dapos)
{
const double Rad_to_deg = 57.29577951;
cout << "distance = " << dapos.distance;
cout << ", angle = " << dapos.angle *Rad_to_deg;
cout << " degrees\n";
}
结果

知识点
1.将程序各部分分开放置
2.头文件中存放的内容
3.主函数和被调函数都需要#include “头文件”
9.4 auto.cpp
代码
#include <iostream>
using namespace std;
void oil(int x);
int main()
{
int texas = 31;
int year = 2011;
cout << "In main(), texas = " << texas << ", &texas = ";
cout << &texas << endl;
cout << "In main(), year = " << year << ", &year = ";
cout << &year << endl;
oil(texas);
cout << "In main(), texas = " << texas << ", &texas = ";
cout << &texas << endl;
cout << "In main(), year = " << year << ", &year = ";
cout << &year << endl;
return 0;
}
void oil(int x)
{
int texas = 5;
cout << "In oil(), texas = " << texas << ", &texas = ";
cout << &texas << endl;
cout << "In oil(), x = " << x << ", &year = ";
cout << &x << endl;
{
int texas = 113;
cout << "In block, texas = " << texas << ", &texas = ";
cout << &texas << endl;
cout << "In block, x = " << x << ", &year = ";
cout << &x << endl;
}
cout << "Post-block texas = " << texas;
cout << ", &texas = " << &texas << endl;
}
结果

知识点
1.变量的作用域
9.5 6
代码
//external.cpp
#include <iostream>
using namespace std;
double warming = 0.3;
void update(double dt);
void local();
int main()
{
cout << "Global warming is " << warming << " degrees.\n";
update(0.1);
cout << "Global warming is " << warming << " degrees.\n";
local();
cout << "Global warming is " << warming << " degrees.\n";
return 0;
}
//support.cpp
#include <iostream>
using namespace std;
//使用其他cpp文件中的变量warming
extern double warming;
void update(double dt);
void local();
void update(double dt)
{
extern double warming;
warming += dt;
cout << "Updating global warming to " << warming;
cout << " degrees.\n";
}
void local()
{
double warming = 0.8;
cout << "Local warming = " << warming << " degrees.\n";
cout << "But global warming = " << ::warming;
cout << " degrees.\n";
}
结果

知识点
1.extern 使用外部变量
9.7 8
代码
//twofile1.cpp
#include <iostream>
using namespace std;
int tom = 3;
int dick = 30;
//静态变量
static int harry = 300;
void remote_access();
int main()
{
cout << "main() reports the following address:\n";
cout << &tom << " = &tom, " << &dick << " = &dick, ";
cout << &harry << " = &harry\n";
remote_access();
return 0;
}
//twofile2.cpp
#include <iostream>
using namespace std;
extern int tom;
static int dick = 10;
int harry = 200;
void remote_access()
{
cout << "remote_access() reports the following addresses:\n";
cout << &tom << " = &tom, " << &dick << " = &dick, ";
cout << &harry << " = &harry\n";
}
结果

知识点
1.内部变量和外部变量
2.静态static
9.9 static.cpp
代码
//static.cpp
#include <iostream>
using namespace std;
const int ArSize = 10;
void strcount(const char* str);
int main()
{
char input[ArSize];
char next;
cout << "Enter a line:\n";
cin.get(input, ArSize);
while (cin)
{
cin.get(next);
while (next != '\n')
cin.get(next);
strcount(input);
cout << "Enter next line (empty line to quit):\n";
cin.get(input, ArSize);
}
cout << "Bye\n";
return 0;
}
void strcount(const char* str)
{
static int total = 0;
int count = 0;
cout << "\"" << str << "\"contains ";
while (*str++)
count++;
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
结果

知识点
1.static
9.10 newplace.cpp
代码
//newplace.cpp
#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 (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 ( i = 0; i < N; i++)
{
cout << pd1[i] << " at " << &pd1[i] << "; ";
cout << pd2[i] << " at " << &pd1[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 second 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;
}
return 0;
}
结果

知识点
1.new分配内存
9.11 12 13
代码
//头文件
#pragma once
//namesp.h
#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 <iostream>
#include "namesp.h"
using namespace std;
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)
{
cout << rp.lname << ", " << rp.fname;
}
}
namespace debts
{
void getDebt(Debt& rd)
{
getPerson(rd.name);
cout << "Enter debt: ";
cin >> rd.amount;
}
void showDebt(const Debt& rd)
{
showPerson(rd.name);
cout << ": $" << rd.amount << endl;
}
double sumDebts(const Debt ar[], int n)
{
double total = 0;
for (int i = 0; i < n; i++)
{
total += ar[i].amount;
}
return total;
}
}
#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();
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;
return;
}
void another(void)
{
using pers::Person;
Person collector = { "Milo","Rightshift" };
pers::showPerson(collector);
std::cout << std::endl;
}
结果

知识点
1.命名空间
总结
革命尚未成功,同志还需努力!
本文介绍了C++编程中的多个实用技巧,包括如何通过头文件管理结构和函数声明、使用auto关键字简化变量类型定义、掌握作用域和变量生命周期、利用extern关键字访问外部变量、理解静态变量的特性、使用new操作符进行内存分配以及运用命名空间避免命名冲突。

2101

被折叠的 条评论
为什么被折叠?



