0% found this document useful (0 votes)
6 views

c++ file

Uploaded by

Akshat Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

c++ file

Uploaded by

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

Practical file

SUBMITTED BY :
HARSHIT KHANNA
Introduction to
Programming
An organized list of instructions that, when executed,
causes the computer to behave in a predetermined
manner. Without programs, computers are useless.
A program is like a recipe. It contains a list of ingredients
(called variables) and a list of directions (called
statements) that tell the computer what to do with the
variables. The variables can represent numeric data, text,
or graphical images.
There are many programming languages -- C, C++, Pascal,
BASIC, FORTRAN, COBOL, and LISP are just a few. These
are all high-level languages. One can also write programs
in low-level languages called assembly languages,
although this is more difficult. Low-level languages are
closer to the language used by a computer, while high-
level languages are closer to human languages.
Categories of Programming
Language
Programming languages fall into two fundamental
categories low and high-level languages. Low-level languages
are machine-dependent; that is, they are designed to be run
on a particular computer. In contrast, high-level languages
(for example, COBOL and BASIC) are machine-independent
and can be run on a variety of computers.

The hierarchy of programming languages contain various


types of programming languages. Through the first four
decades of computing, programming languages evolved in
generations. The first two generations were low-level and
the next two high-level generations of programming
languages.

The higher-level languages do not provide us greater


programming capabilities, but they do provide a more
sophisticated programmer/computer interaction. In short,
the higher the level of the language, the easier it is to
understand and use. For example, in a fourth-generation
language, you need only instruct the computer system what
to do, not necessarily how to do it.
WHAT IS C++?
C++ is a versatile programming language, evolving from C, renowned for
its efficiency and object-oriented features. Developed by Bjarne
Stroustrup, it offers encapsulation, inheritance, and polymorphism,
facilitating modular and reusable code organization. As a compiled
language, it translates source code into machine code for execution,
ensuring high performance. Its Standard Template Library (STL) provides
a rich collection of classes and functions for various tasks like data
structures and algorithms. C++ enables low-level memory manipulation,
inline functions, and cross-platform compatibility. Widely used in game
development, system software, and embedded systems, it remains a
cornerstone for applications requiring performance and flexibility.

The C++ programming languages is an extension of C that was


developed by Bjarne Stroustrup in the early 1980s at Bell Laboratories.
C++ provides many features that "spruce up" the C language, but more
importantly, it provides capabilities for object-oriented programming. A
computer cannot understand our language that we use in our day to
day conversations, and likewise, we cannot understand the binary
language that the computer uses to do its tasks. It is, therefore,
necessary for us to write instructions in some specially defined
language like C++ which is like natural language and after conversing
with the help of compiler the computer can understand it.
Significant Language Features
Object-oriented programs are easier to understand, correct and
modify. Many other object-oriented languages have been
developed, including most notably, Smalltalk. The best features
of C++ are:
C++ is a hybrid language -it is possible to program in either a C-
like style, an object-oriented style, or both.
C++ programs consist of pieces called class function. you can
program each piece you need to form a C++ program. The
advantage of creating your own functions and classes is that
you will know exactly how they work. You will be able to
examine the C++ code.

Areas of Application :
 For Develop Graphical related application like a computer and mobile
games.

 To evaluate any kind of mathematical equation use C++ language.

 C++ Language is also used for design OS. Like window xp .

 Google also use C++ for Indexing

 Few parts of Apple OS X are written in C++ programming language.

 Internet browser Firefox is written in C++ programming language


 All major applications of Adobe systems are developed in C++
programming language. Like Photoshop, Image Ready, Illustrator, and
Adobe Premier.

 Some of the Google applications are also written in C++, including


Google file system and Google Chromium.

 C++ is used for design database like MySQL.

C++ COMPILER
A C++ Compiler is itself a computer program which’s the only
job is to convert the C++ program from our form to a form the
computer can read and execute. The original C++ program is
called the “source code”, and the resulting compiled code
produced by the compiler is usually called an “object file”.
Before compilation, the preprocessor performs preliminary
operations on C++ source files. A preprocessed form of the
source code is sent to the compiler.
After compilation stage object files are combined with
predefined libraries by a linker, sometimes called a binder, to
produce the final complete file that can be executed by the
computer. A library is a collection of pre-compiled “object code”
that provides operations that are done repeatedly by many
computer programs.
C++ CHARACTER SET
The character set is a set of valid characters that a language can
recognize

TOKENS
A token is a group of characters that logically belong together. The
programmer can write a program by using tokens. C++ uses the
following types of tokens. Keywords, Identifiers, Literals, Punctuators,
Operators.

1. Keywords : These are some reserved words in C++ which have


predefined meaning to compiler called keywords.
2. Identifiers :
Symbolic names can be used in C++ for various data items used by
a programmer in his program. A symbolic name is generally known
as an identifier. The identifier is a sequence of characters taken
from C++ character set. The rule for the formation of an identifier
are:
 An identifier can consist of alphabets, digits and/or underscores.

 It must not start with a digit

 C++ is case sensitive that is upper case and lower-case letters are

considered different from each other.


 It should not be a reserved word

3. Literals :
Literals (often referred to as constants) are data items that never
change their value during the execution of the program. The
following types of literals are available in C++.
 Integer-Constants
 Character-constants
 Floating-constants
 Strings-constants
