Sie sind auf Seite 1von 99

TE

&

( )

.
2004


1.

5
6
6
7
8

2. C++

, ,


( , , )

3.

Private public

-
(constructors)



-

4.

9
10
10
11
12
13
16
26

31
31
32
32
33
35
36
38
40
42

47

47
48
52
54

5.

57

57
59

6.

61
61
63
3

7.


-


8.

67
67
70
71
72
74
74
75

77



void

: new delete

this

77
80
81
82
83
85
87
88

9.

91

10.

91
92

93
93
97

..... 99

1.


.
, , .

(PASCAL, C, .).
.


(objects).
,
, .
, C++ - (member functions),

.
.
(encapsulated) .


.
. .. ,
, .
. .. , ,
.
, .. ,
. , ,
,
, ..
, , .

, ,
.
.
(methods).

.
,

.
.. int () C++
:
int x;
int count;
.
()
() .
,
int . ,

.
.
.

.

(). ,
, , . , ,
, .


. ,
, , .
.
, ,
. ,
,
.


, ,
(). C++
(base class).
,
(derived classes).

, ,
. (reusability)

. ,
,
.
,
.
,
. ,

.

(polymorphism)
, ,
, .
()
, . ,
.
.
, ,
, .

C++ C
C++ C.
C. C, C++.
C C++,
. , C++
, ,
/ .

2. C++

:
#include <iostream.h>
void main()
{
cout << Hello world;
}
, main(). void
.
C++ ,
, ,
main(). main() ,
.
-
(preprocessor directive),
.


cout .
(standard output stream).
.
,
.
<< (insertion) (put
to). ,
.

:
//
/*

*/


.

, :
#include <iostream.h>
void main()
{
int qty, price, value;
cout << :;
cin >> qty;
cout << :;
cin >> price;
value = qty * price;
cout << : << value;
}
cin >> qty
.
qty. cin , C++
.
. >>
.

<<
.
,
:
#include <iostream.h>
void main()
{
float radius, perim, area;
const float PI = 3.14159;
cout << :;
cin >> radius;
perim = 2 * PI *radius;
area = PI * radius * radius;
cout <<setw(12)<<:<<setw(5)<<setprecision(2)<< perim << endl;
cout << setw(12)<<: <<setw(5) <<setprecision(2) << area << endl;
}
10

setw
.
setprecision
.
endl
<< .

char

-128

127

int

-32768

32767

long

-2147483648

2147483647

-38

Byte
1
2

38

float

3.4*10

3.4*10

double

1.7*10-308

1.7*10308

15

long double

3.4*10-4932

1.1*104932

19

10


C++ , ,
.
,

.

long double

double
float
long
int
char


, C++ .
,
,

11

,
. ,
,
,
, .

(casting)

,
.
:
#include <iostream.h>
void main()
{
int x = 25000;
x = (x * 10) / 10;
//
cout << x = << x << endl;
x = 25000 ;
x = (long(x) * 10) / 10 ; // long
cout << x = << x << endl;
}
:
x = -1214
x = 25000
, , x*10
int.
, long
250000 .

+
/
*
\
%

++

12


-
=
+=
-=
*=
/=
%=

<
>
<=
>=
==
!=

&& (and)
||
(or)
!
(not)




/ .
.


.

( )
.

if (x > 0)
cout << x is positive;

13


if (x > 0)
cout << x is positive;
else
cout << x is negative or zero;
E
if (x > 0)
cout << x is positive;
else
if (x == 0)
cout << x is zero;
else
cout << x is negative;

1 ( if)
if (grade > 8)
cout << excellent;
else
if (grade > 6)
cout << very good;
else
if (grade == 6)
cout << good;
else
if (grade == 5)
cout << pass;
else
cout << fail;
2 ( switch)
switch (grade)
{
case 9, 10: cout << excellent;
break;
case 7, 8: cout << very good;
break;
case
6: cout << good;
break;
case
5: cout << pass;
break;
default : cout << fail;
break;
}

.
14

.

.
.
,
.
:


,
.

, .

.
, .

sum = 0;
cin >> x;
while (x != 0)
{
sum = sum + x;
cin >> x;
}
cout << sum;

15


do
{
cout << 1. << endl;
cout << 2. << endl;
cout << 3. << endl;
cout << 4. << endl;
cout << :;
cin >> x;
switch (x)
{
case 1: cout << ;
break;
case 2: cout << ;
break;
case 3: cout << ;
break;
}
}while (x != 4);

sum = 0;
for (i=0; i<10; i++)
{
cin >> x;
sum = sum + x;
}
cout << sum;
(while)
.
(do-while)
,
, .
(for)
.



. C++
.
16

, main,
.
:

,
.
(.
).
(.
).
:
< > < > < >
{
< >
< >
}
,
, .
return.
,
void .


, , return,
.
#include <iostream.h>
int athroisma(int x, int y); //
main()
{
int a,b,c;
cout << 1 :;
cin >> a;
cout << 2 :;
cin >> b;
c = athroisma(a,b); //
17

cout << << a << << b << << c;


}
//
int athroisma(int x, int y)
{
int z; //
z = x + y;
return z ;
}
,
,
. ,

c = athroisma(a,b);

return.
, , x
y. a b
a b.
a

12

17

12

17

, x y,
a b. .

(call by value).

, athroisma
, , (
).
#include <iostream.h>
void athroisma(int x, int y);

//

18

