forked from ShujiaHuang/Cpp-Primer-Plus-6th
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1f5253
commit 3a9076d
Showing
14 changed files
with
540 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// autoscp.cpp -- illustrating scope of automatic variables | ||
#include <iostream> | ||
void oil(int x); | ||
int main() | ||
{ | ||
using namespace std; | ||
|
||
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; | ||
// cin.get(); | ||
return 0; | ||
} | ||
|
||
void oil(int x) | ||
{ | ||
using namespace std; | ||
int texas = 5; | ||
|
||
cout << "In oil(), texas = " << texas << ", &texas = "; | ||
cout << &texas << endl; | ||
cout << "In oil(), x = " << x << ", &x = "; | ||
cout << &x << endl; | ||
{ // start a block | ||
int texas = 113; | ||
cout << "In block, texas = " << texas; | ||
cout << ", &texas = " << &texas << endl; | ||
cout << "In block, x = " << x << ", &x = "; | ||
cout << &x << endl; | ||
} // end a block | ||
cout << "Post-block texas = " << texas; | ||
cout << ", &texas = " << &texas << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// coordin.h -- structure templates and function prototypes | ||
// structure templates | ||
#ifndef COORDIN_H_ | ||
#define COORDIN_H_ | ||
|
||
struct polar | ||
{ | ||
double distance; // distance from origin | ||
double angle; // direction from origin | ||
}; | ||
struct rect | ||
{ | ||
double x; // horizontal distance from origin | ||
double y; // vertical distance from origin | ||
}; | ||
|
||
// prototypes | ||
polar rect_to_polar(rect xypos); | ||
void show_polar(polar dapos); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// external.cpp -- external variable | ||
// compile with support.cpp | ||
#include <iostream> | ||
// external variable | ||
double warming = 0.3; // warming defined | ||
|
||
// function prototypes | ||
void update(double dt); | ||
void local(); | ||
|
||
int main() // uses global variable | ||
{ | ||
using namespace std; | ||
cout << "Global warming is " << warming << " degrees.\n"; | ||
update(0.1); // call function to change warming | ||
cout << "Global warming is " << warming << " degrees.\n"; | ||
local(); // call function with local warming | ||
cout << "Global warming is " << warming << " degrees.\n"; | ||
// cin.get(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// file1.cpp -- example of a three-file program | ||
#include <iostream> | ||
#include "coordin.h" // structure templates, function prototypes | ||
using namespace std; | ||
int main() | ||
{ | ||
rect rplace; | ||
polar pplace; | ||
|
||
cout << "Enter the x and y values: "; | ||
while (cin >> rplace.x >> rplace.y) // slick use of cin | ||
{ | ||
pplace = rect_to_polar(rplace); | ||
show_polar(pplace); | ||
cout << "Next two numbers (q to quit): "; | ||
} | ||
cout << "Bye!\n"; | ||
// keep window open in MSVC++ | ||
/* | ||
cin.clear(); | ||
while (cin.get() != '\n') | ||
continue; | ||
cin.get(); | ||
*/ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// file2.cpp -- contains functions called in file1.cpp | ||
#include <iostream> | ||
#include <cmath> | ||
#include "coordin.h" // structure templates, function prototypes | ||
|
||
// convert rectangular to polar coordinates | ||
polar rect_to_polar(rect xypos) | ||
{ | ||
using namespace std; | ||
polar answer; | ||
|
||
answer.distance = | ||
sqrt( xypos.x * xypos.x + xypos.y * xypos.y); | ||
answer.angle = atan2(xypos.y, xypos.x); | ||
return answer; // returns a polar structure | ||
} | ||
|
||
// show polar coordinates, converting angle to degrees | ||
void show_polar (polar dapos) | ||
{ | ||
using namespace std; | ||
const double Rad_to_deg = 57.29577951; | ||
|
||
cout << "distance = " << dapos.distance; | ||
cout << ", angle = " << dapos.angle * Rad_to_deg; | ||
cout << " degrees\n"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// namesp.cpp -- namespaces | ||
#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; | ||
return total; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// namesp.h | ||
#include <string> | ||
// create the pers and debts namespaces | ||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// new standard header for Microsoft | ||
#include <iostream> | ||
using namespace std; | ||
#pragma once | ||
#ifndef _NEWER_ | ||
#define _NEWER_ | ||
#ifndef RC_INVOKED | ||
#include <exception> | ||
|
||
#pragma pack(push,_CRT_PACKING) | ||
#pragma warning(push,3) | ||
// #pragma push_macro("new") | ||
|
||
// #undef new | ||
|
||
|
||
#if !defined(__CRTDECL) | ||
#if defined(_M_CEE_PURE) | ||
#define __CRTDECL | ||
#else | ||
#define __CRTDECL __cdecl | ||
#endif | ||
#endif | ||
|
||
_STD_BEGIN | ||
|
||
// SUPPORT TYPES | ||
#if !defined(_INC_NEW) || !defined(_MSC_EXTENSIONS) | ||
// handler for operator new failures | ||
#ifdef _M_CEE_PURE | ||
typedef void (__clrcall * new_handler) (); | ||
#else | ||
typedef void (__cdecl * new_handler) (); | ||
#endif | ||
#endif /* !defined(_INC_NEW) || !defined(_MSC_EXTENSIONS) */ | ||
|
||
#ifndef __NOTHROW_T_DEFINED | ||
struct nothrow_t | ||
{ // placement new tag type to suppress exceptions | ||
}; | ||
|
||
extern const nothrow_t nothrow; // constant for placement new tag | ||
#endif /* __NOTHROW_T_DEFINED */ | ||
|
||
// FUNCTION AND OBJECT DECLARATIONS | ||
_CRTIMP2 new_handler __cdecl set_new_handler(_In_opt_ new_handler) | ||
_THROW0(); // establish alternate new handler | ||
_STD_END | ||
|
||
// new AND delete DECLARATIONS (NB: NOT IN std) | ||
void __CRTDECL operator delete(void *) _THROW0(); | ||
#pragma warning (suppress: 4985) | ||
_Ret_bytecap_(_Size) void *__CRTDECL operator new(size_t _Size) _THROW1(_STD bad_alloc); | ||
|
||
#ifndef __PLACEMENT_NEW_INLINE | ||
#define __PLACEMENT_NEW_INLINE | ||
inline void *__CRTDECL operator new(size_t, void *_Where) _THROW0() | ||
{ // construct array with placement at _Where | ||
cout << "New!\n"; | ||
return (_Where); | ||
} | ||
|
||
inline void __CRTDECL operator delete(void *, void *) _THROW0() | ||
{ // delete if placement new fails | ||
cout << "Delete!\n"; | ||
} | ||
#endif /* __PLACEMENT_NEW_INLINE */ | ||
|
||
#ifndef __PLACEMENT_VEC_NEW_INLINE | ||
#define __PLACEMENT_VEC_NEW_INLINE | ||
inline void *__CRTDECL operator new[](size_t, void *_Where) _THROW0() | ||
{ // construct array with placement at _Where | ||
cout << "New!\n"; | ||
return (_Where); | ||
} | ||
|
||
inline void __CRTDECL operator delete[](void *, void *) _THROW0() | ||
{ // delete if placement array new fails | ||
cout << "Delete!\n"; | ||
} | ||
#endif /* __PLACEMENT_VEC_NEW_INLINE */ | ||
|
||
void __CRTDECL operator delete[](void *) _THROW0(); // delete allocated array | ||
|
||
_Ret_bytecap_(_Size) void *__CRTDECL operator new[](size_t _Size) | ||
_THROW1(_STD bad_alloc); // allocate array or throw exception | ||
|
||
#ifndef __NOTHROW_T_DEFINED | ||
#define __NOTHROW_T_DEFINED | ||
_Ret_opt_bytecap_(_Size) void *__CRTDECL operator new(size_t _Size, const _STD nothrow_t&) | ||
_THROW0(); | ||
|
||
_Ret_opt_bytecap_(_Size) void *__CRTDECL operator new[](size_t _Size, const _STD nothrow_t&) | ||
_THROW0(); // allocate array or return null pointer | ||
|
||
void __CRTDECL operator delete(void *, const _STD nothrow_t&) | ||
_THROW0(); // delete if nothrow new fails -- REPLACEABLE | ||
|
||
void __CRTDECL operator delete[](void *, const _STD nothrow_t&) | ||
_THROW0(); // delete if nothrow array new fails -- REPLACEABLE | ||
#endif /* __NOTHROW_T_DEFINED */ | ||
|
||
|
||
#if !defined(_INC_NEW) || !defined(_MSC_EXTENSIONS) | ||
using _STD new_handler; | ||
#endif /* !defined(_INC_NEW) || !defined(_MSC_EXTENSIONS) */ | ||
|
||
#pragma pop_macro("new") | ||
#pragma warning(pop) | ||
#pragma pack(pop) | ||
|
||
#endif /* RC_INVOKED */ | ||
#endif /* _NEWER_ */ | ||
|
||
/* | ||
* Copyright (c) 1992-2007 by P.J. Plauger. ALL RIGHTS RESERVED. | ||
* Consult your license regarding permissions and restrictions. | ||
V5.03:0009 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// newplace.cpp -- using placement new | ||
#include <iostream> | ||
#include <new> // for placement new | ||
const int BUF = 512; | ||
const int N = 5; | ||
char buffer[BUF]; // chunk of memory | ||
int main() | ||
{ | ||
using namespace std; | ||
|
||
double *pd1, *pd2; | ||
int i; | ||
cout << "Calling new and placement new:\n"; | ||
pd1 = new double[N]; // use heap | ||
pd2 = new (buffer) double[N]; // use buffer array | ||
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 " << &pd2[i] << endl; | ||
} | ||
|
||
cout << "\nCalling new and placement new a second time:\n"; | ||
double *pd3, *pd4; | ||
pd3= new double[N]; // find new address | ||
pd4 = new (buffer) double[N]; // overwrite old data | ||
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; | ||
// cin.get(); | ||
return 0; | ||
} |
Oops, something went wrong.