Comparison of C and C++ (Appendix B)

2. C++ comments come in two forms.

* The first is the same as in C:

/* Some comment */

* The other form begins with a pair of slashes and extends to the end of the line:

int itemNo; // item number in the inventory

* Which form is better? why? (the second form)

* What is the problem with the following code?

/* Joe Bob */

* CSE 143 AZ

*/

3. Parameter Passage

* C++ supports both "parameter passage by value" and "parameter passage by reference".

* With passage by value, a "copy" of the actual parameter is sent to the function. Within the function, any change to the formal parameter has no effect on the caller's actual parameter.

* With passage by reference, the "memory address" of the actual parameter is sent to the function. If the function modifies the formal parameter, it is really modifying the caller's actual parameter.

* To pass a variable by reference, e.g.,

void initialize(int& m, int& n)

{

m = 5;

n = 7;

}

* The call is simply

Initialize(alpha, beta);

* What happens to alpha and beta after the call?

* What happens to them if the function prototype is

void initialize(int m, int n) // pass by value

* What happens if the prototype is

void initialize(int* m, int* n) // warning!

4. Declaring "const" Parameters

* In both C and C++, aggregate assignment of one array to another is prohibited.

char myname[20];

myname = "Joe Bob"; // No!

char another[20];

myname = another; // No!

* One way to copy the strings is to write your own function.

void string_copy(char dst[], char src[])...

* To protect src from being accidentally changed:

void string_copy(char dst[], const char src[])...

* In fact, a standard "strcpy" function is provided in <string.h>

5. Declaration statements

* In C++, a declaration within a block is a genuine statement and may appear anywhere an executable statement can.

int main()

{

int alpha;

if (alpha > 0) {

int beta;

...

}

int gamma;

}

* An example in a for-loop:

for (int i = 0; i < max; i++) {

...

6. Enumeration Types

* An enumeration type is an integral type whose constants are meaningful identifiers, not numbers:

enum FlagColor { RED, WHITE, BLUE };

* The constants "red", "white", and "blue" are called "enumerators". You can declare variables of type "FlagColor" and assign them values as follows:

FlagColor star;

star = WHITE;

* The purpose of "enum"s is to encourage the writing of descriptive, self-documenting code.

* In C++, an enumeration is a unique type, distinct from "int".

star = 1; // warning! why?

7. "Struct"s

struct PatientType {

int age;

float height;

};

* A variable declaration of PatientType in C++:

/*struct*/ PatientType joe_bob;

* The "struct" keyword is not necessary in C++.

8. Type Casting

* Do not use type casting unless absolutely necessary.

* Prefix notation:

int1 = (int) unknown;

* Functional notation:

int2 = int(unknown);

* What happens here?

int total;

int count;

float average = total / count;

* One way to fix the problem...

float average = float(total) / float(count);

* Other ways to fix the problem?

9. Symbolic Constants

* Do not use "#define" directives.

#define PI 3.141692653

* Because there is no type checking performed.

* Use this form:

const float PI = 3.141592653;

* Because the declaration is handled by the compiler so that type checking and scope analysis are performed.

10. Static variables

* Initialization of a static variable occurs once only!

void init()

{

static int n = 0; // initialize once!

int m = 3; // initialize every time

}

11. C++ Stream I/O

* To perform I/O, a C++ program must invoke a function that is located in an external library.

* Here, we focus on an innovative I/O concept unique to C++: the "stream".

* You can think of a stream as an endless sequence of characters.

* An "istream" is a data type representing a stream of input characters coming from some I/O device.

* An "ostream" represents an output stream going to some I/O device.

* An example to use stream I/O:

#include <iostream.h>

cout << "hello world\n"; // insertion operator

cerr << "oops\n"; // insertion operator

cin >> int1; // extraction operator

* For details about stream I/O, refer to Appendix C.1.

* The advantages of using stream I/O (over stdio.h) are:

1. Easy to use.

2. Type checking.