main()
{
int a,b,c;
cout << 1 :;
cin >> a;
cout << 2 :;
cin >> b;
athroisma(a,b); //
return 0;
}
//
void athroisma(int x, int y)
{
int z; //
z = x + y;
cout << << x << << y << << z;
}


,
(modules) .
..

19

, .
,
,
. ,
.
.
, .

.

.
#include <iostream.h>
#define N 10
struct employee
{
int am;
char name[20];
int hours;
int payrate;
int misthos;
};
typedef struct employee Ergaz;
void eisagogiDedomenon(Ergaz p[N]);
int ypologismosMisthou(int hrs, int prt);
void emfanisiApotelesmaton(Ergaz p[]);
main()
{
Ergaz emp[N];
eisagogiDedomenon(emp);
emfanisiApotelesmaton(emp);
getch();
return 0;
}
void eisagogiDedomenon(Ergaz p[N])
{
int i;
for (i=0; i<N; i++)
{
cout << "Ergazomenos << i+1 << endl;

20

cout << ============" << endl;


cout << "A.M.:";
cin >> p[i].am;
cout << "Name:";
cin >> p[i].name;
cout << "Ores:;
cin >> p[i].hours;
cout << "Oromisthio:";
cin >> p[i].payrate;
//
p[i].misthos = ypologismosMisthou(p[i].hours, p[i].payrate);
cout << endl;
}
}
int ypologismosMisthou(int hrs, int prt)
{
int sal;
sal = hrs * prt;
return sal;
};
void emfanisiApotelesmaton(Ergaz p[N])
{
int i;
for (i=0; i<N; i++)
{
cout << endl << A.M. : << p[i].am << endl;
cout << " Name : << p[i].name << endl;
cout << " Ores: << p[i].hours << endl;
cout << " Oromisthio: << p[i].payrate << endl;
cout << " Misthos: << p[i].misthos << endl;
}
}

, ,
.
.
.

.

. ,
21

. ,


, .
,
.

)

:
n! = 1*2*3*..(n-1)*n

:
int factorial(int n)
{
int i,f;
f = 1;
for (i=2; i<=n; i++)
f = f*i;
return f;
}

:
n! = 1
n = 0,
= n*(n-1)! n > 0

:
int factorial(int n)
{
int f;
if (n = = 0)
f = 1;
else
f = n*factorial(n-1);
return f;
}

, .. n = 4:

22

:
f=1
i=2
f = 1*2 = 2
i=3
f = 2*3 = 6
i=4
f = 6*4 = 24
:
factorial(4) = 4*factorial(3)
factorial(3) = 3*factorial(2)
factorial(2) = 2*factorial(1)
factorial(1) = 1*factorial(0)
factorial(0) = 1
, 1
factorial(1) = 1.
factorial(2) = 2*1 = 2
factorial(3) = 3*2 = 6
factorial(4) = 4*6 = 24
)

:
xn = x*x*x*..*x, n

:
int power(int x, int n)
{
int i,p;
p = 1;
for (i=1; i<=n; i++)
p = p*x;
return p;
}

:
xn = 1
= x* xn-1

n = 0,
n > 0
23

:
int power(int x, int n)
{
int p;
if (n = = 0)
p = 1;
else
p = x*power(x,n-1);
return p;
}

C ,
.
, ,
, swap.

swap(a,b);
swap
void swap(int x, int y)
{
int temp ;
temp = x ;
x=y;
y = temp ;
}

57

75

temp
5

a b,
x y, (call by
value).
a b,
:
swap(&a, &b);
& , &a &b
,
a b .

24

void swap(int *pa, int *pb)


{
int temp ;
temp = *pa ;
*pa = *pb ;
*pb = temp ;
}

pa

57

75

pb

temp
5



, . C++

. .
- . ,
,
,
.

. ,

.
main()
{
int a, b;
swap(a,b);
}
void swap(int& x, int& y)
{
int temp ;
temp = x ;
x=y;
y = temp ;
}

a
x

b
y

57

75

temp
5

// x y a b.

25

&
.
, :
#include <iostream.h>
#include <conio.h>
main()
{
int a, &x=a, *d; // x a
// d
a = 15;
d = &a;
// a d
cout << "a = " << a << endl;
cout << "x = " << x << endl;
cout << "address a = " << d << endl;
getch();
}
:
a = 15
x = 15
address a = 0063FE00


(overloading)
.
:

void starline()
45 .

void charline(char ch)


45 .
.

void repchar(char ch, int n)


.
.

,
.
, .
.
:

26

#include <iostream.h>
void repchar()
void repchar(char ch)
void repchar(char ch, int n)
main()
{
repchar();
repchar(+);
repchar($,25);
}
void repchar()
{
int i;
for (i=0; i<45; i++)
cout << *;
cout << endl;
}
void repchar(char ch)
{
int i;
for (i=0; i<45; i++)
cout << ch;
cout << endl;
}
void repchar(char ch, int n)
{
int i;
for (i=0; i<n; i++)
cout << ch;
cout << endl;
}
:
********************************************
+++++++++++++++++++++++++++++++++++++++
$$$$$$$$$$$$$$$$$$$$$$$$
.
, .

.

27

,
.
..
void display(char ch)
{
cout << : << endl;
for(i=0; i<10; i++)
cout << ch;
cout << endl;
}
void display(int n)
{
cout << : << endl;
for(i=1; i<=n; i++)
cout << i << endl;
}
,
.
.

,
.

.

.

