Introduction to Programming with C++
Programming with C++
Welcome to Introduction to Programming with C++
Introduction to programming with C++
Installing Visual Studio 2017
How to install Visual Studio 2017 Community Edition
Link to page:https://visualstudio.microsoft.com/vs/older-downloads/
Starting with C++ & Programming Languages
Programming Languages
In this unit, we will examine programming languages and Visual C++. We will exam a typical C++ program and how the C++ application works.
Before we can start developing, we need to understand the process of communication between program and machine.
Programming Structure
First program “Hello World”
C++ programs follow a standard layout (In most cases). They start with Libraries, default function, and comments.
First, create a project using C++ and select an empty project.
Libraries & Pre-processor
Comments
/* A comment block should be the first thing in a file!*/
/* Comments are ignored by the compiler. They help explain the code.*/
/* It should summarize what the program does. */
/* It should identify who wrote the program and when it was written. */
Pre-Processor
Pre-processor directives should follow the comment block. A pre-processor directive in an instruction to the preprocessor (which runs before the compiler) as to where to find useful definitions used by the code in the file. For example, the following two lines tell the preprocessor to bring in all the definitions from the iostream header file provided by the system. This is where the cin and cout input and output streams come from that enable keyboard input and console output.
#include
using namespace std;
Stream insertion operator & Es’cape sequences
cout << “name”;
The console displays the value “name”
Es’cape sequences
Common elements in programming languages
What are the elements of a program -Key Words examples using, namespace, int, string, void … -Programmer-Defined Identifiers examples are name, firstName, sum, num1, numberOne … -Operators example +, _, *. / and <<, >> – Punctuation example string name, firstName, lastName; -Syntax rules of grammar use when writing a program C++ Keywords This is a list of reserved keywords in C++. Since they are used by the language, these keywords are not available for re-definition or overloading.
Input, Process and output (IPO)
Input Process & Output –
IPO Model
// IPO Input, Process and Output
//Input -> Gather input data from keyboard from files and Disk Drives
//Process the input data
//Display the results as output
// Procedural programming focus on the process ( Procedures or functions are written to process data)
Input | Process | Output |
Hours Rate totalPayCheck ask user for input hours ask user to input rate | TotalPayCheck = Hours * Rate; |
Cout << “you get paid” << totalPayCheck << endl;
You get pay 200 |
Variable Names
The name of a variable in programming should represent the purpose of the variable in the program.
For example, when you are adding two numbers.
- First Number should be call number1 or num1 or numberOne or _num1
- num1 First letter should be lower case
- _num1 you can use underscore to start the name of a variable
- numberOne If you have more than one word to describe the variable the second word should start with Upper case
- @num1 can NOT use special characters to begin the name of a variable
- 1Num can NOT begin the name with a number
Integer Data Types
Integer Data Types
// holds whole numbers as 5, 7, 230
int num1; //-2, 147.483.647 to +2,147.483.647
unsigned int; // hold only 0 up no negative numbers
short num2;// 2 bytes negative values all the way to positive -32,7698 to + 32.767
long num3; // 4 bytes – 2, 147.483.647 to + 2, 147.483.647
unsigned long num4; //hold only 0 up no negative numbers
https://www.geeksforgeeks.org/c-data-types/
Floating Data Types
Floating Data Types
Float
double
long double
hold real numbers 12.50 -3.5
all floating-point numbers are signed
float num1; //4 bytes, +-3.
double num2; //8 bytes hold only 0 up no negative numbers
long double num3;// 8 bytes
//Float Data Types – float double – long double
//hold real numbers 12.50 – 3.5
//all floating – point numbers are signed
float distance = 1.45674837E11;
double mass = 1.98E30;
long double num3 = 4.963737;
Char & String Data Types
Char & String Data Types
Character data type
This data type is used to store character value. It takes 1 byte in memory. It is used to represent letters, number or punctuation marks and a few other symbols.
These types of values are normally given in single quotes.
EXAMPLE
‘a’, ‘5’, ‘#’, etc
Character Strings
A series of character in consecutive memory locations:
“Hello”
Stored with the null terminator, \0, at the end:
Comprised of characters between the ” “
H | e | l | l | o | \0 |
Bool Data Types
Bool Data Types
Represent values that are TRUE or FALSE
Booleans are variables that are stored as small integers
False represents 0
True represents 1
Arithmetic Operators
Arithmetic Operators
//Binary Arithmetic Operators
+ addition, – subtraction, * multiplication, / division, % modulus
// input
double regularWeekPayCheck; // how much we get pay a week
double rate = 18.25;// 18.25 + 9.125 = 27.78
double hours = 40.0;
//————————-
double overtime;
double overtimeRate = 27.78;
double overtimeHours = 10.0;
//————————-
double totalWeekWages;
// module
int num1 = 13;
int num2 = 5;
int totalModule;
//process
regularWeekPayCheck = hours * rate;
// overtime
overtime = overtimeRate * overtimeHours;
//— total week wages
// process for module
totalModule = num1 % num2;//
totalWeekWages = regularWeekPayCheck + overtime;
// output
cout << “Your week pay check is:\t” << regularWeekPayCheck << endl;
cout << “Your week overtime amount is:\t” << overtime << endl;
cout << “Your total week pay check is:\t” << totalWeekWages << endl;
cout << ” you total is” << totalModule << endl;
return 0;
3 Expressions & Interactivity
Cin Object & Constants
// cin object Standard input object
// requires iostream file
// reads input from the keyboard
//information retrieved from cin with >> operator
// input is store in one or more variables
Rules of Operator Precedence
Evaluation of a Second-Degree Polynomial
To develop a better understanding of the rules of operator precedence, consider the evaluation of a second-degree polynomial y = ax 2 + bx + c:
The circled numbers under the statement indicate the order in which C++ applies the operators. There is no arithmetic operator for exponentiation in C++, so we’ve represented x 2 as x * x. We’ll soon discuss the standard library function pow (“power”) that performs exponentiation. Because of some subtle issues related to the data types required by pow, we defer a detailed explanation of pow until Chapter 6.
Suppose variables a, b, c and x in the preceding second-degree polynomial are initialized as follows: a = 2, b = 3, c = 7 and x = 5. Figure 2.11 illustrates the order in which the operators are applied and the final value of the expression.
Making Decisions
- Allows statements to be conditionally executed or skipped over
- Models the way we mentally evaluate situations:
- If I have valid ID I am welcome into the gym
- else I should try again
Relational Operators
- Boolean expressions – true or false
- Used to compare numbers to determine relative order
- Operators:
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
- Boolean expressions – true or false
- Examples:
12 > 5 is true
7 <= 5 is false
if x is 10, then
x == 10 is true,
x != 8 is true, and
x == 8 is false
Nested if Statements
- An if statement that is nested inside another if statement
- Nested if statements can be used to test more than one condition
Conclusion
Thank you for participation in this introduction to C++. There is some more to covert. If you have fun with this tutorials I would recommend you to read the suggested books and practice.
Book Suggestions: