• Tidak ada hasil yang ditemukan

Sams Teach Yourself.

N/A
N/A
MANMEET KAUR

Academic year: 2023

Membagikan "Sams Teach Yourself."

Copied!
793
0
0

Teks penuh

2. Use the command prompt to navigate to the path to the executable (usually in the Debug directory of the project folder). As you can see, standards conformance helps to compile and run the code snippet in Listing 1.1 on multiple platforms or operating systems—a prerequisite is the availability of standards-compliant C++ compilers.

FIGURE 1.1 A simple “Hello  World” C++
FIGURE 1.1 A simple “Hello World” C++

3Output ▼

You learned about the keyword auto, where you let the compiler decide the data type for you based on the initialization value of the variable. You also learned about the different types of constants and the use of the most important among them using the keywords const, constexpr and enum. The initial value is just the contents of the memory location reserved for the variable.

Note that this is not a problem with the unsigned card, rather with your use of it. The dictionary definition of an array comes very close to what we want to understand. According to Merriam Webster, an array is "a group of elements that form a complete unit, for example, a series of solar panels."

In the previous lines of code, you declared an array named myNumbers that contains five elements of type int—that is, an integer—all initialized to the value 0.

TABLE 3.2  Major C++ Keywords
TABLE 3.2 Major C++ Keywords

Recipes

Dynamic arrays are introduced briefly later in this lesson and are discussed in detail in Lesson 17, "STL Dynamic Array Classes". Each book is an element of the array and the rack is related to the memory reserved to store this collection of books as shown in Figure 4.1.

Note that the array now has two null terminators, but it is the first one that results in the display of the string in line 11 being truncated to just "Hello". This null terminator has been inserted by cin at the end of the user's input. Determining the length of the string is done by calling length() on it in line 27.

You also learned that C-style strings are a special case of character strings where the end of the string is indicated by the null terminator '\0'. Q Does the length of the string include null termination at the end. This is essential so that it can accommodate the null character at the end of the largest string.

If "Hello World" were the largest string your char array would ever hold, then the length of the string must be characters.

5Output ▼

Less than (<) Evaluates to true if one operand is less than the other (op1 < op2), else evaluates to false Greater than (>) Evaluates to true if one operand is greater than. Greater than or equal to (>=) Evaluates to true if one operand is greater than or equal to another, other evaluates to false. As Table 5.1 shows, the result of a comparison operation is always true or false, in other words a bool.

The fact that the result of the equality and relational operators is binary makes them perfectly suited for use in statements that help make decisions and as loop condition expressions that ensure that the loop only executes as long as the condition evaluates to true. A logical OR evaluates to true when at least one of the operands evaluates to true, as shown in Table 5.4. The exclusive OR (abbreviated XOR) operation is slightly different from logical OR in that it evaluates to true when either operand is true, but not both, as shown in Table 5.5.

You need such logical constructs in programming where the result of two operations is used in a logical context to decide the next flow of your program.

TABLE 5.1  Relational Operators
TABLE 5.1 Relational Operators

5Analysis ▼

LISTING 6.1 Multiplying or adding two integers based on user input 0: #include . Listing 6.4 is a program that asks the user for the day of the week and then tells the day what that day is called by using if..else nested constructs. Listing 6.5 is the equivalent of the program in Listing 6.4 that shows how the days of the week are named and also uses enumerated constants.

This program using switch-case produces exactly the same result as Listing 6.4 using the if-else-if construct. LISTING 6.6 Using the conditional operator (?:) to find the maximum of two 0 numbers: #include . LISTING 6.7 Asking the user if he wants to repeat calculations using goto 0: #include .

LISTING 6.9 Using do...while to repeat the execution of a block of code 0: #include .

TABLE 5.6  Compound Assignment Operators
TABLE 5.6 Compound Assignment Operators

6Output ▼

6Analysis ▼

Thus, the effect of continuing in a while, do..while, or for loop is that it results in the loop condition being reevaluated and the loop block re-entered if the condition evaluates to true. Usually programmers expect all the code in a loop to be executed when the conditions of the loop are met. Remember that while, do..while and for loops have a condition expression that results in the loop exiting when the condition evaluates to false.

