Top Banner
BASIC ELEMENTS OF C++
28

Lec02_Basic Elements C++

Dec 25, 2015

Download

Documents

programming
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lec02_Basic Elements C++

BASIC ELEMENTS OF

C++

Page 2: Lec02_Basic Elements C++

OBJECTIVES

To be able to use C++ input/output and formatting

To become familiar with the C++ standard

Page 3: Lec02_Basic Elements C++

Outcome

Know how to write your first program in C++ using basic elements of C++.

Page 4: Lec02_Basic Elements C++

Hello C++!

// This is a C++ program. It prints the sentence:// Welcome to C++ Programming.

#include <iostream>using namespace std;

int main(){ cout << "Welcome to C++ Programming!"

<<endl; system("Pause"); return 0;}

Page 5: Lec02_Basic Elements C++

Your First Program – Explained

Page 6: Lec02_Basic Elements C++

Your First Program – Explained

#include<iostream> - read the file iostream that contains the definition for the stream input/output package.

using namespace std; - all names in the program belong to the "standard namespace"

Page 7: Lec02_Basic Elements C++

cout << "Hello World!" << endl;

• Each C++ statement ends in a semicolon.• "Hello World!" is called a string.• The endl symbol denotes an end of line marker.• OR:

cout << "Hello World!\n";

• The sequence of characters enclosed in quotations marks ("Hello, World\n") is called a string. – Escape sequence indicates a newline.

Your First Program – Explained – The Output Statement

Page 8: Lec02_Basic Elements C++

New Line \n or endl

#include <iostream>using namespace std;

int main(){ cout<< "Hello there."; cout<< "My name is Siti."<<endl<<endl; cout<< "Hello there.\n"; cout<< "My name is Siti.\n\n"; cout<< "Hello \nthere. \nMy name is Siti. \

n"; system("Pause"); return 0;}

Page 9: Lec02_Basic Elements C++

Comments

Comments can be written in two styles:Single line:

double can_volume = 0.355; // Liters in a 12-ounce can

Multiline for longer comments:

/* This program computes the volume (in liters) of a six-pack of soda cans.

*/

Page 10: Lec02_Basic Elements C++

Basic data types in C++Data type C++ keyword Range

Integer int -32768 to 32767

Long integer long -4294967296 to 4294967295

Short integer short -128 to 127

Unsigned integer unsigned 0 to 65535

Character char 0 to 255

Floating point float 6 digits of precision

Double floating point double 12 digits of precision

float amount; //Declares amount as a floating-point variable

int number; //Declares number as an integer variable

char ch; //Declares ch as a character variable

Page 11: Lec02_Basic Elements C++

C++ operators

Several classes of operators Arithmetic Relational Logical Assignment

Page 12: Lec02_Basic Elements C++

Arithmetic operators

Seven arithmetic operators in C++ ( E.g. int a = 7, b = 2; )

Operators Action

- Substraction

+ Addition

* Multiplication

/ Division

% Modulus division

- - Decrement

+ + Increment

Expression Value

a - b 5

a + b 9

a * b 14

a / b 3

a % b 1

a-- 6

b++ 3

Page 13: Lec02_Basic Elements C++

Arithmetic Operators (Integer)

#include <iostream>using namespace std;

int main(){ // Integers Expression cout << "2 + 5 = "<< 2 + 5 << endl;

cout << "4 * 3= "<< 4 * 3 << endl; cout << "5 / 2= "<< 5 / 2 << endl; return 0;}

Page 14: Lec02_Basic Elements C++

Arithmetic Operators (Float)

#include <iostream>using namespace std;

int main(){ // Floating-point expression

cout<<"34.0 - 20.0 = "<<34.0 - 20.0<<endl; cout<<"4.0 * 3.0= "<<4.0 * 3.0<<endl; cout<<"5.0 / 2.0= "<<5.0 / 2.0<<endl; return 0;}

Page 15: Lec02_Basic Elements C++

Arithmetic Operators (Mix)

#include <iostream>using namespace std;

int main(){ // Mix expression cout<<"3 / 2 + 5.5 = "<<3 / 2 +

5.5<<endl; return 0;}

Page 16: Lec02_Basic Elements C++

Relational operators

Six relational operators in C++The results is either TRUE (1) or FALSE (0)E.g. if a = 7 and b = 5, then a < b yields 0 and a != b

yields 1Operator Meaning

< Less than

<= Less than or equal

> Greater than

>= Greater than or equal

= = Equal

!= Not equal

Page 17: Lec02_Basic Elements C++

Logical Operators

&& (logical AND) Returns true if both conditions are true

|| (logical OR) Returns true if either of its conditions are true

! (logical NOT, logical negation) Reverses the truth/falsity of its condition Returns true when its condition is false

Logical operators used as conditions in loops Expression Resulttrue && false falsetrue || false true!false true

Page 18: Lec02_Basic Elements C++

Assignment Operators

Most commonly used assignment operator is = (e.g. int Q = 5; )

Assignment expression abbreviationsc = c + 3; can be abbreviated as c += 3; using the

addition assignment operator

Examples of other assignment operators include:d -= 4 (d = d - 4)e *= 5 (e = e * 5)f /= 3 (f = f / 3)g %= 9 (g = g % 9)

Page 19: Lec02_Basic Elements C++

Increment and Decrement Operators

Find the value of i and j (Assume i = 1)

int j = ++i // j is 2 , i is 2

int j = i++ // j is 1, I is 2

int j = --i // j is 0, I is 0

int j = i-- //j is 1, I is 0

Page 20: Lec02_Basic Elements C++

Type Conversion (Casting)#include <iostream>using namespace std;int main(){ cout<<"7.9 (int) =

"<<static_cast<int>(7.9)<<endl; cout<<"25 (double) =

"<<static_cast<double>(25)<<endl; cout<<"5 + 3 (double) = " <<static_cast<double>(5 + 3)<<endl; cout<<"(15) / 2 (double) = " <<static_cast<double>(15) /

2<<endl<<endl; return 0;}

