Free tutorial : Basic Elements of C++


Free tutorial : Basic Elements of C++


Free tutorial : Basic Elements of C++








Overview

    
·         Chapter 2 teaches your students the basics of C++. Learning a programming language is similar to learning to be a chef or learning to play a musical instrument. All three require direct interaction with the tools; in other words, you cannot become proficient by simply reading books on the topics. In this chapter, your students will begin acquiring a fundamental knowledge of C++ by learning about data types, functions, identifiers, assignment statements, arithmetic operations, and input/output operations.  They will then write and test programs using these concepts to verify their knowledge of the material.
·          
·          

Objectives


In this chapter, the student will:
·         Become familiar with the basic components of a C++ program, including functions, special symbols, and identifiers
·         Explore simple data types
·         Discover how to use arithmetic operators
·         Examine how a program evaluates arithmetic expressions
·         Learn what an assignment statement is and what it does
·         Become familiar with the string data type
·         Discover how to input data into memory using input statements
·         Become familiar with the use of increment and decrement operators
·         Examine ways to output results using output statements
·         Learn how to use preprocessor directives and why they are necessary
·         Explore how to properly structure a program, including using comments to document a program
·         Learn how to write a C++ program
·          

Teaching Tips


The Basics of a C++ Program
·          
1.      Introduce this chapter by explaining that a C++ program is essentially a collection of one or more subprograms, called functions. Note that although many functions are predefined in the C++ library, programmers must learn how to write their own functions to accomplish specific tasks.
·          
2.      Emphasize that every C++ program must have a function called main. Use Example 2-1 to illustrate a basic main function.
·          
3.      Define the terms syntax rules and semantic rules as they relate to a programming language and explain the difference between the two.
·          

Teaching

Tip

Emphasize that compilers check for syntax but not semantic errors. Give an example of each type of error.
·          
·          
·         Comments
·          
1.      Use the program in Example 2-1 to describe the use and importance of comments. Stress that comments are for the reader, not for the compiler.
·          
·         Special Symbols
·          
1.      Explain that the C++ programming language consists of individual units called tokens, and these are divided into special symbols, word symbols, and identifiers.
·          
2.      Go over some of the special symbols in C++, including mathematical symbols, punctuation marks, the blank symbol, and double characters that are regarded as a single symbol.
·          
·         Reserved Words (Keywords)
·          
1.      Discuss the word symbols, or keywords, used in C++, using Appendix A as a guide.  Emphasize that C++ keywords are reserved and cannot be redefined for any other purpose with a program. 
·          
·         Identifiers
·          
1.      Define the term identifier as a name for something, such as a variable, constant, or function. 
·          
2.      Discuss the rules for naming identifiers in C++. Also note that C++ is a case-sensitive language.
·          

Teaching

Tip

Discuss the difference between C++ conventions and rules. For example, it is a rule that a mathematical symbol cannot be used in an identifier name. However, it is a convention to begin an identifier with a lowercase letter.
·          
·          
·         Whitespaces
·          
1.      Explain that whitespaces (which include blanks, tabs, and newline characters) are used to separate special symbols, reserved words, and identifiers.
·          
          
Data Types
·          
1.      Explain that C++ categorizes data into different types in order to manipulate the data in a program correctly. Although it may seem cumbersome at first to be so type-conscious, emphasize that C++ has these built-in checks to guard against errors.
·          

Teaching

Tip

Explain that C++ is called a strongly-typed language because it checks for operations between inconsistent data types. This results in more robust and error-free programs. Demonstrate how C++ checks for data types with a simple program that attempts to add a string and a numeric value.
   
·          
2.      Mention that C++ data types fall into three categories: simple data types, structured data types, and pointers. Only the first type is discussed in this chapter.   
·          
·         Simple Data Types
·         
1.      Using Figure 2-2, describe the three categories of simple data types in C++: integral, floating-point, and enumeration.
·           
2.      Using Figure 2-3, mention the nine categories of integral data types. Explain why C++   (and many other languages) has so many categories of the same data type. In addition, discuss the rules involving the use of integral types.
·          
3.      Explain the purpose of the bool data type.
·          
4.      Discuss the char data type, including its primary uses. Mention commonly used ASCII characters and their predefined ordering. Explain that a char data type is enclosed in single quotation marks, and note that only one symbol may be designated as a character.
·          
·         Floating-Point Data Types
·          
1.      Use Table 2-3 to explain how C++ represents real, or floating-point, numbers. Use Figure 2-4 to mention the three categories of data types to represent real numbers (float, double, and long double), and explain when to use each type.
·          
2.      Define the terms precision, single precision, and double precision.
·          

Teaching

Tip

Demonstrate how to find the values of float and double on a particular system by running the program with the header file in Appendix F. Encourage students to try running this program on their own computers and comparing the results.
·          
·          
·          


·       Quick Quiz 1
·          
  1. What is an enumeration type?
·         Answer: C++’s method for allowing programmers to create their own simple data types
·          
  1. The maximum number of significant digits in a number is called the ____________________.
·         Answer: precision
·          
3.      The data type ____________________ has only two values: true and false.
·                    Answer: bool
·          
  1. The ____________________ data type, the smallest integral data type, is used to represent integral numbers between -128 and 127.
·                    Answer: char
·          
·          
Arithmetic Operators and Operator Precedence
·          
1.      Discuss the five arithmetic operators in C++ that are used to manipulate integral and floating-type data types.
·          
2.      Explain the difference between unary and binary operators.
·          
·         Order of Precedence
·          
1.      Review operator precedence rules, as C++ uses these rules when evaluating expressions. Example 2-5 illustrates the use of parentheses to override the order of operator precedence.
·          
·          
Expressions
·          
1.      This section discusses integral and floating-point expressions in detail. Use Examples 2-6 and 2-7 to clarify how C++ processes expressions.

Mixed Expressions

1.      Discuss the two rules for evaluating mixed expressions and illustrate these rules in practice using Example 2-8.........
















Free tutorial : Basic Elements of C++



1 commentaire: