Академический Документы
Профессиональный Документы
Культура Документы
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
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
Precompiler, #-directives
18.07.2012
Janne Mntykoski
int main() {
std::cout << "Hello, world!\n";
return 0;
}
#include <iostream>
using namespace std;
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
Functions
ray
Surface Area
GiveSurfac
eArea
GiveKeh
variable
Keh
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
Keh
Surface
Area
Public variable
return EXIT_SUCCESS;
}
19.07.2012
Janne Mntykoski
13
Unnecessary
but
recommended
SetRay
ray
GiveSurfac
eArea
GiveVolume
GiveRay
The Ray value
Surface Area
variable
Volume
Janne Mntykoski
14
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
Janne Mntykoski
16
18.07.2012
Janne Mntykoski
17
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;
}
18.07.2012
#include <iostream>
#include Ball.hpp"
Definitions
of
Class
functions
ray_v
SetRay
ray_v
GiveRay
ray_v
GiveSurface
Area
Surface
Area
ray_v
GiveVolume
Volume
Janne Mntykoski
19
To find cout
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
Object creation
Initialization
Ball();
Ball ball2;
Default value(s)
Ball(float ray);
Ball ball1(2);
Parameter value(s)
Action
~Ball() {};
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;
}
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
Copy
stringCpp2= stringCpp1
Length
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;
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
Inherited,
Base class
Ball
The Ray value
Functions
Surface Area
GiveSurface
Area
SetRay
GiveRay
ray
SetColor
color
GiveVolume
Volume
GiveColor
Object interface
Janne Mntykoski
27
class Ball
Surface
Area
GiveRay
ray_v
GiveSurface
Area
Volume
GiveVolume
color_v
GiveColor
Private variable
New Color value
19.07.2012
Janne Mntykoski
Public inteface
28
14
Class ColoredBall
ColoredBall () {
ColoredBall::SetColor(1);
};
SetColor
color_v
color_v
GiveColor
Janne Mntykoski
29
Class ColoredBall
To find cout
Object ball1 created with parameters
Object ball2 created
cout
cout
cout
cout
cout
cout
cout
cout
ball2.SetColor(5);
cout << " The color of ball2 is " << ball2.GiveColor() << endl;
return EXIT_SUCCESS;
}
19.07.2012
Janne Mntykoski
30
15
C++
int abs(int);
int abs(int);
long labs(long);
long abs(long);
double fabs(double);
double abs(double);
Janne Mntykoski
31
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
Janne Mntykoski
32
16
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
Janne Mntykoski
33
C++
#include <iostream>
using namespace std;
int main()
{
int intN = 5;
int &intNRef = intN;
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;
}
intN=intM=6
floatN=floatM=5.12
Janne Mntykoski
35
int main()
{
int intN = 5;
float floatN = 6.12;
function(intN, floatN);
function(intN);
}
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)
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);
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)++
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';
}
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
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];
}
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]);
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>
State machine
STATE_START
STATE_SUCCESS
}
20.07.2012
Janne Mntykoski
48
24
13.01.2006
Janne Mntykoski
49
06.01.2008
Janne Mntykoski
50
25
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;
}
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);
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.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