Professional Documents
Culture Documents
Hufana
Speaker
Programming in C++
2/23/2008
2/23/2008
2/23/2008
Programming in C++
2/23/2008
Programming in C++
2/23/2008
Programming in C++
2/23/2008
#include <iostream>
Programming in C++
2/23/2008
Programming in C++
2/23/2008
int main ()
This line corresponds to the beginning of the
definition of the main function.
Every C++ program contains one or more
functions, one of which must be named main.
The main function is special in various ways, the
most important of which are that the function
must exist in every C++ program and it is the
(only) function that the operating system
explicitly calls.
Programming in C++
2/23/2008
2/23/2008
10
return 0;
The return statement causes the main function
to finish. return may be followed by a return
code (in our example is followed by the return
code 0).
A return code of 0 for the main function is
generally interpreted as the program worked as
expected without any errors during its execution.
This is the most usual way to end a C++ console
program.
Programming in C++
2/23/2008
11
// line comment
/* block comment */
Programming in C++
2/23/2008
12
#include <iostream>
/* Simple main function: Read two numbers
and write their sum */
int main()
{
// prompt user to enter two numbers
std::cout << "Enter two numbers:" <<
std::endl;
int v1, v2; // uninitialized
std::cin >> v1 >> v2; // read input
return 0;
}
Programming in C++
2/23/2008
13
Programming in C++
2/23/2008
14
Name
char
Description
Character or small integer.
Size*
1byte
Range*
signed: -128 to 127
unsigned: 0 to 255
signed: -32768 to 32767
unsigned: 0 to 65535
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
signed: -2147483648 to
2147483647
unsigned: 0 to 4294967295
2bytes
int
Integer.
4bytes
Long integer.
4bytes
bool
1byte
true or false
4bytes
8bytes
8bytes
2bytes
1 wide character
float
double
long double
wchar_t
Programming in C++
2/23/2008
15
Programming in C++
2/23/2008
16
Programming in C++
2/23/2008
17
Programming in C++
2/23/2008
18
int a = 0;
int a (0);
Programming in C++
2/23/2008
19
// initialization of variables
#include <iostream>
using namespace std;
int main (){
int a=5;
// initial value = 5
int b(2); // initial value = 2
int result;// initial value undetermined
a = a + 3;
result = a - b;
cout << result;
return 0;
}
Programming in C++
2/23/2008
20
Programming in C++
2/23/2008
21
Integer Numerals
Floating Point Numbers
Character and string literals
Boolean literals (true and false)
Defined constants (#define)
#define PI 3.14159265
Programming in C++
2/23/2008
22
Assignment (=)
Arithmetic operators ( +, -, *, /, % )
Compound assignment (+=, -=, *=, /=,
%=, >>=, <<=, &=, ^=, |=)
Increase and decrease (++, --)
Relational and equality operators
( ==, !=, >, <, >=, <= )
Logical operators ( !, &&, || )
Conditional operator ( ? )
Programming in C++
2/23/2008
23
Programming in C++
2/23/2008
24
Programming in C++
2/23/2008
25
// i/o example
#include<iostream>
using namespace std;
int main (){
int i;
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
return 0;
}
Programming in C++
2/23/2008
26
Programming in C++
2/23/2008
27
Example:
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
Programming in C++
2/23/2008
28
switch (expression){
case constant1: group of statements 1;
break;
case constant2: group of statements 2;
break; . . .
default: default group of statements
}
Programming in C++
2/23/2008
29
Programming in C++
2/23/2008
30
while (n>0) {
cout << n << ", ";
--n;
}
Programming in C++
2/23/2008
31
Programming in C++
2/23/2008
32
2/23/2008
33
2/23/2008
34
Type of function
Name of function
Function parameter(s)
Statements that makes up the function
Programming in C++
2/23/2008
35
// function example
#include <iostream>
using namespace std;
int addition (int a, int b){
int r; r=a+b;
return (r);
}
int main (){
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}
Function
definition
Function
call
Programming in C++
2/23/2008
36
Use void
void printmessage (){
cout << "I'm a function!";
}
int main (){
printmessage ();
return 0;
}
Programming in C++
2/23/2008
37
By value
By reference
Programming in C++
2/23/2008
38
Programming in C++
2/23/2008
39
Programming in C++
2/23/2008
40
Programming in C++
2/23/2008
41
Programming in C++
2/23/2008
42
Programming in C++
2/23/2008
43
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area (void);
} rect;
Programming in C++
2/23/2008
44
Programming in C++
2/23/2008
45
// classes example
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void set_values
(int,int);
int area () {
return (x*y);}
};
void CRectangle::set_values
(int a, int b) {
x = a; y = b;
}
int main () {
CRectangle rect;
rect.set_values (3,4);
cout << "area: " <<
rect.area();
return 0;
}
Programming in C++
2/23/2008
46
2/23/2008
47
class CRectangle {
int width, height;
public:
Constructor
CRectangle (int,int);
int area () {
return (width*height);
}
};
CRectangle::CRectangle (int a, int b) {
width = a;
height = b;
}
Programming in C++
2/23/2008
48
int main () {
CRectangle rect (3,4);
CRectangle rectb (5,6);
cout << "rect area: " <<
rect.area() << endl;
cout << "rectb area: " <<
rectb.area() << endl;
return 0;
}
Programming in C++
2/23/2008
49
Programming in C++
2/23/2008
50
class CRectangle {
int width, height;
Destructo
public:
r
CRectangle (int,int);
~CRectangle ();
int area () {return (width * height);}
};
CRectangle::CRectangle (int a, int b) {
width = a; height = b;
}
CRectangle::~CRectangle () {
delete width; delete height;
}
Programming in C++
2/23/2008
51
int main () {
CRectangle rect (3,4), rectb (5,6);
cout << "rect area: " << rect.area() <<
endl; cout << "rectb area: " <<
rectb.area() << endl;
return 0;
}
Programming in C++
2/23/2008
52
Programming in C++
2/23/2008
53
class CRectangle {
int width, height;
public:
void set_values (int, int);
int area () {return (width * height);}
friend CRectangle duplicate (CRectangle);
};
void CRectangle::set_values (int a, int b) {
width = a; height = b;
}
CRectangle duplicate (CRectangle rectparam){
CRectangle rectres;
rectres.width = rectparam.width*2;
rectres.height = rectparam.height*2;
return (rectres);
}
Programming in C++
2/23/2008
54
int main () {
CRectangle rect, rectb;
rect.set_values (2,3);
rectb = duplicate (rect);
cout << rectb.area();
return 0;
}
Notice that neither in the declaration of
duplicate() nor in its later use in main()
have we considered duplicate a member of
class CRectangle. It isn't! It simply has access
to its private and protected members without
being a member.
Programming in C++
2/23/2008
55
Programming in C++
2/23/2008
56
2/23/2008
57
Programming in C++
2/23/2008
58
class CPolygon {
protected: int width, height;
public: void set_values (int a, int b)
{width=a; height=b;}
};
class CRectangle: public CPolygon {
public: int area() { return (width *
height); }
};
class CTriangle: public CPolygon {
public: int area(){ return (width *
height / 2); }
};
Programming in C++
2/23/2008
59
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
Programming in C++
2/23/2008
60
Access
members of the
same class
yes
yes
yes
members of derived
classes
yes
yes
no
not members
yes
no
no
Programming in C++
2/23/2008
61
Programming in C++
2/23/2008
62
Programming in C++
2/23/2008
63
http://www.cplusplus.com
C++ Programming Fundamentals by
Charles River
C++ Primer, 4th Ed by Addison-Wesley
C++ Programmers Reference by McGraw
Programming in C++
2/23/2008
64