Page 21: Lec02_Basic Elements C++

Allocating Memory with Constants

const double CONVERSION = 2.54;const int NO_OF_STUDENTS = 20;const char BLANK = ‘ ‘;const double PAY_RATE = 15.75;

Constant? A memory location whose content is not allowed to change during program execution.

Page 22: Lec02_Basic Elements C++

Allocating Memory with Variables

double sale;int num1, num2;char first;string str;Variable? A memory location whose content

may change during program execution.num1 = 4; num2 = 4*5-11;sale = 0.02 * 1000;first = ‘D’; str = “It is a sunny

day.”;

Page 23: Lec02_Basic Elements C++

#include <iostream>using namespace std;

int main(){

const int NO_STUD = 20;

string str; str = “All are good students”;

cout<<“Num of students is “<<NO_STUD;

cout<<str; str = “Some are so-so”; cout<<str;

return 0; }

Page 24: Lec02_Basic Elements C++

Input (Read) Statement#include <iostream>using namespace std;int main(){ int feet, inches;

cout<<“Enter two integers separated by spaces: “;

cin>> feet >> inches; cout<<endl;

cout<< “Feet = “<< feet << endl; cout<< “Inches = “<< inches << endl; system(“pause"); return 0;}

Page 25: Lec02_Basic Elements C++

Increment & Decrement Operators#include <iostream>using namespace std;int main(){ int num = 0, num2 = 0, tot = 5;

num++; num2 = num2 + 1; cout<< num <<“ “<< ++num <<endl;

tot--; cout<< tot; system("Pause"); return 0;}

Page 26: Lec02_Basic Elements C++

Preprocessor Directives

#include <iostream>#include <string>using namespace std;int main(){ int num; string name; cout<<“Enter an integer: “; cin>>num;

cout<<“Enter name: “; cin>>name; return 0; }

Page 27: Lec02_Basic Elements C++

Let’s Try!

Write a program that prompts the user to enter two test scores and then prints the average test score. (Assume test scores are decimal numbers.)

Page 28: Lec02_Basic Elements C++

Let’s Try!

Write a program that converts Fahrenheit degrees to Celsius using the formula :

Celcius = 5/9 * (Fahrenheit – 32)