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

Unit 5 Introduction To C++

The document introduces C++, an object-oriented programming language developed as a successor to C. It outlines key programming concepts such as user roles, programming processes, data types, variables, and operators, along with sample code demonstrating basic input and output operations. Additionally, it emphasizes the importance of proper syntax, variable naming conventions, and reserved words in C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit 5 Introduction To C++

The document introduces C++, an object-oriented programming language developed as a successor to C. It outlines key programming concepts such as user roles, programming processes, data types, variables, and operators, along with sample code demonstrating basic input and output operations. Additionally, it emphasizes the importance of proper syntax, variable naming conventions, and reserved words in C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

INTRODUCTION TO STRUCTURED

PROGRAM DESIGN

Linda Amoako Banning, PhD


INTRODUCTION
TO
PROGRAMMING
WITH C++
What is C++?

● C++ is an object-oriented language


● It is the “successor” to C, a procedural language
○ (the “++” is called the successor operator in C++)
● C was derived from a language called B which was in turn derived from BCPL
● C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs
● C++ was developed in the early 1980’s by Bjarne Stroustrup of AT&T Bell Labs.
● Most of C is a subset of C++
Some Terms To Note

■ User ■ Keywords:
■ an individual who runs ■ Words that have a special meaning in
the language.
or executes, a program ■ Cannot be used for any other
purposes
■ Programmer:
■ Identifiers:
■ an individual who ■ Made up by the programmer
creates or writes, a ■ Not part of the C++ language

program ■ Can be used to represent various


things like variables
Some Terms To Note

■ Operators ■ Punctuation:
■ Used to perform operations ■ Characters that mark the end of a
on data statement
■ Arithmetic: +, -, *, / ■ Syntax:
■ Assignment: = ■ The structure of the language
■ Punctuation: ■ Semantics:
■ Characters that mark the ■ The meanings of the constructs in
end of a statement the language
C++ Programming

A C++ program generally consists of:


○ Declarations

Define the use of various identifiers, thereby creating the


elements used by the program (computer)
○ Statements
Or executable statements representing actions the
computer will take on the user’s behalf
The Programming Process

● Define what the program is to do (fully understand


the problem on hand)
● Visualize the running on the computer
● Use design tools to create a model of the program
○ Pseudocode, flowcharts, etc
● Check the model for logical errors
● Write the program source code
● Compile the source code
The Programming Process

● Correct any errors found during the compilation


● Link the program to create an executable file
● Run the program using test data for input
● Correct any errors found while running the program,
○ *********** Repeat steps 4-10 as many times as necessary
● Validate the results of the program.
○ ***Does the program do what it is meant to do?
Sample C++ Program

#include <iostream> Preprocessor directive


using namespace std; Which namespace to be used
int main() Beginning of the main function
{ Beginning block of the main function
int number; Variable declaration

cout << “Enter a number” << endl; Output statement

cin >> number; Accept input from user

cout << “You entered: “ << number << endl; Output statement

return 0; Preprocessor directive


} End of block for main function
PARTS OF THE PROGRAM
EXPLAINED
The Header file

#include <iostream>
Compiler directive: Tells the compiler what to do before compiling
This one includes source code from another file

using namespace std;


Compiler directive: Tells the compiler to use the standard (std)
namespace. It contains all the standard C++ library functions and
objects such as cout, cin, string, etc.
The main() Statement

int main() ❖ Header for function


{ ❖ Contains data type for the return value
//Declaration ❖ Contains identifiers for function
//Statements ❖ Has a list of arguments between
return 0;
parenthesis(none for this function)
}
Braces

int main()
❖ The braces enclose the body of the
{ function
//Declaration ❖ They represent the start and end of a
//Statements function block
return 0;
}
Declaration Statements

int main() ❖ They are the variable declarations and


{ statements to be executed in the program.
❖ ‘//’ represents the start of comments in the
//Declaration
code
//Statements ❖ It is the main body or function activities of the
code.
return 0;
}
The return Statement

int main()
❖ It specifies the value that the function is
{
expected to return
//Declaration
❖ It ends in a semi-colon(;)
//Statements
❖ Almost all declaration statements end in a
return 0; semi-colon
}
Data Types

Data types define the type of data or content a variable can hold. Examples are:

❖ int - 2, 626, 30
❖ float - 3.56, 96.999
❖ char - ‘a’, ‘b’
❖ Bool - True, False
❖ Array - fruits[apple, banana, grape]
❖ Function - do_this()
❖ String - “Hello world!”
Variables

Variables are used to store data. They are unique names that can be
referenced and modified in the program and they follow unique naming
conventions. They may be a mixture of alphabets [a-z][A-Z], numbers[0-9]
and the underscores(_).

Example: Num1, sum_of_numbers, name

Improper variable names: 1num, @data

Variables are case-sensitive(Age and age point to different data)


Reserved words

They are words that are already in use by the program and therefore can not be
used as variables.

Examples:

❖ If
❖ For
❖ While
❖ Class
❖ Struct
Value Assignment

Assignment in programming is an operation that assigns the value


of an expression to a variable
Example: total = 5 + 5
5 + 5 is computed and the result of that is stored or assigned to
“total”
Therefore the variable “total” holds the value 10.
Value Assignment

When a variable is created, space is allocated for it in the computer’s


memory.

Variables of different data types will require different sizes in bytes of


memory space.
Console output

#include <iostream>
using namespace std; cout is drawn from the iostream header
library for outputting to the console.
int main()
{
int number;
The angle braces << indicate the direction
to the console
cout << “Enter a number” << endl;
cin >> number;
endl represents an end-of-line or an
cout << “You entered: “<<number<< endl;
addition of a new line.
}
Console input

#include <iostream>
using namespace std;
cin is drawn from the iostream header
library for inputting to the console.
int main()
{
int number; The angle braces >> indicate the direction
cout << “Enter a number” << endl; to the console.
cin >> number;
cout << “You entered: “ << number << endl;
}
Expected Output

#include <iostream>
using namespace std; Output:
int main()
{ Enter a number
int number; 7
cout << “Enter a number” << endl; You entered: 7
cin >> number;
cout << “You entered: “ << number <<
endl;
}
Without using namespace std

#include <iostream>
using namespace std;

int main()
{
cout << “Hello, World!” << endl;

return 0;
}
Arithmetic Operations

Mathematical calculations can also be done in programming with the result being
outputted right away or being stored in a variable to be used later.

Addition: 3 + 19

Multiplication: 5 * 2

Subtraction: 9-8

Division: 58 / 7
End

You might also like