.
:
#include <iostream.h>
void repchar(char ch=*, int n=45)
main()
{
repchar();
repchar(+);
repchar($,25);
}

28

void repchar(char ch, int n)


{
int i;
for (i=0; i<n; i++)
cout << ch;
cout << endl;
}
:
********************************************
+++++++++++++++++++++++++++++++++++++++
$$$$$$$$$$$$$$$$$$$$$$$$
, ,
, ,
. , ,
, (
+ ch 45
n). ,
.

.

29

30

3.


. ,
C++.
#include <iostream.h>
class Person
{
private:
char name[30];
int age;
public:
void readData()
{
cout << Enter name:;
cin >> name;
cout << Enter age:;
cin >> age;
}
void printData()
{
cout << The name of the person is << name << endl;
cout << The age of the person is << age << endl;
}
};
//
void main()
{
Person p1, p2;
//
p1.readData();
p1.printData();
p2.readData();
p2.printData();

// -
//

}
Person .

. -

- .
, ,
.

31

data 1
data 2
.
.
.

function1()
function2()
.
.
.

Private public
: private public.
,
, (data hiding).
,
.
private ().
. ,
(public) , .
,
.


main()
Person p1, p2;
Person. ,
, , ,
, ,
.
.

32

-
- readData():
p1.readData();
p2.readData();
- ,
.
.-
,
-
.
readData()
p1.readData();
- readData() p1. ,
, ,

name age .

p2.readData();
- p2.
..
Person

p1

p2
name

Name
Age

age

, printData()
:
p1.printData();
p2.printData();

33


,
.
,
-. :
#include <iostream.h>
#include <string.h>
class Person
{
private:
char name[30];
int age;
public:
void setData(char name1[], int age1)
{
strcpy(name, name1);
age = age1;
}
void printData()
{
cout << The name of the person is << name << endl;
cout << The age of the person is << age << endl;
}
};
//
void main()
{
Person p;
p.setData(PAPADOPOULOS, 25);
p.printData();
}
p. ,
p.setData(PAPADOPOULOS, 25);
- setData(),
PAPADOPOULOS 25 name1 age1
. , ,
.
P
Name
PAPADOPOULOS
Age
25

34

(constructors)

. , ,
, ,
-.

-
(constructor). -
, .

, Account
balance, . ,
, -
- , ,
.
, ,
0. , ,
Account(), .
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account()
{
balance = 0;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void deposit(float money)
{
balance += money;
}

35

float getBalance()
{
return balance;
}
};
main()
{
Account ac;
ac.deposit(100.0);
cout << : << ac.getBalance() << endl;
ac.withdraw(70.0) ;
cout << : << ac.getBalance() << endl;
}

. , .
(destructors)

. , ,
(destructor),
.
, ~.
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account()
{
balance = 0;
}
~Account()
//
{}
.
.
.
};

.

36

(constructor overloading)

,
.
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account()
//
{
balance = 0;
}
Account(float balance1) //
{
balance = balance1;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void deposit(float money)
{
balance += money;
}
float getBalance()
{
return balance;
}
};
main()
{
Account ac1, ac2(50.0), ac3(100.0);
cout << ac1: << ac1.getBalance() << endl;
cout << ac2: << ac2.getBalance() << endl;
cout << ac3: << ac3.getBalance() << endl;
}
,
.
Account ac1, ac2(50.0), ac3(100.0);

37

.
.
balance1,
50.0 100.0
.
,
.

(overloading).
,

.


, - addBalance(),
.
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account()
//
{
balance = 0;
}
Account(float balance1) //
{
balance = balance1;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void deposit(float money)
{
balance += money;
}
float getBalance()
{

38

return balance;
}
void addBalance(Account x, Account y)
{
balance = x.balance + y.balance;
}
};
main()
{
Account ac1(100.0), ac2(70.0), ac3;
ac3.addBalance(ac1, ac2);
cout << ac1: << ac1.getBalance() << endl;
cout << ac2: << ac2.getBalance() << endl;
cout << : << ac3.getBalance() << endl;
}
main()
ac3.addBalance(ac1, ac2);
ac1 ac2 x y
.

x.balance y.balance. , , ,
.

-, ac3.

39

ac3
balance
- ac3

ac3.addBalance(ac1, ac2)
ac2.balance
ac1.balance
ac1
balance

ac2
balance



.
.
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account()
//
{
balance = 0;
}
Account(float balance1) //
{
balance = balance1;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}

40

void deposit(float money)


{
balance += money;
}
float getBalance()
{
return balance;
}
Account sumBalance(Account ac)
{
Account temp;
temp.balance = balance + ac.balance;
return temp;
}
};
main()
{
Account ac1(100.0), ac2(70.0), ac3;
ac3 = ac1.sumBalance(ac2);
cout << ac1: << ac1.getBalance() << endl;
cout << ac2: << ac2.getBalance() << endl;
cout << : << ac3.getBalance() << endl;
}
,

, .
, sumBalance()
, . ac2 ac.


ac1.
temp, . , return,

ac3.

c3 = ac1.sumBalance(ac2);

41

temp
Balance
temp ac3
return temp;
temp.balance
ac3 = ac1.sumBalance(ac2)
ac2.balance
balance
ac1
balance

ac2
balance

-
. , ,

.
, ,
- ,
. , ,
:
..
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account();
Account(float balance1);
void withdraw(float money);
void deposit(float money);
float getBalance();
Account sumBalance(Account ac);
};

42

