Nothing Special   »   [go: up one dir, main page]

C++ Basics

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

C++ Basics

Character Set
• Character set is the combination of English language
(Alphabets and White spaces) and math's symbols (Digits
and Special symbols).

• Character Set means that the characters and symbols


that a C++ Program can understand and accept.

• These are grouped to form the commands, expressions,


words, statements and other tokens for C++ Language.

• C++ program is a sequence of characters.


• There are mainly four categories of the character set:

• 1. Alphabets:

• Alphabets are represented by A-Z or a-z.

• C++ Language is case sensitive so it takes different meaning for


small and upper case letters.

• There are total 26 letters used in C++ programming.

• 2. Digits:

• Digits are represented by 0-9 or by combination of these digits.

• By using the digits numeric constant can be written easily.

• There are total 10 digits used in the C-programming.


• 3. Special Symbols:

• All the keyboard keys except alphabet, digits and white spaces are
the special symbols.

• These are some punctuation marks and some special symbols


used for special purpose.

• There are total 30 special symbols used in the C++ programming.

• Special symbols are used for C-statements like to create an


arithmetic statement +, -, * etc. , to create relational statement <,
>, <=, >=, == etc. , to create assignment statement =, to create
logical statement &&, II etc. are required.

• 4. White Spaces:

• White spaces has blank space, new line return, Horizontal tab
space etc are all used for special purpose.
C++ Tokens
• Tokens are the smallest building block or smallest unit of a C++ program.

• Each individual word and punctuation is referred to as a token in C++.

• These following tokens are available in C++:

• Identifiers

• Keywords

• Constants

• Operators

• Strings
Identifiers
• A C++ identifier is a name used to identify a variable, function, class, module, or
any other user-defined item.

• An identifier starts with a letter A to Z or a to z or an underscore (_) followed by


zero or more letters, underscores, and digits (0 to 9).

• C++ does not allow punctuation characters such as @, $, and % within identifiers.

• C++ is a case-sensitive programming language. Thus, Manpower and manpower


are two different identifiers in C++.

• Here are some examples of acceptable identifiers −

mohd zara abc move_name a_123

myname50 _temp j a23b9 retVal


Keywords
• Keywords are reserved words which have fixed meaning
and its meaning cannot be changed.

• The meaning and working of these keywords are already


known to the compiler.

• C++ has more numbers of keyword than C and those extra


ones have special working capabilities.

• Some of the C++ Keywords are:

• for , if , while, else, main, break, continue, int, float , …


• Constants: Constants are like a variable, except that their
value never changes during execution once defined.

• e.g; 123, abc

• Operators: C++ operator is a symbol that is used to


perform mathematical or logical manipulations.

• e.g; +, -, *, /

• Strings: Strings are objects that signify sequences of


characters.

• e.g; “Good Morning”


Standard output (cout)
• cout: is used to display the output to the standard output device
i.e. monitor. It is associated with the standard C output stream
stdout.

• The cout object in C++ is an object of class ostream.

• cout is used together with the insertion operator, which is written


as << (i.e., two "less than" signs).

• cout << "Output sentence"; // prints Output sentence on screen

• cout << 120; // prints number 120 on screen

• cout << x; // prints the value of x on screen


Insertion Operator (<<)
• The << operator inserts the data that follows it into the stream that
precedes it.

• In the examples above, it inserted the literal string Output


sentence, the number 120, and the value of variable x into the
standard output stream cout.

• Notice that the sentence in the first statement is enclosed in double


quotes (") because it is a string literal, while in the last one, x is not.

• The double quoting is what makes the difference; when the text is
enclosed between them, the text is printed literally; when they are
not, the text is interpreted as the identifier of a variable, and its
value is printed instead.
Standard Input (cin)
• In most program environments, the standard input by default is the keyboard, and the C++
stream object defined to access it is cin.

• cin is used together with the extraction operator, which is written as >> (i.e., two "greater
than" signs).

• This operator is then followed by the variable where the extracted data is stored. For example:

• int age;

• cin >> age;

• The first statement declares a variable of type int called age, and the second extracts from
cin a value to be stored in it.

• This operation makes the program wait for input from cin; generally, this means that the
program will wait for the user to enter some sequence with the keyboard. In this case, note that
the characters introduced using the keyboard are only transmitted to the program when the
ENTER (or RETURN) key is pressed.
Extraction operator (>>)

• The extraction operator >> is the one we usually use for


input, as in:

• cin >> X;

• It gets its name from the idea of extracting data from the
input stream.

• This operator is followed by the variable where the


extracted data is stored.
Cascading of I/O operators
• Multiple insertion operations (<<) may be chained in a
single statement:

• cout << "This " << " is a " << "single C++ statement”;

• This last statement would print the text This is a


single C++ statement.

• Chaining insertions is especially useful to mix literals


and variables in a single statement:

• cout << "I am " << age << " years old and my
zipcode is " << zipcode;
C++ Manipulators
• Manipulators are operators used in C++ for formatting output.

• The data is manipulated by the programmer’s choice of display.

• endl and setw are most commonly used manipulators.

• endl Manipulator:

• This manipulator has the same functionality as the ‘\n’ newline character.

• endl is the line feed operator in C++.

• Its purpose is to point the cursor to the beginning of the next line.

• For example:

• cout << "Exforsys" << endl;

• cout << “Training";


• setw Manipulator:

• This manipulator sets the minimum field width on


output.

• Syntax:

• setw(x)

• Here setw causes the number or string that follows it to


be printed within a field of x characters wide and x is the
argument set in setw manipulator.

• The header file that must be included while using setw


manipulator is iomanip.h.
gets( ) and puts( )

• gets(str): The gets() function reads characters from


stdin (keyboard) and stores them in str until a newline
character or end of file is found.

• puts(str): The puts() function writes or prints


characters on stdout (monitor).

• puts(“Hello world”);
Concept of Data types
• Data type species the different types of data a program can
work with.

• Data types in any of the language mean that what are the
various type of data the variables can have in that particular
language.

• Information is stored in a computer memory with different


data types.

• Whenever a variable is declared it becomes necessary to


define a data type that what will be the type of data that
variable can hold.
Built-in Data types or Primitive Data types
• These data types are built-in or predefined data types and can be used directly by the user
to declare variables.

• Built-in data types are the most basic data-types in C++.

• The term built-in means that they are pre-defined in C++ and can be used directly in a
program.

• Example: int, char , float, bool etc. Primitive data types available in C++ are:

• Integer

• Character

• Boolean

• Floating Point

• Double Floating Point

• Valueless or Void

• Wide Character
• The various built-in data types are:

1. int data type: The int data type is used for integers. Integers are numbers with no
decimal point. For example : 24, -90, 1234, 0 etc.

2. float data type: This data type is used for numbers that have a decimal point which are
commonly called floating-point numbers. For example: 76.45, 12.4, -90.6 etc.

3. double data type: This data type is also used for floating-point numbers. But the
difference is that the range and precision of this data type is greater than that of float. In
other words, it is used for large floating-point numbers.

4. char data type: This data type is used for characters. It can be used for any valid
character in C++ and not just alphabets. Here is a special thing about this data type:
characters are stored in memory using their ASCII codes which are numeric i.e. integers.
Thus, char type is basically the same as int data type.

5. bool data type: bool refers to boolean. This data type has only two values: true and false.

6. void data type: The word void means empty. So it has no value. This is used as return
type for functions that do not return a value.

7. Wide Character: Wide character data type is also a character data type but this data
type has size greater than the normal 8-bit datatype. Represented by wchar_t. It is
generally 2 or 4 bytes long.

You might also like