Anda di halaman 1dari 27

Advanced programming

Janne Mntykoski
Janne.Mantykoski@metropolia.fi
Room B501
18.07.2012

Janne Mntykoski

Course focus
C++ programming
Advanced C programming
Focus is on programming a microcontroller
(mbed, LPC1768)

18.07.2012

Janne Mntykoski

Main contents
C++
- Class - objects
- Derived class base class inheritance
- Polymorfism easier function naming
C and C++
- Strings
- Pointers
- Structure
20.07.2012

Janne Mntykoski

WWW-sites and books


Wikipedia: C++ ja Olio-ohjelmointi
Mureakuha: cpp.mureakuha.com
Rintala ja Jokinen: Olioiden ohjelmointi C++:lla,
Talentum Vaatii C-kielen osaamisen pohjalle
Pivi Hietanen: C++ ja olio-ohjelmointi - Kattava
C++ teos
Overland: C++ Without Fear 2/E
Osa kalvojen esimerkeist suoraan Wikipediasta
tai Mureakuhasta Janne Mntykoski
06.01.2008
4

Free programming environments


Code:blocks supports both C and C++ languages
www.codeblocks.org
Dev-C++ supports both C and C++ languages
www.bloodshed.net

18.07.2012

Janne Mntykoski

Embedded programming
environment
Online C++ compiler for mbed module,
ARM Cortex-M3 LPC1768 microcontroller,
Cortex-M0 module also available
A lot of libraries for devices for mbed
www.mbed.org

18.07.2012

Janne Mntykoski

Why C++?
C++ - language is very popular
It supports modern coding technics like
object oriented programming
SystemC hardware description language
is based on C++
The powerful mbed module supports C++,
ARM Cortex-M3
18.07.2012

Janne Mntykoski

Introduction to C++
Development sterted in 1979
Main designer Bjarne Stroustrup
Expands the C language with modern
coding technics like object oriented
programming
Object oriented programming is very
useful in big coding projects
First ANSI standard in 1998, ANSI
C++, ISO
18.07.2012

Janne Mntykoski

C++ programming
C++ source files, extension .cpp

C++ header files, extension .h or .hpp

Precompiler, #-directives

Compiler, creates .obj files

Linker, combines .obj files and


adds startup code

Executable program, extension .exe or .bin

18.07.2012

Janne Mntykoski

C and C++ comparison


C style
#include <stdio.h>
main() {
printf ("Hello World!\n");
}
C++
#include <iostream>

New include files

int main() {
std::cout << "Hello, world!\n";
return 0;
}
#include <iostream>
using namespace std;

std:: needed to find cout

int main() {
cout << "Hello, world!\n";
return 0;
}

19.07.2012

Janne Mntykoski

10

#include <iostream>
using namespace std;
Objects cout and cin
void main(void)
To find cout and cin
{
char c;
Like printf in C
cout << Give characters, stop at X\n";
do {
Cout=standard output (stdout in C)
cout << ": ";
Cin=standard input (stdin in C)
cin >> c;
printf(\n , c); in C
cout << "\n " << c;
} while ( c != 'x' );
}
printf and scanf functions still exist in C++
<< Printing operator
>> Input operator

I/O operators

19.07.2012

Janne Mntykoski

11

Simple object example, circle


Object interface
New Ray value

Functions

ray

Surface Area
GiveSurfac
eArea
GiveKeh

The Ray value

variable

Keh

Variables and functions have been


combined into an object (encapsulation)
Class is the type of the object
18.07.2012

Janne Mntykoski

12

#include <iostream>
using namespace std;
const double pi = 3.141;
Constant PI
class Ball
Begin class declaration
Public inteface
{
private :
New Ray value
Not inheritable,
// Place for variables and functions
Invisible to the outside
The Ray value
public:
External interface
float ray_v;
float GiveSurfaceArea()
{ return pi*ray*ray; };
Definition of
ray_v
float GiveCF()
Class functions
{ return 2.0*pi*ray; };
};
End class declaration
GiveSurface
int main()
Area
{
Object ball1 created
Ball ball1;
Object variable
GiveKeh

Class Circle.hpp file

Object function call


ball1.ray_v = 3;
cout << The ray of ball1 is " << ball1.ray_v;
cout << " Surface area is " << ball1.GiveSurfaceArea();
cout << " Circumference is << ball1.GiveCF() << endl;

Keh

Surface
Area

Public variable

return EXIT_SUCCESS;
}
19.07.2012

