0% found this document useful (0 votes)
20 views22 pages

3 Programming Fundamentals

The document discusses the basics of C++ programming including the structure of a C++ program, data types, variables, constants, and input/output statements. It covers topics like main functions, comments, libraries, and the different sections that make up a C++ program.

Uploaded by

hlt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
20 views22 pages

3 Programming Fundamentals

The document discusses the basics of C++ programming including the structure of a C++ program, data types, variables, constants, and input/output statements. It covers topics like main functions, comments, libraries, and the different sections that make up a C++ program.

Uploaded by

hlt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 22

Programming Fundamentals:

The First C++ Program

Writing Comments

Constants and Variables

Data Types

I/O Statements

Defining Constants
First Program
 // Simple C++ program to display "Hello World"

 // Header file for input output functions


 #include<iostream>
 using namespace std;
 int main() // main function -
 {
 // prints hello world
 cout<<"Hello World";
 return 0;
 }
 Output:
 Hello World
Structure of C++ Program

The C++ program is written using a specific template structure. The structure of the
program written in C++ language is as follows:
Documentation Section:

 This section comes first and is used to document the logic of the program that the programmer
going to code.
 It can be also used to write for purpose of the program.
 Whatever written in the documentation section is the comment and is not compiled by the compiler.
 Documentation section is optional since the program can execute without them. Below is the
snippet of the same:
 Example:/* This is a C++ program to find the
 factorial of a number
 The basic requirement for writing this
 program is to have knowledge of loops
 To find the factorial of number
 iterate over range from number to one
 */
Linking Section
 Standard libraries section
#include <iostream>
 using namespace std;
 #include is a specific preprocessor command that effectively copies and pastes the
entire text of the file, specified between the angle brackets, into the source code.
 The file <iostream>, which is a standard file that should come with the C++ compiler,
is short for input-output streams. This command contains code for displaying and
getting an input from the user.
 This enables the programmer to use standard input, output, and error facilities that are
provided only through the standard streams defined in <iostream>. These standard
streams process data as a stream of characters, that is, data is read and displayed in a
continuous flow.
Definition Section:

 It is used to declare some constants and assign them some value.


 In this section, anyone can define your own datatype using primitive data types.
 In #define is a compiler directive which tells the compiler whenever the message is
found replace it with “factorial\n” .
 Global declaration section:
 Here the variables and the class definitions which are going to be used in the
program are declared to make them global.
 The scope of the variable declared in this section lasts until the entire program
terminates.
 These variables are accessible within the user-defined functions also.
Function declaration section:

 It contains all the functions which our main functions need.


 Usually, this section contains the user-defined functions.
 This part of the program can be written after the main function but for this, write the function
prototype in this section for the function which for you are going to write code after the main
function.
 Function body section
 Cout << "hello world" << endl;
 Return 0;
 The return keyword tells the program to return a value to the function int main
 After the return statement, execution control returns to the operating system component that
launched this program.
 Execution of the code terminates here.
Main function section

 int main() {}
 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.
 { signifies the start of a block of code, ​and } signifies the end.
 Example:
 #include <iostream>
 using namespace std;
 int main() {
 cout << "Hello World!" << endl;
 return 0;
 }
C++ Variables
 The name of a variable can be composed of letters, digits, and the underscore character. It must begin with
either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive .
 A variable is a name given to a memory location. It is the basic unit of storage in a program.

 // Declaring a single variable


 datatype variable_name;

 Char date= value; int a=5;

 // Declaring multiple variables:


 type variable1_name, variable2_name, variable3_name;
• Rules for naming a variable
• A variable name can only have alphabets, numbers, and the underscore
_.

• A variable name cannot begin with a number. Variable names should


not begin with an uppercase character.

• A variable name cannot be a keyword. For example, int is a keyword


that is used to denote integers.

• A variable name can start with an underscore. However, it's not


considered a good practice.
 There are three types of variables based on the scope of variables in C++:

 Local variables
 Instance variables
 Static variables
 Local variables: A variable defined within a block or method or constructor is called
local variable.
 These variable are created when the block in entered or the function is called and
