C Examples Hello World include lt iostream gt using namespace std void main int argc char argv cout ltlt Hello World ltlt endl Variables ID: 315052
Download Presentation The PPT/PDF document "ITEC 320" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
ITEC 320
C++ ExamplesSlide2
Hello World
#include <
iostream
>
using namespace
std
;
void main(
int
argc
, char**
argv
)
{
cout
<< “Hello World” <<
endl
;
}Slide3
Variables
#include <
iostream
>
#include <string>
using namespace
std
;
void main(
int
argc
, char**
argv
)
{
int
x;
double y;
char z;
string a;
}Slide4
Loops
#include <
iostream
>
#include <string>
using namespace
std
;
void main(
int
argc
, char**
argv
)
{
int
i
=0;
while(
i
<10)
{
cout
<<
i
<<
endl
;
i
++;
}
for (
int
j=0; j<10; j++)
cout
<< j <<
endl
;
}Slide5
Functions
#include <
iostream
>
#include <string>
using namespace
std
;
void print();
void main(
int
argc
, char**
argv
)
{
print();
}
void print()
{
cout
<< “Hello World” <<
endl
;
}Slide6
Parameter passing
#include <
iostream
>
#include <string>
using namespace
std
;
void print(
int
& x,
const
int
& y,
const
z);
void main(
int
argc
, char**
argv
)
{
int
x=3;
int
y=4;
int
z=5;
print(
x,y,z
);
cout
<< x << y << z <<
endl
;
}
void print(
int
& x,
const
int
& y,
const
z
)
{
x=4; //X’s value in main is changed, Y/Z cannot be changed
//A copy of Z is made but y is not copied
}Slide7
Multiple files
#
ifndef
__
header_h
_
#define __
header_h
_
void print();
#
endif
#include <
iostream
>#include <string>using namespace std;#include “header.h”void main(int argc, char** argv){ print();}
#include <iostream>#include <string>using namespace std;#include “header.h”void print(){ cout << “Hello World” << endl;}
header.h
main.cpp
print.cpp
Compile with:
g++ -o
prog
main.cpp
print.cpp
Run:
prog
or ./
prog
(windows /
unix
)Slide8
Pointers
#include <
iostream
>
#include <string>
using namespace
std
;
void main(
int
argc
, char**
argv
){ int* p = NULL; p = new int; *p = 3; cout << *p << endl;}Slide9
Arrays
#include <
iostream
>
#include <string>
using namespace
std
;
void main(
int
argc
, char**
argv
){ int first[10]; int* p = new int[10]; for (int i=0; i<10; i++) { first[
i] = i; p[i] = i; } delete [] p;}Slide10
Input
#include <
iostream
>
#include <string>
using namespace
std
;
void main(
int
argc
, char**
argv
){ int x; string line; cin >> x; //Read an integer, ignores preceding whitespace getline(cin, line); //Reads until end of line getline(cin, line, ‘\t’); //Reads up until the tab cin.ignore(200,’\n’); // Ignore 200 chars or a new line
//whichever comes first}Slide11
Input / Looping
#include <
iostream
>
#include <string>
using namespace
std
;
void main(
int
argc
, char**
argv
){ int x; cin >> x; //Read an integer, ignores preceding whitespace while (!cin.eof() && !cin.fail()) { if (x == 5) cout << “Magic number” << endl; cin
>> x; }}Prime the input before the loopOtherwise it will repeat 1 moreTime than it shouldSlide12
File I/O
#include <
fstream
>
#include <string>
using namespace
std
;
void main(
int
argc
, char**
argv
){ ifstream i(“file.txt”); ofstream o(“output.txt”); int x; i >> x; o << x << endl
; i.close(); o.close();}