The fundamentals of C++

The Fundamentals of C++

#include
using namespace std;
int main() {
// Extract length and width
cout << « Rectangle dimensions: « ; float Length; float Width; cin >> Length >> Width;

// Compute and insert the area

float Area = Length * Width;

cout << « Area =  » << Area <<  » = Length  »
<< Length <<  » * Width  » << Width << endl;
return 0;
}

Comments

Allow prose or commentary to be included in program
Importance
Programs are read far more often than they are written
Programs need to be understood so that they can be maintained
C++ has two conventions for comments
// single line comment (preferred)
/* long comment */ (save for debugging)
Typical uses
Identify program and who wrote it
Record when program was written
Add descriptions of modifications

fundamental C++ objects

C++ has a large number of fundamental or built-in object types
The fundamental object types fall into one of three categories
Integer objects
Floating-point objects
Character objects

Integer objects types

The basic integer object type is int
The size of an int depends on the machine and the compiler
On PCs it is normally 16 or 32 bits
Other integers object types
short: typically uses less bits
long: typically uses more bits
Different types allow programmers to use resources more efficiently
Standard arithmetic and relational operations are available for these types

Integer constants

Integer constants are positive or negative whole numbers
Integer constant forms
Decimal
Octal (base 8)
Digits 0, 1, 2, 3, 4, 5, 6, 7
Hexadecimal (base 16)
Digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a , b, c, d, e, f, A, B, C, D, E, F
Consider
31 oct and 25 dec

Character object types

Character type char is related to the integer types
Characters are encoded using a scheme where an integer represents a particular character
ASCII is the dominant encoding scheme
Examples
‘ ‘ encoded as 32 ‘+’ encoded as 43
‘A’ encoded as 65 ‘Z’ encoded as 90
‘a’ encoded as 97 ‘z’ encoded as 122

Appendix A gives the complete ASCII character set
Arithmetic and relational operations are defined for characters types
‘a’ < ‘b’ is true ‘4’ > ‘3’ is true
‘6’ <= ‘2’ is false

character constant

Explicit (literal) characters within single quotes
‘a’,’D’,’*’
Special characters – delineated by a backslash \
Two character sequences (escape codes)
Some important special escape codes
\t denotes a tab w \n denotes a new line
\\ denotes a backslash w \’ denotes a single quote
\ » denotes a double quote
‘\t’ is the explicit tab character, ‘\n’ is the explicit new line character, and so on

Literal string constants

A literal string constant is a sequence of zero or more characters enclosed in double quotes
« We are even loonier than you think »
« Rust never sleeps\n »
« Nilla is a Labrador Retriever »
Not a fundamental type

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *