Hi there. I've been working through Bjarne Stroustrup's "The C++ Programming Language" (2nd edition - I know I should really get a new copy but this is a library book!), and had a few questions about one of his simpler questions. In Chapter 2, when talking about Declarations and Constants, he lists a set of declarations, some of which are definitions as well. In an exercise at the end of the chapter, he challenges the reader to go back and re-write the list, this time changing all of the defined declarations to just declarations and changing all of the non-defined ones to have a definition.
I have completed this task, hopefully mostly correctly, but there were a few parts I got stuck on. I would appreciate it if anyone could take a quick look through my list, check if there are any of the original list I mis-allocated, and then check my changes - including most specifically how to declare but not define a typedef
, and if my enum
declaration-not-definition is right. Thanks very much to anyone who helps. I apologise since this isn't strictly a direct code question - i.e. there's no compilable code here, it's more... I dunno. But it does have code in it, so I hope that's okay.
// Original:
/*
char ch; // Definition.
int count = 1; // Definition.
char* name = "Njal"; // Definition.
struct complex { float re, im; }; // Definition.
complex cvar; // Definition.
extern complex sqrt(complex); // Declaration, NOT definition.
extern int error_number; // Declaration, NOT definition.
typedef complex point; // Definition.
float real(complex* p) { return p->re; } // Definition.
const double pi = 3.1415926535897932385; // Definition.
struct user; // Declaration, NOT definition.
template<class T> abs(T a) { return a < 0 ? -a : a; } // Definition.
enum beer { Carlsberg, Tuborg, Thor }; // Definition.
*/
// Definitions/Declarations switched:
/*
extern char ch;
extern int count;
extern char* name;
struct complex;
extern complex cvar;
complex sqrt(complex in) { // Yes, my maths may be wrong here. Doing the actual maths from memory.
complex out;
out.re = (in.re * in.re) - (in.im * in.im);
out.im = (in.re * in.im)*2;
return out;
}
int error_number;
// No idea how to declare but not define a typedef!
float real(complex* p);
extern const double pi;
struct user { string name; int age; char gender; }; // Lets assume we include <string>, and yes, using int for age *might* be a bit wasteful, but meh.
template<class T> abs(T a);
extern enum beer; // Not sure if this is right.
*/