Account::Account()
{
balance = 0;
}
Account::Account(float balance1) //
{
balance = balance1;
}
void Account::withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void Account::deposit(float money)
{
balance += money;
}
float Account::getBalance()
{
return balance;
}
Account Account::sumBalance(Account ac)
{
Account temp;
temp.balance = balance + ac.balance;
return temp;
}
main()
{
Account ac1(100.0), ac2(70.0), ac3;
ac3 = ac1.sumBalance(ac2);
cout << ac1: << ac1.getBalance() << endl;
cout << ac2: << ac2.getBalance() << endl;
cout << : << ac3.getBalance() << endl;
}

::,

(scope resolution

operator),
.

(private),

43

. (static),
,
.
.
,
count Account,
.
.
#include <iostream.h>
class Account
{
private:
static int count = 0;
float balance;
public:
Account()
//
{
count++;
balance = 0;
}
Account(float balance1) //
{
count++;
balance = balance1;
}
int getCount()
{
return count;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void deposit(float money)
{
balance += money;
}
float getBalance()
{
return balance;
}
Account sumBalance(Account ac)
{

44

Account temp;
temp.balance = balance + ac.balance;
return temp;
}
};
main()
{
Account ac1(100.0), ac2(70.0), ac3;
ac3 = ac1.sumBalance(ac2);
cout << ac1: << ac1.getBalance() << endl;
cout << ac2: << ac2.getBalance() << endl;
cout << ac3: << ac3.getBalance() << endl;
cout << : << ac1.getCount() << endl;
cout << : << ac2.getCount() << endl;
cout << : << ac3.getCount() << endl;
}
Account
count. main()
count . getCount()
, count, 3
. count ,
1
.

45

46

4.

.

,
.

.

( , .).

.

(indexes) .
,
, - . C++
, ,
, . ,
(one-dimensional)
(two-dimensional) .

p

#define N 10
main()
{
int p[N];
.
.
.
.
}

47

.
for (i=0; i<N; i++)
cin >> p[i];
.
for (i=0; i<N; i++)
cout << p[i];
.
sum = 0;
for (i=0; i<N; i++)
sum = sum + p[i]);


.
#include <stdio.h>
#define N 10
main()
{
int p[N];
int i, art = 0, per = 0 ;
for (i=0; i<N; i++)
cin >> p[i];
for (i=0; i<N; i++)
if (p[i] % 2 == 0)
art = art + 1;
else
per = per + 1;
cout << = << art << endl;
cout << = << per << endl;
}

48


,
.
,
,
.
, .
p 0
0

1
2
3
4
5
6
7

#define M 8
#define N 10
main()
{
int p[M][N];
.
.
.
.
}
.
for (i=0; i<M; i++)
for (j=0; j<N; j++)
cin >> p[i][j];

49

.
for (i=0; i<M; i++)
for (j=0; j<N; j++)
cout << p[i][j];
.
sum = 0;
for (i=0; i<; i++)
for (j=0; j<N; j++)
sum = sum + p[i][j]);
.
for (i=0; i<; i++)
{
row[i] = 0;
for (j=0; j<N; j++)
row[i] = row[i] + p[i][j];
}
.
for (j=0; j<N; j++)
{
col[j] = 0;
for (i=0; i<; i++)
col[j] = col[j] + p[i][j];
}

row

16

21

32

21

41

55

210

28

38

13

51

14

23

11

203

17

67

22

40

30

12

54

258

20

40

10

13

33

42

67

11

246

21

34

48

29

26

15

32

22

17

249

23

57

34

12

65

13

87

307

23

11

15

18

22

31

17

158

19

17

12

71

28

37

44

15

254

col

119 220 159 181 191 198 210 199 198 210

1885 sum

50


:
5
5
12
12
5x12
.
,
, , ,
.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#define M 5
#define N 12
main()
{
char cities[M][80] = {"KAVALA", "DRAMA", "THESSALONIKI",
"SERRES", "LARISSA"};
char months[N][80] = {"JANUARY", "FEBRUARY", "MARCH", "APRIL",
"MAY", "JUNE", "JULY", "AUGUST",
"SEPTEMBER", "OCTOBER", "NOVEMBER",
"DECEMBER"};
float temp[M][N] ={{15.6,13.8,16.9,18.2,19.3,26.7,29.5,33.4,22.5,18.4,17.5,14.2},
{13.5,12.9,15.8,17.3,18.2,24.9,28.9,30.1,21.7,17.1,15.8,13.8},
{15.9,14.2,17.3,19.4,21.5,17.9,30.6,34.2,24.1,19.6,18.2,14.7},
{16.3,15.9,17.8,19.8,22.2,18.3,30.8,35.1,25.5,20.5,18.8,15.3},
{18.3,17.1,19.6,22.4,25.3,30.2,32.4,36.3,31.5,22.7,21.5,19.4}
};
float mintemp, maxtemp;
int i,j, row, col;
for (i=0; i<M; i++)
{
cout << cities[i];
for (j=0; j<N; j++)
cout << temp[i][j];
cout << endl;
}
mintemp = temp[0][0];
row = 0;
col = 0;
for (i=0; i<M; i++)
for (j=0; j<N; j++)

51

if (temp[i][j] < mintemp)


{
mintemp = temp[i][j];
row = i;
col = j;
}
cout << endl << Minimum temperature = << mintemp << endl;
cout << "City = << cities[row] << endl;
cout << "Month = " << months[col] << endl;
maxtemp = temp[0][0];
row = 0;
col = 0;
for (i=0; i<M; i++)
for (j=0; j<N; j++)
if (temp[i][j] > maxtemp)
{
maxtemp = temp[i][j];
row = i;
col = j;
}
cout << endl << "Maximum temperature = " << maxtemp << endl;
cout << "City = " << cities[row] << endl;
cout << "Month = << months[col] << endl;
getch();
return 0;
}


.
, Student

8 8 .
#include <iostream.h>
const int N = 8;
class Student
{
private:
int am;
float vath[N];
public:
Student()
{
int i;
am = 0;
for (i=0; i<N; i++)
vath[i] = 0;

52

}
void readData()
{
int i;
cout << ..:;
cin >> am;
cout << :;
for (i=0; i<N; i++)
cin >> vath[i];
}
float getAvg()
{
int i;
float sum = 0, avg;
for (i=0; i<N; i++)
sum += vath[i];
avg = sum/N;
return avg;
}
float getMin()
{
int i;
float min;
min = vath[0];
for (i=1; i<N; i++)
if (vath[i] < min)
min = vath[i];
return min;
}
float getMax()
{
int i;
float max;
max = vath[0];
for (i=1; i<N; i++)
if (vath[i] > min)
max = vath[i];
return max;
}
};
main()
{
Student s1;
s1.readData();
cout << = << s1.getAvg() << endl;
cout << = << s1.getMax() << endl;
cout << = << s1.getMin() << endl;
}

53


,
, .
#include <iostream.h>
const int N = 10;
class Employee
{
private:
int am;
char name[20];
float misthos;
public:
void readData()
{
cout << ..:;
cin >> am;
cout << :;
cin >> name;
cout << :;
cin >> misthos;
}
void printData()
{
cout << ..: << am << endl;
cout << : << name << endl;
cout << : << misthos << endl;
}
};
main()
{
Employee emp[N];
int i, armit;
bool found = false;
cout << << N << : << endl;
for (i=0; i<N; i++)
{
cout << endl << << i+1 << : << endl;
emp[i].readData();
}
cout << .. :;
cin >> armit;
i = 0;
found = false;
while (i<N && found == false)

54

{
if (emp[i].am == armit)
found = true;
else
i++;
}
if (found == true)
emp[i].printData();
else
cout << . << endl;
}
emp
Employee. , for,
10 ,
readData(). ,
. ,
.

55

56

5. (operator overloading)

.
, .. +, -, *, >, <, ==,
.
,
a=b+c
, int
float.
, a, b c . ,
(data type
conversion),
int float. ,
.


Account
, -:
ac3 = ac1.sumBalance(ac2);
+ ,
:
ac3 = ac1 + ac2;
, :
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account()
{
balance = 0;
}
Account(float balance1)
{
balance = balance1;
}
void withdraw(float money)
{
if (money <= balance)
57

balance = balance money;


else
cout << ! << endl;
}
void deposit(float money)
{
balance += money;
}
float getBalance()
{
return balance;
}
Account operator + (Account ac)
{
Account temp;
temp.balance = balance + ac.balance;
return temp;
}
};
main()
{
Account ac1(100.0), ac2(70.0), ac3;
ac3 = ac1 + ac2;
cout << ac1: << ac1.getBalance() << endl;
cout << ac2: << ac2.getBalance() << endl;
cout << : << ac3.getBalance() << endl;
}
+ ,
operator. ,
,
operator . ,
.
main()
ac3 = ac1 + ac2;
+ ( ac1 ac2
), ac1 ac2
ac3.
, ,
(.. ac2). ,
(.. ac1)
.
58

,
,
.



. ,
> ( ) , Account,
:
#include <iostream.h>
class Account
{
private:
float balance;
public:
Account()
{
balance = 0;
}
Account(float balance1)
{
balance = balance1;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void deposit(float money)
{
balance += money;
}
float getBalance()
{
return balance;
}
bool operator > (Account ac)
{
if (balance > ac.balance)
return true;
else
return false;
}
};

59

main()
{
Account ac1(100.0), ac2(70.0);
if (ac1 > ac2)
cout << ac1 . << endl;
else
cout << ac2 . << endl;
}

60

6.

x = y;
x y .. ,
.
,
a1 = a2 + a3
a1, a2, a3 ,
.
,
, ,
.
, ,
, (casting).
.



.
#include <iostream.h>
const float MTF = 3.28033;
class EngDist
{
private:
int feet;
float inches;
public:
EngDist()
{
feet = 0;
inches = 0;
}
EngDist(float meters) //
{
// ,
float ft;
ft = MTF * meters;
feet = int(ft);
inches = 12 * (ft - feet);
}
EngDist(int feet1, float inches1)
{
feet = feet1;
61

inches = inches1;
}
void readDist()
{
cout << Give feet:;
cin >> feet;
cout << Give inches:;
cin >> inches;
}
void printDist()
{
cout << feet << feet, << inches << inches. << endl;
}
operator float()
{
float meters;
meters = (feet + inches/12) / MTF;
return meters;
}
};
main()
{
EngDist d1, d2(5, 10.0);
float metr;
d1 = 1.95;
cout << d1 = ;
d1.printDist() ;

//
// EngDist

metr = d2 ;

//
// EngDist
cout << d2 = << metr << . << endl;
}
, .. float-
.. EngDist-
float.
..
EngDist(float meters) //
{
// ,
float ft;
ft = MTF * meters;
feet = int(ft);
inches = 12 * (ft - feet);
}

d1 = 1.95;

62

, , ,
:
operator float()
{
float meters;
meters = (feet + inches/12) / MTF;
return meters;
}

metr = d2 ;
,
, ,
=.

, ,


,
, .
,
(. =),
,
(. =).
,
( )
( ).
1)
#include <iostream.h>
const float MTF = 3.28033;
class EngDist
{
private:
int feet;
float inches;
public:
EngDist()
{
feet = 0;
inches = 0;
}

63

EngDist(int feet1, float inches1)


{
feet = feet1;
inches = inches1;
}
void printDist()
{
cout << feet << feet, << inches << inches. << endl;
}
int getFeet()
{
return feet;
}
float getInches()
{
return inches;
}
};
class GrDist
{
private:
int m;
float cm;
public:
GrDist()
{
m = 0;
cm = 0;
}
GrDist(int m1, float cm1)
{
m = m1;
cm = cm1;
}
GrDist(EngDist e)
{
int ft;
float in, mf;
ft = e.getFeet();
in = e.getInches();
mf = (ft + in/12) / MTF;
m = int(mf);
cm = (mf m) * 100;
}
void printDist()
{
cout << m << meters, << cm << centimetres. << endl;
}
};

