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

Chapter Two: Programming Fundamentals

C++ began as an expanded version of C created by Bjarne Stroustrup in 1979. It was initially called "C with classes" and later renamed to C++ in 1983. C++ is an object-oriented programming language that uses concepts like classes, inheritance, polymorphism, encapsulation and abstraction. It is efficient and is commonly used to create other programming languages.

Uploaded by

lalisa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Chapter Two: Programming Fundamentals

C++ began as an expanded version of C created by Bjarne Stroustrup in 1979. It was initially called "C with classes" and later renamed to C++ in 1983. C++ is an object-oriented programming language that uses concepts like classes, inheritance, polymorphism, encapsulation and abstraction. It is efficient and is commonly used to create other programming languages.

Uploaded by

lalisa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Chapter two

Programming fundamentals:

1
C++ origins

C++ began as expanded version of C.


C++ was created by Bjarne Stroustrup in 1979 at Bell
laboratories in Murray Hill, New Jersy.
Initially C++ was called new language “C with
classes”.
In 1983 the name was changed to C++.

2
Cont…

 C++ is an object oriented programming


language(i.e. use the objects and classes and the
concepts inheritance, polymorphism, encapsulation
and abstraction).
 Where as c is procedural programming language
( i.e. we organize our code in to sub function and
that function contain action that we wants to do)

3
Cont…
• C++ is an object oriented programming
language
• C++ is a block structured programming
language which means we create functions
inside the function
• C++ is efficient
• C++ is the language of choice for creating
other programming languages.

4
What is toolset ?
 The computer can only understand the machine
language code which is the combination of 0 and 1.
 The three basic tools used to build c++ application are
the compiler, the linker, and the arc hiver (or librarian).
 A collection of these program and possibly other tools is
called a toolset.

5
Cont…
Compilers: takes c++ source file as input and produces object
files, which contain a mixture of a machine executable code
and symbolic references to functions and data.
The arc hiver takes a collection of object files as input and
produces a static library, or archive, which is simply a
collection of object files grouped for convenient use.
The linker takes a collection of object files and libraries and
resolves their symbolic references to produce either an
executable or dynamic library.

6
Executable and Dynamic library

 An executable, or application is simply any program


that can be executed by the operating system.
 A dynamic library also called a shared library, is
like an executable except that it can’t be run on its
own.
 On windows, dynamic libraries are also called
dynamic link libraries(DLLs).
7
How c++ works
C++ C++
source Library
code Compilation

Machine
code
version

+
Linking

Complete
machine
executable
program

8
Structure of c++ program
 Install IDE( example code block)
 C++ program must have an extension of .cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" ; Function body
return 0;
}

9
Cont…

10
Cont…
• # is a signal for preprocessor, allows to include

library files or header files to a program. This line

notifies the preprocessor to include in the program

the contents of the input/output stream header file

<iostream>.

• Preprocessor (include) is run before a compilation

process

11
Cont…
• iostream is the header file which we want to include to our

program so that we can use some objects such as cin, cout etc

which help us to do some i/o task.

• The word stream indicates that the information involved will

be treated as a flow of data.

• using namespace allows direct access to the names of the std

namespace.

• So name space allows to organize large program


12
Cont…

• Main(){ } function is the place where program


execution starts.
• Function is a collection of statements which can do
some particular task.
• << -Stream insertion operator
• return 0 represents this program has successfully
executed
13
Cont…
Exercise 1
Write a C++ program that outputs the following text
on screen:
Oh what
a happy day!
Oh yes,
what a happy day!
Note: Use the manipulator endl where appropriate.

14
A brief look at cout and cin
• Cout is an object used for printing data to the screen.
• To print a value to the screen, write the word cout,
followed by the insertion operator also called
output redirection operator (<<) and the object to
be printed on the screen.
• Syntax: Cout<<Object;
• Cin is an object used for taking input from the
keyboard.

15
Cont…
• To take input from the keyboard, write the word cin,
followed by the input redirection operator (>>) and
the object name to hold the input value.
• >> is extraction operator
• Syntax: Cin>>Object
• Cin will take value from the keyboard and store it in
the memory.

16
Cont…
• Thus the cin statement needs a variable which is a
reserved memory place holder.
• Both << and >> return their right operand as their
result, enabling multiple input or multiple output
operations to be combined into one statement.
• E.g.: Cin>>var1>>var2>>var3;
• Cout<<var1<<”, “<<var2<<” and “<<var3;

17
Putting Comments on C++ programs
• A comment is a piece of descriptive text which
explains some aspect of a program.
• Program comments are text totally ignored by the
compiler and are only intended to inform the reader
how the source code is working at any particular
point in the program.

18
Single line comment
• C++ provides two types of comment delimiters:

• Single Line Comment: Anything after // {double


forward slash} (until the end of the line on which it
appears) is considered a comment.
Eg: cout<<var1; //this line prints the value of var1

19
Multiple line comment
 Multiple Line Comment: Anything enclosed by the
pair /* and */ is considered a comment.
E.g.: /*this is a kind of comment where Multiple lines
can be enclosed in one C++ program */
Comments should be used to enhance (not to hinder)
the readability of a program.

20
Basic Elements of c++
Identifiers
• Identifier is name of a variable or any other named
construct
• Identifier must start with a letter or underscore symbol
(_), the rest of the characters should be letters, digits or
underscores
• The following are legal identifiers:
x x1 x_1 _abc sum RateAveragE

• the following are not legal identifiers. Why?


13 3X %change data-1 my.identifier

