[go: up one dir, main page]

0% found this document useful (0 votes)
19 views84 pages

C++ Basics

C++ BASICS

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views84 pages

C++ Basics

C++ BASICS

Uploaded by

ezekiel nyamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 84

C++ BASICS

Ezekiel Ogakhan Nyamu


History of C++:
The C++ programming language was created by Bjarne Stroustrup
and his team at Bell Laboratories (AT&T, USA) to help implement
simulation projects in an object- oriented and efficient way.
 C++ is a superset of C because; any valid C program is valid C++
program too but not the vice versa is not true.
 C++ can make use of existing C software libraries with major
addition of “Class Construct”.
 This language was called “C with classes” and later in 1983, it was
named “C++” by Rick Mascitii. As the name C++ implies, C++
was derived from the C programming language: ++ is the increment
operator in C.
 C++ systems
 Program-development environment
 Language
 C++ Standard Library
 C++ program names extensions
 .cpp
 .cxx
 .cc
 .C
Why Use C++
 C++ is one of the world's most popular programming languages.
 C++ can be found in today's operating systems, Graphical User Interfaces,
and embedded systems.
 C++ is an object-oriented programming language which gives a clear structure to
programs and allows code to be reused, lowering development costs.
 C++ is portable and can be used to develop applications that can be adapted
to multiple platforms.
 C++ is fun and easy to learn!
 As C++ is close to C# and Java, it makes it easy for programmers to switch to C++
or vice versa
C# (pronounced "C-sharp") is a modern, object-oriented programming language developed by
Microsoft. It was introduced in the early 2000s as part of the .NET framework and is widely used for
building a variety of applications, including desktop software, web services, cloud-based
applications, games, and mobile apps.
•Translating a C++ program
 C++ Install IDE: download and Install Turbo C++ or Install
Codeblocks
 The program execution process consists of the following steps:

Installation Process
Example Using an IDE
Most IDEs like Visual Studio, Code::Blocks, or CLion have a Build and
Run or Run button that automates the entire process:
1.Clicking "Build" compiles the code and checks for errors.
2.Clicking "Run" runs the last successful build.
3.Clicking "Build and Run" compiles and runs in one step, launching the
program if there are no build errors.
•General Structure of C++ Program.
• Different programming languages have their own format of
coding.
• The basic components of a C++program are:
• Comments or Documentation Section
• Pre-processor Directives (Linker Section):
• Definition
• Global Declaration
• main ( ) function
• Declarations
• Statements
Output Operator:

The statement cout <<”Hello, world” displayed the string with in quotes on the screen.
The identifier cout can be used to display individual characters, strings and even
numbers. It is a predefined object that corresponds to the standard output stream.
Stream just refers to a flow of data and the standard Output stream normally flows to
the screen display. The cout object, whose properties are defined in iostream.h
represents that stream. The insertion operator << also called the ‘put to’ operator
directs the information on its right to the object on its left.

Return Statement:

In C++ main ( ) returns an integer type value to the operating system. Therefore every main ( ) in C++ should
end with a return (0) statement, otherwise a warning or an error might occur.
Input Operator:
The statement cin>> number 1; is an input statement and
causes The program to wait for the user to type in a number.
The number keyed in is placed in the variable number1. The
identifier cin is a predefined object in C++ that corresponds to
the standard input stream. Here this stream represents the key
board.
The operator >> is known as get from operator. It extracts
value from the keyboard and assigns it to the variable on its
right.
comments
Parts of C++
What will be the output
•C++ Character Set:
•Character Set means the valid set of characters that a language can
recognizes.
 The character set of C++ includes the following:
C++ Tokens:
The smallest individual unit is known as token.
 These elements help us to construct statements, definitions,
declarations, and so on,which in turn helps us to construct
complete program.
 Tokens used in C++ are:
Identifier
Reserved Keywords
Constants or Literals
Punctuators
Operators
•C++ Identifier:
•identifiers are the unique names assigned to variables, functions, classes, structs,
or other entities within the program
 It contains letters, digits and underscore.
 C++ does not allow punctuation characters such as @, $, and % within

 C++ is a case sensitive; it treats uppercase and lowercase characters differently.


 The following are some valid identifiers:
 Adisesha
 time_table
 s2e2r3
 _dos _HJI3_JK
C++ Identifier:
Rules to be followed while creating identifiers.
 Identifiers are a sequence of characters which should begin with the