Janne Mntykoski

13

Object example, ball


Object interface

New Ray value


Functions

Unnecessary
but
recommended

SetRay
ray

GiveSurfac
eArea
GiveVolume

GiveRay
The Ray value

Surface Area

variable

Volume

Variables and functions have been


combined into an object (encapsulation)
18.07.2012

Janne Mntykoski

14

Benefits from object oriented programming


C code

C++ code

The code has been divided into clear subprograms with objects:
- Easier to design
- Easier to understand
- Easier to reuse
- Easier to modify
18.07.2012

Janne Mntykoski

15

Ways to manage the code


Use state machines to organise the code
Use objects to combine variables and
functions into understandable subprograms
When to use objects?: Definitely when the
code size starts to increase
18.07.2012

Janne Mntykoski

16

Object oriented programming


Three new things:
- encapsulation -> objects and classes (type of object)
- inheritance -> class reuse
- polymorfism -> function name overloading
By encapsulation we are hiding part of the complexity of
the code -> object interface is simpler than the object
Inheritance means less coding and more code reuse
Polymorphism simplifies naming functions and their use

18.07.2012

Janne Mntykoski

17

Class Ball.hpp file


Public inteface
const double pi = 3.141;
Constant PI
class Ball
Begin class declaration
New Ray value
{
protected :
Inheritable,
The Ray value
float ray_v;
Invisible to the outside
// Place for Protected functions
SetRay
public:
External interface
// Place for Public variables, not recommended
Ball();
GiveRay
Class creators
Ball(float ray);
ray_v
Class destructor
~Ball() {};
GiveSurface
void SetRay(float raysade);
Area
float GiveRay();
Declaration of
float GiveSurfaceArea();
Class functions
GiveVolume
float GiveVolume();
};
End class declaration

Class is the type of the


object
18.07.2012

Janne Mntykoski

Volume

Surface
Area

Protected variable
18

#include <iostream>
#include Ball.hpp"
Ball::Ball();
Ball::SetRay(1.0);
};
Ball::Ball(float ray)
{
Ball::SetRay(ray);
}
void Ball::SetRay(float ray)
{
ray_m = ray;
}
float Ball::GiveRay()
{
return ray_v;
}
float Ball::GiveSurfaceArea()
{
return 4*pi*ray*ray;
}
float Ball::GiveVolume()
{
return 4.0/3.0*pi*ray*ray*ray;
}

Class Ball.cpp file

18.07.2012

#include <iostream>
#include Ball.hpp"

Class constructor with parameter


Notice
New Ray value

Definitions
of
Class
functions

ray_v

SetRay

The Ray value

ray_v

GiveRay

ray_v

GiveSurface
Area

Surface
Area

ray_v

GiveVolume

Volume

Janne Mntykoski

19

Test program for class Ball

using namespace std;


int main()
{
Ball ball1(2);
Ball ball2;

Class constructor, no parameters,


default ray=1.0

To find cout

Object ball1 created, calls class constructor with parameter


Object ball2 created, calls class constructor without parameter
Object function call

cout << The ray of ball1 is " << ball1.GiveRay();


cout << " Surface area is " << ball1.GiveSurfaceArea();
cout << " Volume is << ball1.GiveVolume() << endl;
cout << The ray of ball2 is " << ball2.GiveRay();
cout << " Surface area is " << ball2.GiveSurfaceArea();
cout << " Volume is << ball2.GiveVolume() << endl;

Test class functions

ball2.SetRay(5.4);
cout << The ray of ball2 is " << ball2.GiveRay();
cout << " Surface area is " << ball2.GiveSurfaceArea();
cout << " Volume is << ball2.GiveVolume() << endl;
return EXIT_SUCCESS;
}
18.07.2012

Janne Mntykoski

20

10

Class constructors and destructor


Constructor

Object creation

Initialization

Ball();

Ball ball2;

Default value(s)

Ball(float ray);

Ball ball1(2);

Parameter value(s)

Constructor is called when object is created


Destructor

Action

~Ball() {};

Free any reserved dynamic


memory

Destructor is called when object is deleted or


in the end of the program
18.07.2012

Janne Mntykoski

21

Complex objects
Class constants: the same for class
objects
Class wide variable: the same for class
objects
Object copying possible
Object pointers

06.01.2008

Janne Mntykoski

22

11

Program pause
C style (DOS command)

C++

system(PAUSE);

cin.get();

#include <stdio.h>

#include <iostream>
using namespace std;