destroyed after exiting from the block or when the call returns from the function.
 The scope of these variables exists only within the block in which the variable is
declared. I.E. We can access these variable only within that block.
 class calculation{
 int a=10; // instance variable;
 static int b=20; //static variable
 void add() // this is function (or) method
 {
 int sum;// creating variables
 sum=a+b; // performing addition operation
 cout<<“the sum of value is :”<<sum //printing sum of the value
 }
 };
 // driver code
 int main() // this is the main function
 {
 Calculation add(); // we are calling the function
 }
 Instance variables: instance variables are non-static variables and are declared in a class outside any
method, constructor or block.
 As instance variables are declared in a class, these variables are created when an object of the class is created
and destroyed when the object is destroyed..
 Instance variable can be accessed only by creating objects.

 Static variables: static variables are also known as class variables.


 These variables are declared similarly as instance variables, the difference is that static variables are declared
using the static keyword within a class outside any method constructor or block.
 Static variables are created at the start of program execution and destroyed automatically when execution
ends.
Data types
 Whenever a variable is defined in C++, the compiler allocates some memory for that variable based on the data-
type with which it is declared. Every data type requires a different amount of memory.
 Integer: keyword used for integer data types is int. Integers typically requires 4 bytes of memory space and
ranges from -2147483648 to 2147483647.
 Character: character data type is used for storing characters. Keyword used for character data type is char.
Characters typically requires 1 byte of memory space and ranges from -128 to 127 or 0 to 255.
 Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean variable can store
either true or false. Keyword used for Boolean data type is bool.
 Floating point: floating point data type is used for storing single precision floating point values or decimal
values. Keyword used for floating point data type is float. Float variables typically requires 4 byte of memory
space.
 Double floating point: double floating point data type is used for storing double precision floating point
values or decimal values. Keyword used for double floating point data type is double. Double variables
typically requires 8 byte of memory space.
 Void: void means without any value. Void datatype represents a valueless entity. Void data type is used for
those function which does not returns a value.

 Wide Character: wide character data type is also a type but this data type has size greater than the normal 8-
bit datatype. Represented by wchar_t. It is gcharacter data enerally 2 or 4 bytes long.
Constants
 Constants refer to fixed values that the program may not alter and they are called literals.

 Constants can be of any of the basic data types and can be divided into Integer Numerals,
Floating-Point Numerals, Characters, Strings and Boolean Values.
 Integer Literals
 An integer literal can be a decimal, octal, or hexadecimal constant. A prefix specifies the
base or radix: 0x or 0X for hexadecimal, 0 for octal, and nothing for decimal.
 Floating-point Literals
 A floating-point literal has an integer part, a decimal point, a fractional part, and an
exponent part. You can represent floating point literals either in decimal form or exponential
form.
 Boolean Literals
 There are two Boolean literals and they are part of standard C++ keywords −
 A value of true representing true.
 A value of false representing false.
Character Literals
Character constant is a single symbol enclosed in the single quotes.
‘e’ ‘1’ ‘$’

String Literals
Collection of character is called as string. String literals are enclosed in double quotes.

“Computer”
“Programming”
Defining Constants
 There are two simple ways in C++ to define constants −

 Using #define preprocessor.


 Following is the form to use #define preprocessor to define a constant −
 #define identifier value.
 Using const keyword.
 You can use const prefix to declare constants with a specific type as follows −

 const type variable = value;


I/O Header Files
 Header files available in C++ for Input/Output operations are:

 iostream: iostream stands for standard input-output stream. This header file contains
definitions to objects like cin, cout, cerr etc.

 iomanip: iomanip stands for input output manipulators. The methods declared in this
files are used for manipulating streams. This file contains definitions of setw,
setprecision, etc.

 fstream: This header file mainly describes the file stream. This header file is used to
handle the data being read from a file as input or data being written into the file as
output.
 The Standard Output Stream (cout)
 The predefined object cout is an instance of ostream class. The cout object is said to be
"connected to" the standard output device, which usually is the display screen.

 The Standard Input Stream (cin)


 The predefined object cin is an instance of istream class. The cin object is said to be attached
to the standard input device, which usually is the keyboard.

You might also like