64

main()
{
GrDist gr;
EngDist eng(5, 10.0);
gr = eng;
cout << English distance = ;
eng.printDist();
cout << Greek Distance = ;
gr.printDist();
}
2)
#include <iostream.h>
const float MTF = 3.28033;
class GrDist
{
private:
int m;
float cm;
public:
GrDist()
{
m = 0;
cm = 0;
}
GrDist(int m1, float cm1)
{
m = m1;
cm = cm1;
}
void printDist()
{
cout << m << meters, << cm << centimetres. << endl;
}
};
class EngDist
{
private:
int feet;
float inches;
public:
EngDist()
{
feet = 0;
inches = 0;
}

65

EngDist(int feet1, float inches1)


{
feet = feet1;
inches = inches1;
}
void printDist()
{
cout << feet << feet, << inches << inches. << endl;
}
operator GrDist()
{
int met;
float ekat, mf;
mf = (feet+inches/12)/MTF;
met = int(mf);
ekat = (mf-met)*100;
return GrDist(met, ekat);
}
};
main()
{
GrDist gr;
EngDist eng(5, 10.0);
gr = eng;
cout << English distance = ;
eng.printDist();
cout << Greek Distance = ;
gr.printDist();
}

66

7.

.
.
(derived class),
(base class).

.
, .
.
,
.



67

#include <iostream.h>
class Account
{
protected:
float balance;
public:
Account()
{
balance = 0;
}
Account(float balance1)
{
balance = balance1;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void deposit(float money)
{
balance += money;
}
float getBalance()
{
return balance;
}
};
class AccInter : public Account
{
public:
void interest()
{
balance += balance*0.1;
}
};
main()
{
AccInter a1;
cout << a1 = << a1.getBalance();
a1.deposit(100);
cout << a1 = << a1.getBalance();
a1.interest();
cout << a1 = << a1.getBalance();
}
68

, Account AccInter.

class AccInter : public Account


. AccInter
Account.
main() AccInter:
AccInter a1;
0,
AccInter.
,
.
a1 AccInter
deposit() getBalance() Account. ,
, -
a1 -
.
balance Account protected.
-
.
, ..
main().

public
protected
private

.
. ,
, main()
Account a2;
-
, interest()
AccInter.

69


,
a1 , .
,
,
. ,
.
.
.
.
class AccInter : public Account
{
public:
AccInter() : Account()
{}
AccInter(float bal) : Account(bal)
{}
void interest()
{
balance += balance*0.1;
}
};

:
main()
{
AccInter a1, a2(100);
.
.
.
}
main(),
.
AccInter a1;

AccInter() : Account()
AccInter()
Account() .
,

70

AccInter a2(100);

AccInter(float bal) : Account(bal)
AccInter()
Account()
.

-
-
.
, deposit() withdraw()

. , ,
. , ,
.
>, , ,
:
.
.
.
class AccInter : public Account
{
public:
AccInter() : Account()
{}
AccInter(float bal) : Account(bal)
{}
void interest()
{
balance += balance*0.1;
}
void deposit(float money)
{
if (money>0)
Account::deposit(money);
else
cout << .;
}
void withdraw(float money)
{
if (money>0)

71

Account::deposit(money);
else
cout << .;
}
};
main()
{
AccInter a1;
a1.deposit(100);
cout << a1 = << a1.getBalance();
a1.deposit(-10);
cout << a1 = << a1.getBalance();
a1.withdraw(50);
cout << a1 = << a1.getBalance();
a1.withdraw(-5);
cout << a1 = << a1.getBalance();
}

a1.deposit(100);
deposit(),
, .
,
Account::deposit(money);
deposit()
.
withdraw(). ,
,
(
). , , deposit()
withdraw()

, ::,

.

72

,
:
#include <iostream.h>
class A
//
{
private:
int privdataA;
protected:
int protdataA;
public:
int pubdataA;
};
class B : public A
//
{
public:
void funct( )
{
int a;
a = privdataA;
// :
a = protdataA;
//
a = pubdataA;
//
}
};
class C : private A //
{
public:
void funct( )
{
int a;
a = privdataA; // :
a = protdataA; //
a = pubdataA; //
}
};
main( )
{
int a;
B objB;

73

a = objB.privdataA;
a = objB.protdataA;
a = objB.pubdataA;
C objC;
a = objC.privdataA;
a = objC.protdataA;
a = objC.pubdataA;

// :
// :
//
// :
// :
// :

}
A ,
. , ,
, B C
.
, -
. ,

.

, (.. B).
(.. C)
.
,
(private).


.
..
class A
{};
class B : public A
{};
class C : public B
{};


.
..
class A
{};
class B
74

{};
class C : public A, public B
{};
, A B C,
.

.
.
..
class A
{
b;
};
class B
{};

#include <iostream.h>
#include <string.h>
class Account
{
protected:
float balance;
public:
Account()
{
balance = 0;
}
Account(float balance1)
{
balance = balance1;
}
void withdraw(float money)
{
if (money <= balance)
balance = balance money;
else
cout << ! << endl;
}
void deposit(float money)
{
75

balance += money;
}
float getBalance()
{
return balance;
}
};
class Customer
{
private:
int number;
char name[20];
Account a;
public:
Customer()
{}
Customer(int number1, char name1[], float bal):Account(bal)
{
number = number1;
strcpy(name, name1);
}
void getMoney()
{
float money;
cout << :;
cin >> money;
a.withdraw(money);
}
void putMoney()
{
float money;
cout << :;
cin >> money;
a.deposit(money);
}
void balanceReport()
{
cout << = << a.getBalance();
}
};
main()
{
Customer c;
c.balanceReport();
c.putMoney();
c.balanceReport();
c.getMoney();
c.balanceReport();
}

76

8.

.

C++.
,
,
, ..
.
:

.
,
. ,
. ,
.

77

.
.
2499

.
.

2500

2502

2501
2502

2503
.
.
.

.
.
.

(pointer operators)

(address operator) &

(indirection operaror) *

& ,
.
* &, . ,
,
, .

int i;

i = 7;

1000
1001

int *p;

p = &i;

1000

1002
1003

int j;

j = *p;

1004

C:
main()
{
int i;
int *p;
int j;

// p

78

i = 7;
p = &i; // p i
j = *p; // j ,
}
// p (. i)
i
p

1000

1000

j
7


int *p, *q;
int i = 7, j = 8 ;
a) p = &i; // i p,
// p i .
q = &j; // j q,
// q j .