21
Rules of defining identifiers
Identifier must start with letter or underscore
symbol (_), the rest of the characters should
be letters, digits or underscores (A-Z), (a-z)
or(_)
Identifier cannot be a keyword.(E.g. int,
switch, float…etc )
It can have a numbers
Identifier should not have special symbols
such as $, #, ., %, +,…

22
Cont…
Should not start with space and should not
contain empty space
It should not have two consecutive under
score
It should be simple and should exactly
represent why it is for?
Identifier is case sensitive

23
Case Sensitivity

• C++ is case-sensitive.
• In other words, uppercase and lowercase letters are
considered to be different.
• A variable named age is different from Age, which is
different from AGE.

24
Keywords
 keywords are identifiers reserved as part of the
language
int, return, float,char
 These are keywords used by the compiler to control
your program.
• Cannot be used by the programmer to name things
• Consist of lowercase letters only
• Have special meaning to the compiler

25
Cont…
case
do if return typedef
auto double inline short typeid
bool dynamic_cast int signed typename
break delete long sizeof union
else mutable static unsigned
catch enum namespace static_cast using
char explicit new struct virtual
class extern operator switch void
const false private template volatile
const_cast float protected this wchar_t
continue for public throw while
default friend register true union
delete goto reinterpret_cast try unsigned

26
Variables
• In C++ a variable is a place to store information.
• A variable is a location in your computer's memory in
which you can store a value and from which you can
later retrieve that value.
NOTE: RAM is random access memory.
 When you run your program, it is loaded into RAM
from the disk file.
 All variables are also created in RAM. When
programmers talk of memory, it is usually RAM to
which they are referring.

27
Variables cont...
• When you define a variable in C++, you must tell the
compiler what kind of variable it is: an integer, a
character, and so forth.
• This information tells the compiler how much room
to set aside and what kind of value you want to store
in your variable.

28
Variable declaration
• Syntax:

data type variable_name


Example: int number1;
In this example int represent the type integer to be
stored on the variable name number1;
You can define more than one variables

int num1, int num2, int num3;…


29
Assigning Values to Your Variables
• You assign a value to a variable by using the
assignment operator(=).
A) During declaration
float pi=3.14;
int num1=5;
B) During run time
• use input and output statements
input->cin >>
output->cout <<

30
Example
Eg. Accept variable to compute the value of y where
y=3x+b
Solution
int y, x, b
cout <<“enter the value of x” << endl;
cin >> x;
cout <<“enter the value of b” << endl;
cin >> b;
y=3*x+b;
Cout<<“The value of y is: ”<<y;
31
Scope of Variables.
• Scope of a variable is the boundary or block in a
program where a variable can be accessed.
• The boundary or block is identified by the left and
right bracket
• You can declare variables any where in the
source code.
• But you should declare a variable before using.

32
Scope of variables

Global variables: are variables that can be


referred/accessed any where in the code, within any function,
as long as it is declared first.
A variable declared before any function immediately
after the include statements are global variables.
Local Variables: the scope of the local variable is limited to
the code level or block within which they are declared. In the
following example, the integer data type num1 is
accessible everywhere whereas z is only accessible in the add
function and num2 is accessible in main function.
• This means cout<<z; or any statement involving z is only
valid in add function.
33
Example
#include<iostream>
using namespace std;
int num1;
 int add( int x, int y)
{
int z;
….
}
int main()
{
float num2;
cout<<”Enter your age:”;

} 34
Data Type
• Data type is a type which is established when the
variable is defined. (e.g. integer, float, character etc).
• Data type describes the property of the data and the
size of the reserved memory

35
Fundamental Variable types
• Several other variable types are built into C++.
• They can be conveniently classified as integer, floating-point
and character variables.
• Floating-point variable types can be expressed as fraction
i.e. they are “real numbers”.
• Character variables hold a single byte.
• The type of variables used in C++ program are described in
the next table, which lists the variable type, how much room

36
Cont…
Type Length Range
char 8 bits -128 to 127

unsigned int 16 bits 0 to 65,535

short int 16 bits -32,768 to 32,767

int 16 bits -32,768 to 32,767

unsigned long 32 bits 0 to 4,294,967,295

long 32 bits -2,147,483,648 to 2,147,483,647

float 32 bits -3.4x10-38 to 3.4x10+38

double 64 bits -1.7x10-308 to 1.7x10+308

long double 80 bits -3.4x10-4932 to 1.1x10+4932

bool 8 bits true or false

37
Signed and Unsigned.

• Signed integers are either negative or positive.


• Unsigned integers are always positive.
• Because both signed and unsigned integers
require the same number of bytes, the largest
number (the magnitude) that can be stored in an
unsigned integer is twice as the largest positive
number that can be stored in a signed integer.

38
E.g.: Lets us have only 4 bits to represent
numbers

39
Characters.

• Characters variables (type char) are typically one


byte in size, enough to hold 256 different values.
• A char can be represented as a small number (0 -
255).
Syntax:
char a;

40
Constants
• A constant is any expression that has a fixed value.
• Like variables, constants are data storage locations in
the computer memory. But, constants, unlike
variables their content can not be changed after the
declaration.
• Constants must be initialized when they are created
by the program, and the programmer can’t assign a
new value to a constant later.
• C++ provides two types of constants: literal and
symbolic constants.

41
Cont…
• Literal constant: is a value typed directly into the program
wherever it is needed.
E.g.: const int a= 43;
• 43 is a literal constant in this statement:
• Symbolic constant: is a constant that is represented
by a name, similar to that of a variable, but unlike a
variable, its value can’t be changed after initialization. E.g.:
Int studentPerClass =15;
students = classes * studentPerClass;
• studentPerClass is a symbolic constant having a value of
15, and 15 is a literal constant directly typed in the
program.
42
Thank you for your
attention!

43

You might also like