alphabet either from A-Z (uppercase) or a-z (lowercase) or _
(underscore).
 C++ treats uppercase and lowercase characters differently. For
example, DATA is not same as data.
 No Special character is allowed except underscore “_”.
 Identifier should be single words i.e. blank spaces cannot be included
in identifier.
 Reserved Keywords should not be used as identifiers.
 Identifiers should be of reasonable length.
C++ Keywords:
•Keyword is a predefined word that gives special meaning to the
complier. The programmer is not allowed to change its meaning.
 These words have predefined functions, so you cannot use them as
identifiers (like variable names, function names, etc.).
 Example:
• cin, cout, for, if, else, ,
• do, float, while, switch etc.,
There are keywords in C++ as mentioned below
•Literals: A Literals/ constant are identifiers whose value does not
change during program execution.

•In C++, literals are fixed values written directly in the code. They represent
constant values of various data types, such as integers, floating-point
numbers, characters, strings, and Boolean values

 Constants are sometimes referred to as literal.


 A constant or literal my be any one of the following:
 Integer Constant
 Floating Constant
 Character Constant
 String Constant
•Integer Constant:
 An integer constant is a whole number, which can be either positive or
negative.
 They do not have fractional part or exponents.
 We can specify integer constants in:
 Decimal Integer Constant int a = 120; //Decimal Constant
 Octal Integer Constant int b = 0374; //Octal Constant
 Hexadecimal Integer Constant int c = -0XABF; //Hexadecimal
Constant
 Unsigned Constant unsigned d = 328u; //Unsigned
value
•Floating Point Constant:
 Floating point constants are also called as “real constants”.
 These values contain decimal points (.) and can contain exponents.
 They are used to represent values that will have a fractional part
and can be represented in two forms (i.e. fractional form and
exponent form)
 We can specify Floating Point constants in:
 float a=23.46 // equal to 23.46 x 100 = 23.46 x 1 = 23.46
 float b=26.126
•Character constants: are specified as single character enclosed
in pair of single quotation marks.
 For example char ch = ‘P’;
 There are certain characters used in C++ which represents character

constants called as escape sequence which starts with a back slash (


\ ) followed by a character.
String Constants:
 A string constant consists of zero or more character enclosed by double
quotation marks (“ ”).
 Multiple character constants are called string constants and they are treated as
an array of char.
 By default compiler adds a special character called the “Null Character” (\0) at
the end of the string to mark the end of the string.
 Example:
 char str[25] = “Hello Adisesha” ;
 This is actually represented as char str[25] = “Hello Adisesha\0” in the memory
There are two simple ways in C++ to define constants

• Using #define preprocessor.
• Using const keyword.

The #define Preprocessor


• Following is the form to use #define preprocessor to
define a constant −
#define identifier value
#define LENGTH 10
#define WIDTH 5
The const Keyword
You can use const prefix to declare constants with a
specific type as follows −

const type variable = value;


const int LENGTH = 10;
const int WIDTH = 5;
C++ Operators
C++ Operators: Operators are used to perform operations on variables and
values.
 Example: int x = 100 + 50;
 C++ Operators Types:
 C++ divides the operators into the following groups.
 Arithmetic operators
 Assignment operators
 Comparison operators
 Logical operators
 Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical
operations.
Assignment Operators
Assignment operators are used to assign values to variables
Relational Operator
 Comparison operators are used to compare two values.

 The return value of a comparison is either true (1) or false (0).


Logical Operators
Logical operators are used to determine the logic between variables
or values
Bitwise Operators
A Bitwise operators are used in bit level programming
Classification of Operators
Operators may also be classified on the number of operands they act on
either:
 Unary Operators
 Example: a++, a+1

 Binary Operators
 Example: x = x – 10;

 Ternary Operators
 Example: x = (a>b) ? a:b;
•Special Operator :
 An expression is a combination of opcode and operand.

 Some special operators used in C++ programming are:


Punctuators
Punctuators in C++ have syntactic and semantic meaning to the
compiler.
• General Structure of C++ Program.
• Different programming languages have their own format of coding.
• The basic components of a C++program are:
• Comments or Documentation Section
• Pre-processor Directives (Linker Section):
• Definition
• Global Declaration
• main ( ) function
• Declarations
• Statements
•General Structure of C++ Program
 Different programming languages have their own format of coding.
 The basic components of a C++program are:
•C++ Comments
•Comments can be used as non-executable statement to explain C++ code, and to
make
•it more readable.
 It can also be used to prevent execution when testing alternative code.

 Comments can be:

 singled-lined: Single-line comments start with two forward slashes (//).

• Example:
 multi-lined.: Multi-line comments start with /* and ends
with */. Example:
Linker Section in C++
•The linker is a program that makes executable files.
 The linker resolves linkage issues, such as the use of symbol hash (#) defined in

one translation unit.


 The C++ preprocessor directives programming skills that should be acquired:

 Able to understand and use #include.


 Able to understand and use #define.
 Able to understand and use macros and inline functions.
Input & Output Operators
Input & Output Operators
• The input output operations are done using library functions cin
and cout objects of the class iostream.
• Using the standard input and output library, we will able to
interact with the user by printing message on the screen and
getting the user’s input from the keyboard.
• A stream is an object where a program can either insert/extract
characters to/from it.
• The standard C++ library includes the header file iostream,
where the standard
• input and output stream objects are declared.
•Library Functions in C++
•Built-in functions are also known as library functions.
 We need not to declare and define these functions as they are already

written in the
• C++ libraries such as iostream, cmath etc
•Library Functions in C++
•Built-in functions for String manipulation are:
Main function section in C++
•The starting point of all C++ programs is the main function.
 This function is called by the operating system when your program is executed

by the computer.
int main( )
{
cout << "Hello World" << endl; //Function body section
return 0;
}
 { signifies the start of a block of code, and } signifies the end.
Data Types in C++:
•Data Types can be defined as the set of values, which can be stored in a variable
along
•with the operations that can be performed on those values.
 C++ defines several types of data and each type has unique characteristics.

 C++ data types can be classified as:

 The fundamental data type(built-in data)


 Derived Data type
 User-defined data type
Data Types:
 C++ defines several types of data and each type has unique
characteristics.
 C++ data types can be classified as:
Basic Data Types:char type
•The char type: It is character data type to store any character from the basic
character set.
 Characters are enclosed in single quotation marks (‘). ‘A’, ‘a’, ‘b’, ‘9’, ‘+’ etc.

are
• character constants.
 When a variable of type char is declared, the compiler converts the character to its
equivalent ASCII code.
 A character is allocated only 1 byte (8 bits) of memory space.
 The general form of a character declaration is: char variable_list;
 Example: char alpha=’a’;
Basic Data Types:float type
•The float type: This represents the number with fractional part i.e. real numbers.
 The float type is used to store real numbers.

 Float is allocated 4 bytes (32 bits) of memory space.

 Number such as 1.8, 4.5, 12e-5 and -9.66 are all floating-point numbers.

 The range of numbers we can store from -34e-38 to 3.4e38.

 The general form of a float declaration is: float variable_name;

 Example:

 float a=5.5;
Basic Data Types:double type
•The double type: The double and float are very similar.
 The float type allows you to store single precision floating point numbers,

while the
• double keyword allows you to store double precision floating point numbers.
 Its size is typically 8 bytes of memory space. The range of numbers we

can store are from -1.7e308 to 1.7e308.


 The general form of a double declaration is: double variable_list;

 Example: double a = 5.5e-7; //a is equivalent to 5.5x10-7

 float a=5.5;
Basic Data Types:bool type
•The bool type: The bool type has logical value true or false.
 The identifier true has the value 1, and the identifier false has the value 0.

 The general form of a bool declaration is: bool variable_name;

 Example:

 bool legal_age=true;
 The statement legal_age= (age>=21);
 assigns the value true if age is greater than or equal to 21 or else it
returns the value false
Basic Data Types:void type
•The void type : The void data type has no values and no operations.
 In other words, both the set of values and set of operations are empty.

 In this declaration the main function does not return any value.

 Example:

 void main( )

{
Statements;
}
•Basic Data Types:
 The data type specifies the size and type of information the variable
will store.
Derived data types:
•These data types are constructed using simple or fundamental data types.
 Derived data types emphasize on structuring of a group of homogeneous (same

type)
• data items.
 These data types includes:

 Arrays
 Functions
 Pointers
 References
User defined data types:
These data types are constructed by user using simple or fundamental data types.
 User defined data types emphasize on structuring of a group of homogeneous

(same
• type) or heterogeneous (different type) data items.
 Some user defined data types include

 Enumerated
 Structure
 Union
 Class

You might also like