Chap I Basic C++ Programming Elements
Chap I 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.
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+
+?
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.
• 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
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.
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;
19
Sources
20
21