Int main(void) {
printf ("Hello!\n");
system(PAUSE);
return 0;
}

int main() {
cout << "Hello!\n";
cin.get();
return 0;
}

Program will wait for ENTER to be


pressed
19.07.2012

Janne Mntykoski

23

Strings
C functions

C++

Include
library

string.h

string

Define
string

char stringC[30];
// capacity is 30 characters

string stringCpp;
// class

Compare

int strcmp(const char *stringC1,


const char *stringC2);

stringCpp2 < stringCpp1


stringCpp2 == stringCpp1

Copy

char *strcpy(char *copy,


const char *original);

stringCpp2= stringCpp1

Length

int strlen(char *stringC);

stringCpp.size

20.07.2012

Janne Mntykoski

24

12

Strings
C

C++

#include <stdio.h>
#include <string.h>

#include <iostream>
#include <string>
using namespace std;
int main () {
string str1 = Hi!, str2;

int main(void) {
char str1[30] = Hi!, str2[30];
strcpy(str2, str1);
if (strcmp(str1, str2) == 0)
printf(They are equal\n);

str2 = str1;
if (str1 == str2)
cout << They are equal\n;

printf(Length: %d, strlen(str2););

cout << Length: " << str2.size();


return 0;

return 0;
}

20.07.2012

Janne Mntykoski

25

Class String
#include <iostream>
Class string
#include <string>
To find objects cout, cin and class string
using namespace std;
int main (void) {
string str1, str2;
cout << "\nEnter a string";
cin >> str1;
Copies a string, not possible in C
str2 = str1;
cout << "\nCopy of string 1 is : " << str2;
cout << "\nEnter a string";
cin >> str2;
Required strcmp function in C
if (str1 < str2)
cout << str1 << "is less than" << str2 ;
else
cout << str2 << "is > or equal to" << str1 ;
cout << "\nThe capasity of the str2 is " << str2.capacity();
Amount of character
cout << "\nThe size (length) of the str2 is " << str2.size();
return 0;
}
19.07.2012

Janne Mntykoski

26

13

Inheritance example, ColoredBall


New Ray value

Inherited,
Base class
Ball
The Ray value

Functions

Surface Area
GiveSurface
Area

SetRay
GiveRay

ray

SetColor

color

GiveVolume

Volume

GiveColor

New Color value


The Color value
variables

Object interface

Inheritance is an easy way to reuse code


ColoredBall is called derived class
19.07.2012

Janne Mntykoski

27

Derived class ColoredBall.hpp file


#include Ball.hpp"

class Ball

New Ray value

class ColoredBall : public Ball


Inherit class Ball
{
private :
Not inheritable,
int color_v;
Invisible to the outside
// Private functions
public:
External interface
ColoredBall();
Class creators
ColoredBall(float ray, int color);
~ ColoredBall() {};
Class destructor
void SetColor(int color);
Declaration of
int GiveColor(void);
Class functions
};

The Ray value


SetRay

Surface
Area

GiveRay
ray_v
GiveSurface
Area

Volume

GiveVolume
color_v

GiveColor

Base class Ball


SetColor

Private variable
New Color value
19.07.2012

Janne Mntykoski

The Color value

Public inteface
28

14

Derived class ColoredBall.cpp file


#include <iostream>
#include ColoredBall.hpp"

Class ColoredBall

ColoredBall () {
ColoredBall::SetColor(1);
};

Class constructor, no parameters,


default Color=1.0
Notice

ColoredBall::ColoredBall (float ray, int color)


{
Ball::SetRay(ray);
ColoredBall::SetColor(color);
}
void ColoredBall::SetColor(int color)
{
color_v = color;
}

Class constructor with parameters

New Color value

SetColor

color_v

Definitions of Class functions


int ColoredBall::GiveColor(void)
{
return color_v;
}
19.07.2012

color_v

The Color value

GiveColor

Janne Mntykoski

29

Test program for derived class ColoredBall


#include <iostream>
#include ColoredBall.hpp"
using namespace std;
int main()
{
ColoredBall ball1(3.0,2);
ColoredBall ball2;

Class ColoredBall
To find cout
Object ball1 created with parameters
Object ball2 created

cout
cout
cout
cout

<< The ray of ball1 is " << ball1.GiveRay();


<< " Surface area is " << ball1.GiveSurfaceArea();
<< " Volume is << ball1.GiveVolume();
<< " Color is " << ball1.GiveColor() << endl;

cout
cout
cout
cout

<< The ray of ball2 is " << ball2.GiveRay();


<< " Surface area is " << ball2.GiveSurfaceArea();
<< " Volume is << ball2.GiveVolume();
<< " Color is " << ball2.GiveColor() << endl;

Test class functions

ball2.SetColor(5);
cout << " The color of ball2 is " << ball2.GiveColor() << endl;

Object function call

return EXIT_SUCCESS;
}
19.07.2012

Janne Mntykoski

30

15

Function overloading is polymorfism


C

C++

int abs(int);

int abs(int);

long labs(long);

long abs(long);

double fabs(double);

double abs(double);

There has to difference in parameter types


Return type difference is not enough
Polymorfism (=many forms) -> same
function name, many different functions
Operator overloading is also possible
19.07.2012

Janne Mntykoski

31

Polymorfism example, function abs


#include <iostream>
using namespace std;
Int abs( int n);
double abs( double n );

Polymorfic function declarations

int main( )
{
cout << Absolute of -10 is " << abs( -10 ) << "\n";
cout << Absolute of -10.01 is " << abs( -10.01 ) << "\n";
return 0;
}
int abs( int n )
{
return n < 0 ? -n : n ;
}

Test functions

If (n<0) then -n else n


Define functions

double abs( double n )


{
return n < 0 ? -n : n ;
}
19.07.2012

Janne Mntykoski

32

16

C and C++ comparison


C style
#include <stdio.h>
int main(void)
{
int i, j=1;
printf(Give integer: );
scanf(%d, &i);
printf(i is %d, j is %d\n, i. j);
}

Variables here in C style

C++
#include <iostream>
using namespace std;
int main()
{
int i;
cout << Give integer: ";
cin >> i;
int j = 1;
cout << i is << i << , j is << j << endl;
}
19.07.2012

Void not needed in C++ style

Variables are anywhere in C++ style

Janne Mntykoski

33

Reference vs. pointers


C style
#include <stdio.h>
int main(void)
{
int intN = 5;
int* intNpointer = &intN;

intNpointer has the memory address

printf(%d = %d\n, intN. *intNpointer);


}

*intNpointer gives the value of it

C++
#include <iostream>
using namespace std;
int main()
{
int intN = 5;
int &intNRef = intN;

Copy the memory address

cout << intN << = << intNRef << endl;

Both have the same memory address

19.07.2012

Janne Mntykoski

34

17

#include <iostream>
using namespace std;
void funktio(int &intM, float &intM);
int main()
{
int intN = 5;
float floatN = 6.12;

Reference-operator

cout << Integer is " << intN << float is << floatN << endl;

&intM=&intN, &floatM=&floatN
funktio(intN, floatM);
cout << Integer is " << intN << float is << floatN << endl;
return 0;
}

Memory address parameters

void funktio(int &intM, float &floatM)


{
intM = 6;
floatM = 5.12;
}
19.07.2012

intN=intM=6
floatN=floatM=5.12
Janne Mntykoski

35

Defaults for function parameters


#include <iostream>
using namespace std;
void function(int intM, float floatN = 1.12);

Parameter floatN has default value

int main()
{
int intN = 5;
float floatN = 6.12;
function(intN, floatN);
function(intN);

The same as function(intN, 1.12);

}
void function(int intM, float floatN)
{
cout << intN=" << intM << , floatN=" << floatN << endl;
}
20.07.2012

Janne Mntykoski

36

18

Advanced C programming

20.07.2012

Janne Mntykoski

37

String pointers
#include <stdio.h>
void main(void)
{
Array name=stringC=address of the member 0 of array
char stringC[11] = String";
char *string_pointer;
Pointer to a memory location which contains character(s)

Add 6*(size of character= 1 byte)


string_pointer = stringC + 6;
printf(stringC = %s, stringC + 6 = %s\n", stringC, string_pointer);
printf(Member?(alkio) 6 of stringC = %c\n", *string_pointer);
stringC[3] = '\0
printf(stringC = %s\n", stringC);
stringC = stringC + 1;

'\0'-characters is the end of string


Therefore here is Str printed
INCORRECT! stringC is constant

system(PAUSE);
}
20.07.2012

Janne Mntykoski

38

19

#include <stdio.h>
int main()
{
int i = 5;
int *ptr;
float array[2] = {3.14, 0.07};
float *ptr2;

Pointers
Pointer to a memory location which contains an integer
Pointer to a memory location which contains a float

ptr = &i;
printf(Value of i: %d, Value in address ptr: %d.\n", i, *ptr);
printf(Address of i: %p, value of pointer ptr %p.\n", &i, ptr);

& is address operator


i=*ptr, * is contents operator
&i=ptr

i=*ptr, * is contents operator


ptr2= array;
printf(Address of member 0 of array: %p, ptr2: %p.\n", array, ptr2);
The same address
printf(Value of member 0 of array: %f. ptr2[0]: %f.\n", *array, ptr2[0]);
*array=array[0]
ptr2 = ptr2 + 1;
Add size of float= 4 bytes
printf(Address of member 1 of array: %p, ptr2: %p.\n", array+1, ptr2);
printf(Value of member 1 of array: %f. ptr2[0]: %f.\n", array[1], ptr2[0]); array[1]=ptr2[0]
}
20.07.2012

Janne Mntykoski

39

Pointer calculations
#include <stdio.h>
int main()
{
int i = 5;
int *ptr;
int array[3] = {3, 2, 1};
int *ptr2;
*ptr++
(*ptr)++

kasvattaa osoittimen arvoa kahdella


kasvattaa osoittimen osoittaman muistipaikan sislt yhdell
sallitut operaatiot ovat +, ++, - ja

x = *ptr;
ptr++;
}

13.01.2006

OR! x = *ptr++;

Janne Mntykoski

40

20

#include <stdio.h>
struct personStruct {
char name[30];
int age;
};

Structure
Structure type personType contains
fields name and age

int main(void)
{
struct personStruct person;
int j;
printf(Give name: ");
fgets(person.name, 30, stdin);
printf(Give age: ");
scanf("%d", &(person.age));
for (j=0; j < 30; j++)
if (person.name[j] == '\n')
person.name[j] = '\0';

Read into field name in structure person


Read into field age in structure person

Remove newline \n from the end of string

}
20.07.2012

#include <stdio.h>
typedef struct personStruct {
char name[30];
int age;
} personType;

Janne Mntykoski

41

Array of structures
Type definition
Could be omitted, default=size of initialization

int main(void)
{
personType persons[2] = {{"Untamo",1},{"Juusto", 0}};
personType *personsPtr;

Initialize
Structure pointer

personsPtr = persons;
printf(Member 0: name: %s, age: %d\n", persons[0].name, persons[0].age);
printf(Member 0: name: %s, age: %d\n", personsPtr->name, personsPtr->age);
personsPtr++;
Move to the next structure
printf(Member 1: name: %s, age: %d\n", personsPtr[0].name, personsPtr[0].age);
printf(Member 1: name: %s, age: %d\n", personsPtr->name, personsPtr->age);
}
20.07.2012

Janne Mntykoski

42

21

#include <stdio.h>
#include <string.h>
void main()
Function strcmp
{
char *words[3][2]= {{"sana","word"},{"peli","game"},{"talo","house"}};
char word[30];
int i;

2-dimensional array

[rows][columns], 3 rows, 2 columns

printf(Give word: ");


fgets(word, 30, stdin);
for (i=0; i < 30; i++)
if (word[i] == '\n')
word[i] = '\0';

Read into string word


Remove newline \n from the end of string

for (i=0; i <3; i++) {


if (strcmp(word, words[i][0]) == 0)
printf("%s(suomi) -> %s(english)\n", words[i][0], words[i][1]);
if (strcmp(word, words[i][1]) == 0)
printf("%s(english) -> %s(suomi)\n", words[i][1], words[i][0]);
}

Finnish word?
English word?

}
06.01.2008

Janne Mntykoski

43

?Input file
C

C++

#include <stdio.h>
???

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
FILE *inputfile;
inputfile=fopen(input.txt,r);
if (inputfile == NULL) {
printf(File open failed!);
return 1;
}

int main(void)
{
ifstream inputfile(input.txt");
if (!inputfile) {
cout << File open failed!";
return 1;
}
char lineC[80];

char lineC[80];
while (fgets(lineC, 80, inputfile)
!= EOF) {
printf(%s, lineC);
}
fclose(inputfile);
}
06.01.2008

while (!inputfile.eof()) {
inputfile.getline(lineC, 80);
cout << lineC << endl;
}
inputfile.close();
}
Janne Mntykoski

44

22

?Output file
C

C++

#include <stdio.h>
???

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
FILE *outputfile;
outputfile=fopen(output.txt,w); //a?
if (outputfile == NULL) {
printf(File open failed!);
return 2;
}

int main(void)
{
ofstream outputfile(output.txt");
if (!outputfile) {
cout << File open failed!";
return 2;
}

char stringC[80];

char stringC[30];

printf(Give string: ");


fgets(stringC, 30, stdin);
fputs(stringC, outputfile);
fputs(Hi, outputfile);
fclose(outputfile);

printf(Give string: ");


fgets(stringC, 30, stdin);
outputfile << stringC << "Hi" << endl;
outputfile.close();
}

}
20.07.2012

Janne Mntykoski

#include <stdio.h>
union bytes {
unsigned int uintN;
unsigned char charArray[sizeof(int)];
};

45

?Union
Both fields uintN and charArray
share the same memory

int main(void)
{
union bytes y;
y.uintN = 64000;
printf(%d is in hex %X\n, y.uintN, y.uintN);
printf(Highest byte is %X, y.uintN >> (8*(sizeof(int)-1)));
printf(Highest byte is %X, y.charArray[sizeof(int)-1]);

The same output

20.07.2012

Janne Mntykoski

46

23

Bit fields
In struct ja union you can define the number of
bits used
Field has to be of type int or unsigned
struct bit_card {
unsigned nro:4;
/* 4 bits */
unsigned country:2;
/* 2 bits */
unsigned color:1;
/* 1 bit */
};

13.01.2006

Janne Mntykoski

47

Enumerated type

#include <stdio.h>

typedef enum {START, SUCCESS} state_t;


START=0, SUCCESS=1
Int main()
{
int c;
State variables
state_t current_state = START, next_state = START;
while(1) {
switch(current_state){
case START :
next_state = SUCCESS;
break;
case SUCCESS :
printf(Program terminated.\n");
getch();
return;
}
current_state=next_state;
}

State machine
STATE_START
STATE_SUCCESS

}
20.07.2012

Janne Mntykoski

48

24

Dynamic memory allocation


Memory is allocated and freed during runtime
Function malloc() reserves memory
Function free() releases the reserved
memory
They are in stdlib.h library

13.01.2006

Janne Mntykoski

49

Other additions to C++

06.01.2008

Janne Mntykoski

50

25

Other additions to C++


Classes: (Simula67)
Operator overloading
Others:
Type bool. Has two values: false and true.
Dynamic type conversions of the variables
Inline functions
Dynamic memory allocation (new, delete)

06.01.2008

Janne Mntykoski

51

Advanced I/O
C++ code
#include <iostream.h>
main()
{
// muutetaan formaattia
cout << 88 << '\n';
cout.setf( ios::hex | ios::uppercase | ios::showbase );
cout << 88 << '\n';
cout.unsetf( ios::uppercase );
cout << 88 << '\n';
return 0;
}

Change output format


Default is lowercase

Output
88
0X58
0x58

19.07.2012

Janne Mntykoski

52

26

?Output format
C

C++

Output

printf(%f\n,123.234567);
printf(%s\n, "hello);

cout << 123.234567 << '\n'; 123.234500


cout << "hello" << '\n';
hello

printf(%10f\n,123.23456
7);
printf(%10s\n, "hello);

cout.width( 10 );
cout << "hello" << '\n';

printf(%f\n,123.234567);
printf(%s\n, "hello);

cout.fill('%');
cout.width( 10 );
cout << "hello" << '\n';

?left?printf(%10s\n,
"hello);

cout.setf( ios::left );
cout.width( 10 );
cout << "hello" << '\n';

printf(%10.3f\n,123.2345 cout.width( 10 );
67);
cout.precision(3);
cout << 123.234567 << '\n';
19.07.2012

123.234500
hello

123.235

hexJanne Mntykoski

53

?I/O manipulators
C++
cout <<
123.234567 <<
'\n';
cout << "hello"
<< '\n';
cout.width( 10 );
cout << "hello"
<< '\n';
cout.fill('%');
cout.width( 10 );
cout << "hello"
<< '\n';

I/O
manipulators

Output
123.234500
hello

cout << setw( 9 ) 123.234500


<< "x";
hello
cout <<
setfill('X') <<
setw(10) << 100
<< " hi " << endl;

cout.setf(
ios::left );
cout.width( 10 );
cout << "hello"
<< '\n';
cout.width( 10 );
cout.se
123.235
cout.precision(3) tf( ios::right );
;
coutJanne
<< Mntykoski
19.07.2012
cout <<
setprecision(3);
123.234567 <<
cout <<

#includ
e
<ioman
ip.h>

54

27

Anda mungkin juga menyukai