Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Chap I Basic C++ Programming Elements

Uploaded by

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

Chap I Basic C++ Programming Elements

Uploaded by

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

Advanced Programming (C++)

Basic C++ Programming Elements

1
A Simple C++ Program
• Like many programming languages, creating and running a C++ program requires
several steps.
 First, we create a C++ source file into which we enter the lines of our
program.
 After we save this file, we then run a program, called a compiler, which creates a
machine-code interpretation of this program.

 Another program, called a linker (which is typically invoked automatically by the


compiler), includes any required library (code functions) needed and produces the
final machine-executable file.

2
A Simple C++ Program(cont)
#include <cstdlib>
#include <iostream>
/* This program inputs two numbers x and y and outputs
their sum */
int main( ) {
int x, y;
std::cout << "Please enter two numbers: ";
std::cin >> x >> y; // input x and y
int sum = x + y; // compute their sum
std::cout << "Their sum is " << sum << std::endl;
return EXIT SUCCESS; // terminate successfully
}

3
What are the differences between C and C+
+?

• C is a procedural programming language and does not support classes and


objects
• C++ is a combination of both procedural and object oriented programming
language; therefore C++ can be called a hybrid language.

4
What are the differences between C and C++?

C C++
When compared to C++, C is a subset of C+ C++ is a superset of C. C++ can run most of
+. C code while C cannot run C++ code.
C supports procedural programming C++ supports both procedural and object
paradigm for code development. oriented programming paradigms;
therefore C++ is also called a hybrid
language.
C does not support object oriented Being an object oriented programming
programming; therefore it has no support language C++ supports polymorphism,
for polymorphism, encapsulation, and encapsulation, and inheritance.
inheritance.
In C (because it is a procedural In C++ (when it is used as object oriented
programming language), data and programming language), data and
functions are separate and free entities. functions are encapsulated together in
form of an object. For creating objects
class provides a blueprint of structure of
the object.

5
What are the differences between C and C++?
In details (cont.)

C C++
In C, data are free entities and can be In C++, Encapsulation hides the data to
manipulated by outside code. This is ensure that data structures and operators
because C does not support information are used as intended.
hiding.
C, being a procedural programming, it is a While, C++, being an object oriented
function driven language. programming, it is an object driven
language.
C does not support function and operator C++ supports both function and operator
overloading. overloading.
C does not allow functions to be defined In C++, functions can be used inside a
inside structures. structure.
C does not have namespace feature. C++ uses NAMESPACE which avoid name
collisions.

6
What are the differences between C and C++?
In details (cont.)

C C++
C uses functions for input/output. For C++ uses objects for input output. For
example scanf and printf. example cin and cout.
C does not support reference variables. C++ supports reference variables.
C has no support for virtual and friend C++ supports virtual and friend functions.
functions.
C provides malloc() and calloc() functions C++ provides new operator for memory
for dynamic memory allocation, and free() allocation and delete operator for memory
for memory de-allocation. de-allocation.
C does not provide direct support for error C++ provides support for exception
handling (also called exception handling) handling. Exceptions are used for "hard"
errors that make the code incorrect.

7
Agenda
• Data Types
 Fundamental Types
• Bool
• Char
• short
• Int
• Long
• Float
• Double
 Other Types:
• Enum

 Variables Declaration

8
Integer, “int”
• An int variable holds an integer. Integers come in
three sizes: short int, (plain) int, and long int.
The terms “short” and “long” are synonyms for“short
int”and “long int,” respectively.
• Decimal numbers such as 0, 25, 98765, and -3 are of type int.
The suffix “l” or “L” can be added to indicate a long integer, as in
123456789L.
• Octal (base 8) constants are specified by prefixing the number
with the zero digit, and
• Hexadecimal (base 16) constants can be specified by prefixing the
number with “0x.”
9
Characters
• A char variable holds a single character.

• A char in C++ is typically 8-bits, but the exact number of


bits used for a char variable is dependent on the
particular implementation.
 By allowing different implementations to define the meaning of basic
types, such as char, C++ can tailor its generated code to each machine
architecture and so achieve maximum efficiency.

• A literal is a constant value appearing in a program.


Character literals are enclosed in single quotes, as in ’a’, ’Q’,
and ’+’.
10
Characters (cont.)

• A backslash ( \) is used to specify a number of special character


literals as shown below.
’\n’ newline
’\b’ backspace
’\’’ single quote
’\\’ backslash
’\t’ tab
’\0’ null
’\"’ double quote
• The null character, ’\0’, is sometimes used to indicate the end of a
string of characters.
• Every character is associated with an integer code. The function
int(ch) returns the integer value associated with a character
variable ch.
11
Floating Point : float, double
• A variable of type float holds a single-precision floating-point
number, and
• A variable of type double holds a double-precision floating-
point number. As it does with integers, C++ leaves undefined
the exact number of bits in each of the floating point types.
• By default, floating point literals, such as 3.14159 and –
1234.567 are of type double.
• Scientific or exponential notation may be specified using
either “e” or “E” to separate the mantissa from the
exponent, as in 3.14E5, which means 3.14×105.
To force a literal to be a float, add the suffix “f” or “F,” as in
2.0f or 1.234e-3F.
12
Enumerations
• An enumeration is a user-defined type that can
hold any of a set of discrete values.
• Once defined, enumerations behave much like an
integer type.
• A common use of enumerations is to provide
meaningful names to a set of related values.
• Each element of an enumeration is associated with an
integer value.
• By default, these values count up from 0, but it is also
possible to define explicit constant values as shown in
the following example.
13
Enumerations (cont.)

• Example :
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT };
enum Mood { HAPPY = 3, SAD = 1, ANXIOUS = 4, SLEEPY = 2 };
enum MealType {NO_PREF, REGULAR, LOW_FAT, VEGETARIAN };
Day today = THU; // today may be any of SUN ... SAT
Mood myMood = SLEEPY; // myMood may be HAPPY, ..., SLEEPY

• Since we did not specify values, SUN would be


associated with 0, MON with 1, and so on.

14
Enumerations (cont.)
• Exercise : Predict the output of the following C++
program
1. #include <stdio.h>
enum day {sunday = 1, tuesday, wednesday, thursday, friday, saturday};
int main()
{
enum day d = thursday;
std::cout<<"The day number stored in d is “<< d;
return 0;
}

15
More on Data Types
• Together, enumerations and the types bool, char,
and int are called integral types.
• C++ does not specify the exact number of bits in each type, but a
short is at least 16 bits, and a long is at least 32 bits.
• Given a type T, the expression sizeof(T) returns the size
of type T, expressed as some number of multiples of the
size of char.
• For example, on typical systems, a char is 8 bits long, and an int is
32 bits long, and hence sizeof(int) is 4.

16
More on Data Types (cont.)
• There is a special type void, which explicitly indicates
the absence of any type information.

Now, let’s look into Variables’ declaration.

17
Variable Declaration in C++
• Valid Variable name:
 Variable names may consist of any combination of
letters, digits, or the underscore (_) character, but the
first character cannot be a digit. Examples:
short n; // n’s value is undefined
int octalNumber = 0400; // 400 (base 8) = 256 (base 10)
char_newlinecharacter = ’\n’;
long BIGnumber = 314159265L;
short _aSTRANGE__1234_variABlE_NaMe;

• Although it is legal to start a variable name with an


underscore, it is best to avoid this practice, since some C++
compilers use this convention for defining their own
internal identifiers.
18
Variable Declaration in C++ (cont.)

• As you might have noticed the variable


declaration is :
 Data type followed by the variable’s name

• When declaring a variable, we have the option of providing a


definition, or initial value, known as initialization.

• If no definition is given, the initial value is unpredictable, so it is


important that each variable be assigned a value before being
used.

19
Sources

G. T. Michael, T. Roberto, M. M. David, Data Structures


and Algorithms in C++ ,Second Edition,2011.
Juan Soulié, C++ Language June, 2007

20
21

You might also like