The for loop in line 5 differs from that in Listing 6.11 in that it is an infinite for loop that does not contain any conditional expressions and is evaluated on each iteration of the loop. DO use do…while when the logic in the loop needs to be executed at least once. Since -1 is not zero, the while condition evaluates to true and the loop executes.

If you want the loop to execute only for positive numbers, write an expression while(Integer>0).

2.Write a nested loop similar to Listing 6.14 that adds elements of two arrays, but in reverse order. 3.Write a program that displays Fibonacci numbers similar to Listing 6.16, but asks the user how many numbers she wants to calculate. So far in this book, you have seen simple programs where all programming effort is contained in main().

The larger and more complex your program becomes, the longer the contents of main() will be, unless you choose to structure your program using functions. They allow you to divide the contents of your application into logical blocks that are invoked sequentially. Consider a program that asks the user to enter the radius of a circle and then calculates the perimeter and area.

Another way is to split this application into logical blocks: specifically two that calculate area and perimeter respectively given radius.

7Output ▼

Without the function prototype, upon reaching lines 16 and 19 in main() the compiler would not know what the terms Area and Circle are. Function prototypes tell the compiler that Area and Circle are functions; they take a parameter of type double and return a value of type double. The compiler then recognizes these statements as valid, and the job of linking the function call to its implementation and ensuring that program execution activates them is the job of the linker.

In this case, Area() must return a value because the function has been declared as one that returns as double. The statement block contains statements within open and closed brackets ({...}) that are executed when the function is called. In such cases, when you write a function that calculates the surface area of ​​the cylinder, you specify at least two parameters in the parameter list of the function declaration.

Note that the function prototype on line 3 declares the SayHello() function as having a void return value - that is, SayHello() does not return a value.

FIGURE 7.2 A cylinder.
FIGURE 7.2 A cylinder.

7Analysis ▼

Say you need to write an application that calculates the area of ​​a circle and the area of ​​a cylinder. The other function that calculates the area of ​​the cylinder needs the height of the cylinder in addition to the radius of the cylinder. LISTING 7.7 Use an overloaded function to calculate the area of ​​a circle or a cylinder 0: #include .

The definitions of the overloaded functions are in lines 34-44, where the two functions determine the area of ​​a circle given the radius and the surface area of ​​a cylinder given the radius and height, respectively. Interestingly, since the area of ​​a cylinder consists of the area of ​​the two circles it contains (one at the top and one at the bottom) in addition to the area of ​​the sides, the overloaded version for the cylinder was able to reuse the Area ( ) for the circle, as shown in line 43. The return type has been changed to void since the function no longer supplies the calculated area as a return value, but rather as an output parameter by reference.

When the microprocessor encounters CALL, it stores the position of the instruction to be executed after the function call on the stack and jumps to the memory location contained in the CALL instruction.

7Understanding the Stack

You learned about the auto keyword in Lesson 3, "Using Variables, Declaring Constants." It allows you to leave the deduction of a variable's type to the compiler, which does it based on the initialization value assigned to the variable. The compiler infers the return type based on a return expression that uses a double. LISTING 7.12 Using lambda functions to display the elements in an array and sort them 0: #include .

One of the biggest advantages of C++ is that it allows you to write high-level applications that are abstracted from the machine as well as those that work close to the hardware. This garbage value is especially dangerous in the case of a pointer because the value of the pointer is expected to contain an address. Listing 8.1 demonstrates the concept of the memory address of an integer variable used to hold the value it contains.

Listing 3.4 in Lesson 3 uses sizeof() to show that the size of an integer is 4 bytes (on my system, using my compiler).

Gambar

FIGURE 1.1 A simple “Hello  World” C++
TABLE 3.2  Major C++ Keywords
FIGURE 4.1 Books on a shelf:
FIGURE 4.2 Organization of an  array of five  integers,  myNumbers , in  memory.
+7

Referensi

Dokumen terkait