Sie sind auf Seite 1von 28

BASIC ELEMENTS

OF

C++

OBJECTIVES
To be able to use C++

input/output and formatting

To become familiar with the

C++ standard

Outcome
Know how to write your first

program in C++ using basic


elements of 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;
}

Your First Program Explained

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"

Your First Program Explained The Output


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

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;

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.
*/

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

C++ operators
Several classes of operators

Arithmetic
Relational
Logical
Assignment

Arithmetic operators
Seven arithmetic operators in C++

( E.g. int a = 7, b =

2; )
Operators

Action

Expression

Value

Substraction

a-b

Addition

a+b

Multiplication

a*b

14

Division

a/b

Modulus division

a%b

--

Decrement

a--

++

Increment

b++

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;

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;

Arithmetic Operators (Mix)


#include <iostream>
using namespace std;

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

return 0;

Relational operators
Six relational operators in C++
The results is either TRUE

(1) or FALSE (0)

E.g.
Meaning
if a = 7 and Operator
b = 5, then a < b yields
0 and a != b yields 1
<

Less than

<=

Less than or equal

>

Greater than

>=

Greater than or equal

==

Equal

!=

Not equal

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
true && false
true || false
!false

Result
false
true
true

Assignment Operators
Most commonly used assignment operator is =

(e.g. int Q = 5; )
Assignment expression abbreviations
c = c + 3; can be abbreviated as c += 3; using the addition
assignment operator
Examples of other assignment operators include:

d
e
f
g

-=
*=
/=
%=

4
5
3
9

(d
(e
(f
(g

=
=
=
=

d
e
f
g

*
/
%

4)
5)
3)
9)

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

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;
}

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.

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

#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; }

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;
}

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;
}

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; }

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

Lets Try!
Write a program that converts Fahrenheit degrees to

Celsius using the formula :


Celcius = 5/9 * (Fahrenheit 32)

Das könnte Ihnen auch gefallen