Module 1 - Transition From C To C++
Module 1 - Transition From C To C++
CSNB244
CSNB244 Programming II
Objectives
In this chapter, you will learn:
• C++ enhancement to C.
• The header files of the C++ Standard Library.
• To use inline functions.
• To use references.
• To use default arguments.
• To use the unary scope resolution operator to access a
global variable.
• To overload functions.
• To create and use function templates that perform
identical operations different types.
History of C++
BCPL
(1967)
C++ B
Bjarne Ken
Stroustrup Thompson
(1970)
Early 1980s
C
Dennis
Ritchie -
1972
INTRODUCTION
• C++
– Improves on many of C's features
– Has object-oriented capabilities
• Increases software quality and reusability
– Developed by Bjarne Stroustrup at Bell Labs
• Called "C with classes"
• C++ (increment operator) - enhanced version of C
– Superset of C
• Can use a C++ compiler to compile C programs
• Gradually evolve the C programs to C++
Systems and Networking CSNB244
CSNB244 Programming II
#include <iostream.h>
void main ()
{
std::cout << "HELLO WORLD\n ";
Header Files
• Header files
– Each standard library has header files
• Contain function prototypes, data type definitions, and
constants
– Files ending with .h are "old-style" headers
• User defined header files
– Create your own header file
• End it with .h
– Use #include "myFile.h" in other files to
load your header
Systems and Networking CSNB244
CSNB244 Programming II
std::___
• Each header file in C++ draft standard uses a
namespace to guarantee that every feature in
the C++ standard library is unique.
• So if a user defined a class that have features
with identical names, no name conflict will
occur.
• But to suppress the use of std:: , for all use of
features in the standard library, we can use
the statement:
using namespace std;
Systems and Networking CSNB244
CSNB244 Programming II
Control Structures
• Basically nothing change with the statements
in the structured programming part.
• The basic control structures i) Sequential
structure, ii) conditional structure & iii)
repetition structure.
Conditional Statements
• if
• If .. else
• switch
• nested if .. else
Repititions : Loops
• while (condition) …
• for (init counter var; condition to continue;
increment/decrement counter var)
• do …. while (condition)
Selection Structures
• The if selection statement is a single-selection
statement because it selects or ignores a single
action (or, as we’ll soon see, a single group of
actions).
• The if…else statement is called a double-
selection statement because it selects between two
different actions (or groups of actions).
• The switch selection statement is called a
multiple-selection statement because it selects
among many different actions (or groups of
actions).
Repetition Structures
• C++ provides three types of repetition statements (also
called looping statements or loops) for performing
statements repeatedly while a condition (called the loop-
continuation condition) remains true.
• These are the while, do…while and for statements.
• The while and for statements perform the action (or
group of actions) in their bodies zero or more times.
• The do…while statement performs the action (or
group of actions) in its body at least once.
Data types
• Besides the basic data types in C :
– int
– float / double
– char
• C++ have an extra : bool
– bool (short for Boolean) is a data type that can
contain the value of true or false
– e.g. bool Flag;
Flag = false;
Systems and Networking CSNB244
CSNB244 Programming II
Sample Problem
Develop a class averaging program that will
process an arbitrary number of marks for an
exam each time the program is run.
– using a Top-Down approach, we start from the
goal.
– Goal : to get an average value for an exam
Let’s do it
C++ Keywords
Keywords common to the C and C++ programming languages
auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while
C++ only keywords
asm bool catch class const_cast
delete dynamic_cast explicit false friend
inline mutable namespacenew operator
private protected public reinterpret_
cast
static_cast template this throw true
try typeid typename using virtual
wchar_t