7
q

7
q

b) p = q; // q p,
// q p .

i
7

i
7

79

c) *p = *q; // q,
// p.

i
7

i
8
j
8



struct imerominia
{
int mera;
int minas;
int etos;
};

struct imerominia simera, *imp;
simera struct imerominia.

imp struct

imerominia. ,
imp = &simera;

simera, imp.
imp
simera
mera

15

minas

etos

2003


(*imp).mera = 15;
80

15 mera simera.
.
*.
, ->, o
(structure pointer operator).
,
imp->mera = 15;
imp->minas = 2;
imp->etos = 2003;

// (*imp).mera = 15;
// (*imp).minas = 2;
// (*imp).etos = 2003;


,
struct deiktes
{

d1
d2

int *d1;
int *d2;

i
5

};
j
main()
{
struct deiktes x;
int i = 5, j;

-8

x.d1 = &i; // i x.d1


x.d2 = &j; // j x.d2
*x.d2 = -8; // 8
// x.d2
}
x.d2 = -8, .
*.
,
*(x.d2) = -8;
.


,
,
(linked lists).
struct node
{

81

int data;
struct node *next;
};

data next.

, .
,
struct node *next;
next node node.
-
,
.
..
struct node x, y, z;
int j;
x.data = 1 ;
y.data = 2 ;
z.data = 3;
x.next = &y; // x.next y
y.next = &z; // y.next z
j = x.next->data; // data
// x.next j.
// j = y.data

data
next

x
1

data
next

y
2

data
next

z
3

j
2

void
, ,
. ..
float int. ,
.
void *ptr;

82


// void
#include <iostream.h>
main()
{
int intvar;
float flovar;
int *ptrint;
// int
float *ptrflo;
// float
void *ptrvoid;
// void
ptrint = &intvar;
// ptrint = &flovar;

// , int* int*
// , float* int*

// ptrflo = &intvar;
ptrflo = &flovar;

// , int* float*
// , float* float*

ptrvoid = &intvar;
ptrvoid = &flovar;

// , int* void*
// , int* void*


,
, :


,


int athroisma(int x, int y) ;
main()
{
int a, b;
.
sum = athroisma(a, b); //
.
.
}
int athroisma(int x, int y)
{
return x+y ;
83

}

void praxeis(int &s, int &d, int x, int y);
main()
{
int a, b, sum, diff;
.
praxeis(sum, diff, a, b);
.
.
}
void praxeis(int &s, int &d, int x, int y)
{
s = x+y;
d = x-y;
}
int &s s sum.
s sum . ,
s sum. d
diff.

void praxeis(int *s, int *d, int x, int y);
main()
{
int a, b, sum, diff;
.
praxeis(&sum, &diff, a, b);
.
.
}
void praxeis(int *s, int *d, int x, int y)
{
*s = x+y;
*d = x-y;
}
int:
int *s;
int *d;
,
sum diff. ,
, :
*s = x+y *d = x-y;

84

-

, .
, .

: new delete
,
.
.
, ,
.
C++ , ,
, .
new delete.

#include <iostream.h>
#include <string.h>
struct person
{
int id;
char name[20];
}
main()
{
struct person *ptr;
ptr = new struct person[1];
cout << :;
cin >> ptr->id;
cout << :;
cin >> ptr->name;
delete ptr;
}
new malloc()
C. new
, malloc()
(casting) .
new delete. ,
, ,

85

delete .
, ,
new .
,

, ,
.


, ,
. ,
,
new. new

.

class Person
{
private:
int id;
char name[20];
public:
void readData()
{
cout << :;
cin >> id;
cout << :;
cin >> name;
}
void printData()
{
cout << : << id << endl;
cout << : << name << endl;
}
};
main()
{
Person *p;
p = new Person;
p->readData();
p->printData();
}

86

, p
Person new
p .
-
->.


,
,
:
class Person
{
private:
int id;
char name[20];
public:
void readData()
{
cout << :;
cin >> id;
cout << :;
cin >> name;
}
void printData()
{
cout << : << id << endl;
cout << : << name << endl;
}
};
main()
{
Person *perspin[100];
int i = 0, j;
char choice;
do
{
perspin[i] = new Person;
perspin[i]->readData();
i++;
cout << More?;
cin >> choice;
}
while (choice == y);
for (j=0; j<i; j++)
perspin[j]->printData();
}

87

this
-

this. -

. , this

. ,
, ,
this. , , this .
, ,
this.
.
this:
#include <iostream.h>
#include <stdio.h>
class Employee
{
private:
float mikta;
int categ;
public:
Employee()
{
this->mikta = 0;
this->categ = 0;
}
Employee(float mikta0, int categ0)
{
this->mikta = mikta0;
this->categ = categ0;
}
void showAddress()
{
cout << "The address of the object is " << this << endl;
}
float calcTax()
{
float foros;
if (this->categ == 1)
foros = this->mikta * 0.15;
else
foros = this->mikta * 0.3;
return foros;

88

}
float calcSalary()
{
float misthos;
misthos = this->mikta - this->calcTax();
return misthos;
}
};
main()
{
Employee emp(1000,1);
emp.showAddress();
cout << "The salary is: " << emp.calcSalary() << endl;
}

89

90

9.
(virtual functions)
,
.


, .

#include <iostream.h>
class A
{
public:
virtual void display()
{
cout << " .";
}
};
class Par1 : public A
{
public:
void display()
{
cout << "1 .";
}
};
class Par2 : public A
{
public:
void display()
{
cout << "2 .";
}
};
main( )
{
Par1 p1;
Par2 p2;
A *ptr;
ptr = &p1;
ptr->display();
ptr = &p2;
ptr->display();
}

91

:
1
2
, A , Par1 Par2
, display()
.

,
virtual. ,

ptr->display();
, virtual,
display() . ,
virtual,
.



--
. ,
.
,
. , ,

.
, .
,
friend,
.

class B;
class A
{
private:
int x;
public:

92

A()
{
x=5;
}
friend int ff(A a1, B b1) ;
};
class B
{
private:
int y;
public:
B()
{
y=4;
}
friend int ff(A a1, B b1) ;
};
int ff(A a1, B b1)
{
return (a1.x+b1.y);
}
main()
{
A a;
B b;
cout << = << ff(a, b);
}
, ff()
.
friend. ,
, .

93

94

10.

,
. C++,
,
.

#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <conio.h>
class Person
{
protected:
int am;
char name[20];
public:
void setData(int am1, char name1[])
{
am = am1;
strcpy(name, name1);
}
};
void main()
{
Person p;
FILE *outfile;
int armit;
char onoma[20];
outfile = fopen("person.dat", wb);
cout << "Give am, 0 to stop:";
cin >> armit;
while (armit != 0)
{
cout << "Give name:";
cin >> onoma;
p.setData(armit, onoma);
fwrite(&p, sizeof(p), 1, outfile);
cout << "Give am, 0 to stop:";
cin >> armit;
}
fclose(outfile);
}

95

, 0.
setData()
fwrite().

#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <conio.h>
class Person
{
private:
int am;
char name[20];
public:
void printData()
{
cout << "A.M.: " << am << endl;
cout << "Name: " << name << endl;
}
};
void main()
{
Person p;
FILE *infile;
infile = fopen("person.dat", rb);
cout << "The contents of the file are:" << endl;
fread(&p, sizeof(p), 1, infile);
while (!feof(infile))
{
p.printData();
cout << endl;
fread (&p, sizeof(p), 1, outfile)
}
getch();
fclose(infile);
}
,
fread(), ,
printData().

96


, #include
( .h)
. (header files)
(include files)
. , iostream.h
cout <<.

. ,
myheader.h
.
myheader.h
#include <stdio.h>
#include <iostream.h>
class AAA
{
private:
int x;
int y;
public:
AAA()
{}
AAA(int x0, int y0)
{
x = x0;
y = y0;
}
void print()
{
cout << "x=" << x << endl;
cout << "y=" << y << endl;
}
};
, ,

:

97

#include "myheader.h"
#include <conio.h>
class BBB : public AAA
{
private:
int r;
public:
BBB(): AAA()
{}
BBB(int x1, int y1, int r1) : AAA(x1,y1)
{
r = r1;
}
void print()
{
AAA::print();
cout << "r=" << r << endl;
}
};
void main()
{
BBB ant(2,3,4);
ant.print();
getch();
}

#include "myheader.h"

,
. <> ,

INCLUDE.

98


Turbo C++
Robert Lafore
Waite Group
Object oriented software in C++
Michael Smith

99

Das könnte Ihnen auch gefallen