Integer Constants: Integer constants are the whole number
without any fractional part. C++ allows three types of integer
constants.
Decimal integer constants: It consists of a sequence of digits and
should not begin with 0 (zero). For example, 124, - 179, +108.
Octal integer constants: It consists of a sequence of digits starting
with 0 (zero). For example. 014, 012.
Hexadecimal integer constant: It consists of a sequence of digits
preceded by ox or OX.
BASIC SYNTAX
BASIC DATA TYPES
C++ supports many data types. The built-in or basic data types
supported by C++ are an integer, floating point, and character.
These are summarized in table along with description and
memory requirement
VARIABLES
It is a location in the computer memory which can store data
and is given a symbolic name for easy reference. The variables
can be used to hold different values at different times during
the execution of a program.
To understand more clearly, we should study the following
statements: Total = 20.00; In this statement, a value 20.00 has
been stored in a memory location Total.

Declaration of a variable
Before a variable is used in a program, we must declare it. This
activity enables the compiler to make available the appropriate
type of location in the memory.
float Total;
You can declare more than one variable of the same type in a
single statement int x,y;

Initialization of variable
When we declare a variable it's default value is undetermined.
We can declare a variable with some initial value.
int a = 20;
Primitive Built-in Types
C++ offers the programmer a rich assortment of built-in as well
as user-defined data types. Following table lists down seven
basic C++ data types:

STRUCTURE OF C++ PROGRAM


#include
main ()
{
...........
...........
...........
}

A C++ program starts with function called main ( ). The body of


the function is enclosed in curly braces. The program
statements are written within the braces. Each statement must
end with a semicolon;(statement terminator). A C++ program
may contain as many functions as required. However, when the
program is loaded in the memory, the control is handed over to
function main ( ) and it is the first function to be executed.

Flow of control
Statements
Statements are the instructions given to the computer to
perform any kind of action. Action may be in the form of data
movement, decision making etc. Statements form the smallest
executable unit within a C++ program. Statements are always
terminated by a semicolon.

Compound Statement
A compound statement is a grouping of statements in which
each individual statement ends with a semicolon. The group of
statements is called block. Compound statements are enclosed
between the pair of braces ({}.). The opening brace ({) signifies
the beginning and closing brace (}) signifies the end of the
block.

Null Statement
Writing only a semicolon indicates a null statement. Thus ';' is a
null or empty statement. This is quite useful when the syntax of
the language needs to specify a statement but the logic of the
program does not need any statement. This statement is
generally used in for and while looping statements.

Conditional Statements
Sometimes the program needs to be executed depending upon
a condition. C++ provides the following statements for
implementing the selection control structure.
•if statement
•if else statement
• nested if statement
•switch statement

if statement
The syntax of the if statement
if (condition)
{
statement(s);
}
From the flowchart, it is clear that if the if the condition is true,
the statement is executed; otherwise it is skipped. The
statement may either be a single or compound statement.

if else statement
syntax of the if - else statement
if (condition)
statement1;
else
statement2;
From the above flowchart, the given condition is evaluated first.
If the condition is true, statement1 is executed. If the condition
is false, statement2 is executed. It should be kept in mind that
statement and statement2 can be a single or compound
statement.

Nested if statement
The if block may be nested in another if or else block. This is
called nesting of if or else block.

Switch statement
The if and if-else statements permit two-way branching
whereas switch statement permits multiple branching.

Looping statement
It is also called a repetitive control structure. Sometimes we
require a set of statements to be executed many times by
changing the value of one or more variables each time to obtain
a different result.

ARRAY
An array is a collection of data elements of same data type. It is
described by a single name and each element of an array is
referenced by using array name and its subscript no.

Declaration of Array
Type arrayname[numberofelements];
For example, int Age[5] ;
float cost[30];

Arrays as Parameters
At some moment, we may need to pass an array to a function
as a parameter. In C++, it is not possible to pass a complete
block of memory by value as a parameter to a function, but we
can pass its address. For example, the following function: void
print(int A[]) accepts a parameter of type "array of int" called A.

Functions in C++
A function is a group of statements that together perform a
specific task. Every C++ program has at least one function,
which is main (). The function is used for dividing a large code
into a module, due to this we can easily debug and maintain the
code. For example, if we write calculator programs at that time
we can write every logic in a separate function (For addition
sum (), for subtraction sub()). Any function can be called many
times.

Advantage of Function
 Code Re-usability
 Develop an application in module format.
 Easily to debug the program.
 Code optimization: No need to write a lot of code.
Type of Function
There are two types of function in C++ Language. They are;
 Library function or pre-define function.
 User defined function.

Calling a function.
When we call any function, control goes to function body and
execute entire code. For a call, any function just writes the
name of the function and if any parameter is required then pass
a parameter.
Syntax
function_name();
or variable=function_name(argument);
Note: At the time of function calling function must be
terminated with ';'.

SOME BASIC EXAMPLES OF C++


Example 1
#include <iostream>
int main()
{
std::cout”Hello UIT”;
return0;
}
Example 2
#include <iostream>
using namespace std;
int main
{
int a;
cout<<”the value of a”;
cin>>a;
if(a<100);
cout<<”a is less than 100”;
else if(a>=100);
cout<<”a is greater than or equal to 100”;
return 0 ;
}

Example 3
#include <iostream>
using namespace std;
int main()
{
int a, b, c;
cout<<”the value of a is “;
cin>>a;
cout<<”the value of b is “;
cin>>b;
c = a+b;
cout<<”the sum of a and b is “<<c;
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy