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

Module 1 - Transition From C To C++

This document provides an overview of the CSNB244 Programming II course. It discusses: 1. The objectives of the course, which include learning C++ enhancements to C, header files, inline functions, references, default arguments, function overloading, and function templates. 2. A brief history of C++, which originated from C and was created by Bjarne Stroustrup to improve on C's features with object-oriented capabilities. 3. An introduction to key concepts in C++ like classes, the standard library, and how C++ programs are composed of classes and functions.

Uploaded by

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

Module 1 - Transition From C To C++

This document provides an overview of the CSNB244 Programming II course. It discusses: 1. The objectives of the course, which include learning C++ enhancements to C, header files, inline functions, references, default arguments, function overloading, and function templates. 2. A brief history of C++, which originated from C and was created by Bjarne Stroustrup to improve on C's features with object-oriented capabilities. 3. An introduction to key concepts in C++ like classes, the standard library, and how C++ programs are composed of classes and functions.

Uploaded by

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

CSNB244 Programming II

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.

Systems and Networking CSNB244


CSNB244 Programming II

History of C++
BCPL
(1967)

C++ B
Bjarne Ken
Stroustrup Thompson
(1970)
Early 1980s
C
Dennis
Ritchie -
1972

Systems and Networking CSNB244


CSNB244 Programming II

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

C++ is an OOP Language


• The demand for OOP was soaring To fill the need
to build software quickly, correctly and
economically.
• Objects are essentially reuseable software
components that model items in the real world.
• Developers discovered using, modular, oo design
and implementation is more productive than
previously popular programming techniques such
as structured programming only.
• C++ is a hybrid language – possible to program in
C-like style , oop style or both. (Smalltalk is pure
oo language, literally everything is an object).
Systems and Networking CSNB244
CSNB244 Programming II

C++ Standard Library


• C++ programs consist of pieces called classes and
functions.
• C++ has a rich collections of classes and functions
in its standard library.
• Two parts to learning C++
– Learn the language itself
– Learn the library functions
• Making your own functions
– Advantage: you know exactly how they work
– Disadvantage: time consuming, difficult to maintain
efficiency and design well
Systems and Networking CSNB244
CSNB244 Programming II

Your first C++ Program


// A First Program in C++

#include <iostream.h>

void main ()
{
std::cout << "HELLO WORLD\n ";

Systems and Networking CSNB244


CSNB244 Programming II

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

Output Statement : cout


• Output and input in C++ is accomplished with
streams of character
• “<<“ operator is referred to as stream
insertion operator. This string is inserted to the
standard output stream object –
cout- which is connected to the
screen

std::cout << “Hello World \n”;


Escape sequence
std namespace for a new line

Systems and Networking CSNB244


CSNB244 Programming II

Systems and Networking 10


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

Input Statement : cin


• Input/Output in C++
– Performed with streams of characters
– Streams sent to input/output objects
• Output
– std::cout - standard output stream (connected to screen)
– << stream insertion operator ("put to")
– std::cout << "hi";
• Puts "hi" to std::cout, which prints it on the screen
• Input
– std::cin - standard input object (connected to keyboard)
– >> stream extraction operator ("get from")
– std::cin >> myVariable;
• Gets stream from keyboard and puts it into myVariable

Systems and Networking CSNB244


CSNB244 Programming II

Simple Program: Adding Two Integers


• Let’s try a very simple program in C++ that
would read two integers value displays its
sum.

Systems and Networking CSNB244


2 // Addition program Outline
3 #include <iostream>
4
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 std::cout << "Enter first integer\n"; // prompt
10 std::cin >> integer1; // read 1st integer
11 std::cout << "Enter second integer\n"; // prompt
12 std::cin >> integer2; // read 2nd integer
13 sum = integer1 + integer2; // assignment of sum
14 std::cout << "Sum is " << sum << std::endl; // print sum
15
16 return 0; // indicate that program ended successfully
17 }
Enter first integer
45
Enter second integer
72
Sum is 117
 2000 Prentice Hall, Inc. All rights
CSNB244 Programming II

I/O Manipulator statement : endl


• std::endl
– "end line"
– Stream manipulator - prints a newline and flushes output
buffer
• Some systems do not display output until "there is enough text to
be worthwhile"
• std::endl forces text to be displayed
• using statements
– Allow us to remove the std:: prefix
– Discussed later
• Cascading
– Can have multiple << or >> operators in a single
statement
std::cout << "Hello " << "there" << std::endl;

Systems and Networking CSNB244


2 // Addition program Outline
3 #include <iostream>
4 using namespace std;
us
5 int main()
6 {
7 int integer1, integer2, sum; // declaration
8
9 cout << "Enter first integer\n"; // prompt
10 cin >> integer1; // read 1st integer
11 cout << "Enter second integer\n"; // prompt
12 cin >> integer2; // read 2nd integer
13 sum = integer1 + integer2; // assignment of sum
14 cout << "Sum is " << sum << endl; // print sum
15
16 return 0; // indicate that program ended successfully
17 }
Enter first integer
45
Enter second integer
72
Sum is 117
 2000 Prentice Hall, Inc. All rights
CSNB244 Programming II

Conditional Statements & Loops

Systems and Networking 17


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.

Systems and Networking CSNB244


CSNB244 Programming II

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)

Systems and Networking 19


CSNB244 Programming II

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).

Systems and Networking 20


CSNB244 Programming II

if…else Double-Selection Statement


if ( studentGrade >= 90 ) // 90 and above gets "A"
cout << "A";
else if ( studentGrade >= 80 ) // 80-89 gets "B"
cout << "B";
else if ( studentGrade >= 70 ) // 70-79 gets "C"
cout << "C";
else if ( studentGrade >= 60 ) // 60-69 gets "D"
cout << "D";
else // less than 60 gets "F"
cout << "F";

• If studentGrade is greater than or equal to 90, the first four


conditions are true, but only the output statement after the first
test executes. Then, the program skips the else-part of the
“outermost” if…else statement.

Systems and Networking 21


CSNB244 Programming II

if…else Double-Selection Statement


(cont.)
• The C++ compiler always associates an else with the
immediately preceding if unless told to do otherwise by
the placement of braces ({ and }).
• This behavior can lead to what’s referred to as the dangling-
else problem.
• if ( x > 5 )
if ( y > 5 )
cout << "x and y are > 5";
else
cout << "x is <= 5";
appears to indicate that if x is greater than 5, the nested if
statement determines whether y is also greater than 5.

Systems and Networking 22


CSNB244 Programming II

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.

Systems and Networking 23


CSNB244 Programming II

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

Systems and Networking CSNB244


CSNB244 Programming II

Let’s do it

Systems and Networking 26


CSNB244 Programming II

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

Systems and Networking 27


CSNB244 Programming II

Systems and Networking 28


CSNB244 Programming II

Systems and Networking 29


CSNB244 Programming II

Systems and Networking 30

You might also like