KEMBAR78
introductiontothecomputerprogramming.pptx
Computer Systems &
Programming
Engr. Beenish Adeel
An introduction to C++
Programming
Topics to be Discussed…
• The Evolution of Programming Languages
• Introduction to C++
• The Mechanism of Creating a C++ Program
• Processing a C++ Program
• Data Types in C++
• Variables
• Declaration of Variables
• Initialization of Variables
The Evolution of Programming Languages
1. Machine language
― Only language computer directly understands
― Defined by hardware design
― Machine-dependent
― Generally consist of strings of numbers
― Ultimately 0s and 1s
― Instruct computers to perform elementary operations
― One at a time
― Cumbersome for humans
― Example:
+1300042774 0100 1101 0111 1101 0001 0100 0001 0110
+1400593419 0101 0011 0111 1011 0101 1100 0000 1011
+1200274027 0100 0111 1000 1010 1011 1010 0110 1011
2. Assembly language
– English-like abbreviations representing elementary
computer operations
– Clearer to humans
– Incomprehensible to computers
• Translator programs (assemblers)
– Convert to machine language
– Example:
LOAD BASEPAY
ADD OVERPAY
STORE GROSSPAY
The Evolution of Programming Languages
The Evolution of Programming Languages
3. High-level languages
– e.g. C, C++, BASIC, FORTRAN, Java, Pascal, Ada, Perl
– Use common mathematical notations
– Single statements accomplish substantial tasks
• Assembly language requires many instructions to accomplish simple
tasks
– Translator programs (compilers)
• Convert to machine language
– Interpreter programs
• Directly execute high-level language programs
– Example:
grossPay = basePay + overTimePay
Introduction to C++
• C++ was developed by Bjarne Stroustrup of AT&T Bell
Laboratories in the early 1980's
• based on the C language.
• started as extension to C (macros and variables)
• added new useful, features
• nowadays a language of its own
• C++ (the next thing after C, though wouldn’t +
+C be more appropriate?)
The Mechanism of Creating a C++ Program
Editor: Accepts the typing of the source
code (or header file).
Source file: The file that contains
the program you prepared in the editor
after you save it. (.cpp)
Header file: Header files such as
iostream.h
Preprocessor: Performs preliminary
operations on files before they are
passed to the compiler.
Compiler: Translates the source code
to machine language.
Object code: The file containing the
translated source code. (.obj)
Linker: Links the object file with
additional code, such as the library
codes.
Executable code: The file containing
the final product. (.exe)
C++ Programming: From Problem Analysis to Program Design, Fourth
Edition
9
The Basics of a C++ Program
• Function: collection of statements; when
executed, accomplishes something
– May be predefined or standard
• Syntax: rules that specify which statements
(instructions) are legal
• Programming language: a set of rules, symbols,
and special words
• Semantic rule: meaning of the instruction
Processing a C++ Program
#include <iostream>
using namespace std;
int main()
{
cout<<"My first C++program."<<endl;
return 0;
}
Sample Run:
My first C++ program.
• Let us look at the various parts of the above program −
• The C++ language defines several headers, which contain
information that is either necessary or useful to your program. For
this program, the header <iostream> is needed.
• The line using namespace std; tells the compiler to use the std
namespace. Namespaces are a relatively recent addition to C++.
• The next line '// main() is where program execution begins.' is a
single-line comment available in C++. Single-line comments begin
with // and stop at the end of the line.
• The line int main() is the main function where program execution
begins.
• The next line cout << "Hello World"; causes the message "Hello
World" to be displayed on the screen.
• The next line return 0; terminates main( )function and causes it to
return the value 0 to the calling process.
Processing a C++ Program
Data Types in C++
Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
• C++ offer the programmer a rich assortment of built-in as well
as user defined data types. Following table list down seven
basic C++ data types:
• Several of the basic types can be modified using one or more
of these type modifiers:
• Signed
• unsigned
• short, and
• long
Data types in C++
Type Typical Bit Width Typical Range
Char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
Int 4bytes
-2147483648 to
2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes
-2147483648 to
2147483647
Float 4bytes +/- 3.4e +/- 38 (~7 digits)
Double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character
Example 2
#include <iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
return 0;
}
Output
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4
Variables in C++ (1)
• 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 statement:
Total = 20.00;
• In this statement a value 20.00 has been stored in a
memory location Total.
Declaration of a variable (1)
• 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.
Syntax:
dataType Name;
Example:
float Total;
• You can declare more than one variable of same type
in a single single statement i-e.
int x,y;
• Some valid declarations are shown here :
int i, j, k;
char c, ch;
float f, salary;
double d;
• The line int i, j, k; both declares and defines the
variables i, j and k; which instructs the compiler
to create variables named i, j and k of type int.
Declaration of a variable (2)
Initialization of variable
• When we declare a variable it's default value
is undetermined. We can declare a variable
with some initial value.
Syntax:
dataType Name = Value;
Example:
int a = 20;
Example
#include <iostream>
using namespace std;
int main ()
{
int a, b; // Variable declaration
int c;
float f;
a = 10; // actual initialization
b = 20;
c = a + b;
cout << c << endl ;
f = 70.0/3.0;
cout << f << endl ;
return 0;
}
RESULT
30
23.3333
Exercise
• Write a program which takes 3 numbers from
user, add them and shows the result.
• Write a program that ask 4 integer numbers
from user and performs addition, subtraction,
multiplication and division.
THANKS

introductiontothecomputerprogramming.pptx

  • 1.
  • 2.
    An introduction toC++ Programming
  • 3.
    Topics to beDiscussed… • The Evolution of Programming Languages • Introduction to C++ • The Mechanism of Creating a C++ Program • Processing a C++ Program • Data Types in C++ • Variables • Declaration of Variables • Initialization of Variables
  • 4.
    The Evolution ofProgramming Languages 1. Machine language ― Only language computer directly understands ― Defined by hardware design ― Machine-dependent ― Generally consist of strings of numbers ― Ultimately 0s and 1s ― Instruct computers to perform elementary operations ― One at a time ― Cumbersome for humans ― Example: +1300042774 0100 1101 0111 1101 0001 0100 0001 0110 +1400593419 0101 0011 0111 1011 0101 1100 0000 1011 +1200274027 0100 0111 1000 1010 1011 1010 0110 1011
  • 5.
    2. Assembly language –English-like abbreviations representing elementary computer operations – Clearer to humans – Incomprehensible to computers • Translator programs (assemblers) – Convert to machine language – Example: LOAD BASEPAY ADD OVERPAY STORE GROSSPAY The Evolution of Programming Languages
  • 6.
    The Evolution ofProgramming Languages 3. High-level languages – e.g. C, C++, BASIC, FORTRAN, Java, Pascal, Ada, Perl – Use common mathematical notations – Single statements accomplish substantial tasks • Assembly language requires many instructions to accomplish simple tasks – Translator programs (compilers) • Convert to machine language – Interpreter programs • Directly execute high-level language programs – Example: grossPay = basePay + overTimePay
  • 7.
    Introduction to C++ •C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980's • based on the C language. • started as extension to C (macros and variables) • added new useful, features • nowadays a language of its own • C++ (the next thing after C, though wouldn’t + +C be more appropriate?)
  • 8.
    The Mechanism ofCreating a C++ Program Editor: Accepts the typing of the source code (or header file). Source file: The file that contains the program you prepared in the editor after you save it. (.cpp) Header file: Header files such as iostream.h Preprocessor: Performs preliminary operations on files before they are passed to the compiler. Compiler: Translates the source code to machine language. Object code: The file containing the translated source code. (.obj) Linker: Links the object file with additional code, such as the library codes. Executable code: The file containing the final product. (.exe)
  • 9.
    C++ Programming: FromProblem Analysis to Program Design, Fourth Edition 9 The Basics of a C++ Program • Function: collection of statements; when executed, accomplishes something – May be predefined or standard • Syntax: rules that specify which statements (instructions) are legal • Programming language: a set of rules, symbols, and special words • Semantic rule: meaning of the instruction
  • 10.
    Processing a C++Program #include <iostream> using namespace std; int main() { cout<<"My first C++program."<<endl; return 0; } Sample Run: My first C++ program.
  • 11.
    • Let uslook at the various parts of the above program − • The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream> is needed. • The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++. • The next line '// main() is where program execution begins.' is a single-line comment available in C++. Single-line comments begin with // and stop at the end of the line. • The line int main() is the main function where program execution begins. • The next line cout << "Hello World"; causes the message "Hello World" to be displayed on the screen. • The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process. Processing a C++ Program
  • 12.
    Data Types inC++ Type Keyword Boolean bool Character char Integer int Floating point float Double floating point double Valueless void Wide character wchar_t • C++ offer the programmer a rich assortment of built-in as well as user defined data types. Following table list down seven basic C++ data types: • Several of the basic types can be modified using one or more of these type modifiers: • Signed • unsigned • short, and • long
  • 13.
    Data types inC++ Type Typical Bit Width Typical Range Char 1byte -127 to 127 or 0 to 255 unsigned char 1byte 0 to 255 signed char 1byte -127 to 127 Int 4bytes -2147483648 to 2147483647 unsigned int 4bytes 0 to 4294967295 signed int 4bytes -2147483648 to 2147483647 Float 4bytes +/- 3.4e +/- 38 (~7 digits) Double 8bytes +/- 1.7e +/- 308 (~15 digits) long double 8bytes +/- 1.7e +/- 308 (~15 digits) wchar_t 2 or 4 bytes 1 wide character
  • 14.
    Example 2 #include <iostream> usingnamespace std; int main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; return 0; }
  • 15.
    Output Size of char: 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of wchar_t : 4
  • 16.
    Variables in C++(1) • 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 statement: Total = 20.00; • In this statement a value 20.00 has been stored in a memory location Total.
  • 17.
    Declaration of avariable (1) • 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. Syntax: dataType Name; Example: float Total; • You can declare more than one variable of same type in a single single statement i-e. int x,y;
  • 18.
    • Some validdeclarations are shown here : int i, j, k; char c, ch; float f, salary; double d; • The line int i, j, k; both declares and defines the variables i, j and k; which instructs the compiler to create variables named i, j and k of type int. Declaration of a variable (2)
  • 19.
    Initialization of variable •When we declare a variable it's default value is undetermined. We can declare a variable with some initial value. Syntax: dataType Name = Value; Example: int a = 20;
  • 20.
    Example #include <iostream> using namespacestd; int main () { int a, b; // Variable declaration int c; float f; a = 10; // actual initialization b = 20; c = a + b; cout << c << endl ; f = 70.0/3.0; cout << f << endl ; return 0; } RESULT 30 23.3333
  • 21.
    Exercise • Write aprogram which takes 3 numbers from user, add them and shows the result. • Write a program that ask 4 integer numbers from user and performs addition, subtraction, multiplication and division.
  • 22.