Вы находитесь на странице: 1из 48

.. . .

++

-2005

++



2005
-
++. ,
. , .
,
++ .

: ..
. .

: . . .. !!!

U.T.M., 2005 !!!!


1

:
:
;
;
, ;
.

, .
, , ;
, ,
, .
, ,
, .
,
,
.

struct Book{
//
char *author;
char *title;
int year;
int pages;
};
struct typedef. ,
, ++. ,
. ,
. , .

:

Book b1, b2, bs[10], *bptr;

,
. ,
-, . -.

b1.pages = 153;
bs[i].pages = 24;

, .
->.

bptr = new Book; //


bptr->pages = 176;

,
:

Circle1.Center.x = 20;

3
Circle1.Center.y = 10;

struct Date{
int day, month, year;
};
struct Student{
char *name;
Date birthDay;
float media;
};


.
.
( ).

void setValues(Student* sptr, char* newN,float newM,Date d)


{
sptr->name = new char[strlen(newN)+1];
strcpy(sptr->name,newN);
sptr->media = newM;
sptr->birthDay = d;
}

, .
, ,
.
( ,
).

void setValues(Student* sptr, char* newN,float newM,Date d)


{
delete[] sptr->name;
sptr->name = new char[strlen(newN)+1];
...
}

, ,
.
. .
, ,
( ) . ,
, , -
, , ,
.
.
- NULL;

Book b;
b.author = NULL;
setAuthor(&b, Arthur Conan Doyle);

, ,
. .

void freeMem(Book* bp){


4
delete[] bp->author;
bp->author = NULL;
... //
}

NULL
.

1. ?
2. ?
3. ?
4. ?
5. ?
6. , ?
7. ++?
8. ?
9. ?
10. ?
11. ?

5

1
) , .
/ , , ,
, , (, , ..). -
. ,
.
b) () - , int
. : , , /
, , . ,
main, .
2
) - , ,
().
. new.
+ .
.
b) () - , float
. : , , /
, , . ,
main, .

3
) - , -
, , . ,
, . new.
. main
+ .
b) () - , double
. : , , /
, , .
, main, .
4
) () - , ,
.
, , .
new. main-,
.
b) () - , int
. : , , /
, , .
, main, .

6
5
) () - , , ,
. ,
. new. .
main-, .
b) () - , long
. : , , /
, , .
, main, .
6
) () - , ,
, . , ,
. new. .
main-, +
.
b) () - , float
. : , , /
, , . ,
main, .

7
) () - ,
, , .
, , .
new. . main-,
.
b) () - , byte
. : , , /
, ,
. , main, .
8
) () - , , ,
, . ,
, . new.
. main-, .
b) () - , short
. : , , /
, , .
, main, .

7
9
) () - , , ,
. , , .
new. . main-,
+ .
b) () - , long
. : , , /
, , .
, main, .
lass Vector {
short * Elem;
int CislElem;
public:
Vector();
Vector(short *,int);
~Vector();
void SetCislElem(int);

10
) () (software),
, , . ,
, . new.
. main-,
.
b) () - , double
. : , , /
, , .
, main, .

11
) () - , ,
, . ,
, . new.
. main-, .
b) () - , int
. : , , /
, , .
, main, .

12
) () - , ,
. , , .
new. . main-,
.
b) () - , float
. : , , /
8
, , .
, main, .

9
2

:
:
;
;
;

( )
, ,
. ,
, :

class Book{
char *author;
int year;
int pages;
public:
void Init(char*, int, int);
void Destroy();
};
void Book::Init(char* a, int y, int p){
author = new char[strlen(a)+1];
strcpy(author,a);
year=y;
pages=p;
}
void Book::Destroy(){
delete[] author;
}

, ,
, . ,
,
.
. Destroy, .


, ++
- .
.
,
. ,
, void.
.
,
. ,
~. .
.

, , .

10
-,
delete, .
:

class Book{
char *author;
int year;
int pages;
public:
Book(char*, int, int);
~Book();
};
Book::Book(char* a, int y, int p){
author = new char[strlen(a)+1];
strcpy(author,a);
year=y;
pages=p;
}
Book::~Book(){
delete[] author;
}
void main(){
Book b(Stroustrup,2000,890);
// Book b1; // //
//

Book* ptr = new Book(Lippman,1996, 1200);

//
delete ptr;
// //
}
// b.


: , ,
.
.

, ,
.
()
.
, .
, .
:

class Book{
char *author;
int year;
int pages;
public:
Book(); //
//
Book(const Book&); //
Book(const char*); //
//
Book(char*, int, int); //
...
11
};
...
void main(){
Book b(Stroustrup,1997,890); //
Book b1 = b, b11(b); //
Book b2 = Stroustrup, b21(Bjarne);
//
Book b3; //
}


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

. -
.
, / .

,
, ,
.
:

Book::Book(const Book& b){


author = new char[strlen(b.author)+1];
strcpy(author, b.author);
year = b.year;
pages = b.pages;
}

.
,
,
.

Book::Book(const Book& b): year(b.year),pages(b.pages),


author(new char[strlen(b.author)+1])
{
strcpy(author, b.author);
}
// strcpy ""
// .

, ,
,
.

12
:

1. ?
2.
?
3. ?
4. ?
5. ? ? ?
6. ?
7. ?
8. ?
9. ?
10. ? ?
11. ?
12. ?

13

1
) Date - : (1-28..31), (1-12), ( ).
, - , , -
, . : : "19 2003
" "19.04.2003".
.
b) Matrix-. int,
- . ,

. : (i,j).
( ),
. . .
, ,
.
2
) Time - : (0-23), (0-59), (0-59).
, - , , ,
: : "16 18 3 " "4 p.m. 18
3 ".
.
b) Matrix-. double,
- . ,

. : (i,j).
( ),
. . .
, ,
.
3
) Stack - .
. : ,
, . Push Pop
. IsEmpty IsFull
.
b) Matrix-. float,
- . ,

. : (i,j).
( ),
. . .
, ,
.

14
4
) File ,
(doc Word, psd Photoshop, etc), , ,
. , -
. ,
.
b) Matrix-. long,
- . ,

. : (i,j).
( ),
. . .
, ,
.
5
) Document , , ,
, ,
. : -
. ,
.
b) Matrix-. byte,
- . ,

. : (i,j).
( ),
. . .
, ,
.
6
) Image , : ,
, , , (%).
, - .
, , .
b) Matrix-. Complex,
- . ,

. :
(i,j). ( ),
. . .
, ,
.
7
) Queue - .
. : , ,
. add get
. isEmpty isFull .

15
b) Matrix-. int,
- . ,

. : (i,j).
( ),
. . .
, ,
.
8
) String , . :
, char.
, , , .
b) Matrix-. double,
- . ,

. : (i,j).
( ),
. . .
, ,
.
9
) , , ,
( ).
. : , ,
. ,
.
b) Matrix-. float,
- . ,

. : (i,j).
( ),
. . .
, ,
.
10
) Soft ,
(doc Word, psd Photoshop, etc), , ,
. : , ,
. ,
.
b) Matrix-. long,
- . ,

. : (i,j).
( ),
. . .
, ,
.
16
11
) Group , , ,
, .
. .
.
,
5.
b) Matrix-. byte,
- . ,

. : (i,j).
( ),
. . .
, ,
.

12
) Set , .
: .
, , ,
.
b) Matrix-. Complex,
- . ,

. :
(i,j). ( ),
. . .
, ,
.

17
3

:
:
;
;
;
;

, ,
, .
, , ( ),
, ,
.
, ,
. ,
? . ,
. . ,
, , Assign (
Java). , .
,
, , :

class Book{
char* name;
public:
Book(char n){
name = new char[strlen(n)+1];
strcpy(name,n);
}
~Book(){
delete[] name;
}
void print(){
cout<<name;
}
};
void main(){
Book b(Stroustrup), b2(Lippman);
b2 = b;
b.print();
b2.print();
}

, . ,
, ( Lippman) ,
(
). ,
, .
, ,
, , ,
18
, . ,
,
.
, , , ,
.


?
.
operator, . ,
.
Complex operator+(const Complex& r){
return Complex(re+r.re,im+r.im);
}

, ,
:
Complex c1,c2,c3;
c3=c1+c2; //
c3=c1.operator+(c2); //


: 1.
. & - , ++ -
, ! . : + - , * -
.
, (
). , ++ i++, ++i.
? :
:

Complex& operator++(); //
Complex operator++(int); //


: ( )
. ,
. :
? ?
? , ,
<< . ,
. ,
this. - , .
. <<
ostream .
- , ,
.
.

1
, ,
, . ?:.

19

. ,

. :

., .*, ?:, ::, sizeof, #, ##.

, :

->, [], (), new new[], delete delete[] .

, "=", "[]", "()"


"->" .
->,
, .
.2

:
- , ;
- ;
- void;
operator();
- ;
- operator() ,
.

1. ?
2. ?
3. ?
4. ?
5. ?
6. ?
7. ?
8. "()"?
9. "->"?
10. (
)?
11. "<<"?
12. ?

2
,
.
20

1
) . "++" "+", , "- -" "-"
. ,
, .
b) Set , .
: "+" , "*" , "-" ,
, "+=" , "=="
, . . "<<" ">>".
.
2
) 2-D . "+" "-" ,
.
, ,
.
b) Stack , . "+"
, "=" , "()" n
- . "==", "!=", "<", ">",
. ,
. / .
,
.
3
) 3-D . "+", "-", "=" ,
.
, , .
b) Queue - , . "+"
, "=" . "==",
"!=", "<", ">", .
, . "<<"
">>" / , / / .
4
) Date , : , , . "+" "-",
, "++" "--" ( )
. "+"
int. (x=y+5;). .
b) List - . "+" , "="
. "==", "!=", "<", ">",
. "<<" ">>" / ,
/ / . ,
.
ListItem ,
List.

21
5
) . "++" "+", ,
"- -" "-" . ,
, double.
b) Vector , . "+"
, "-" , "=" ,
. "==", "!=", "<", ">",
. ,
. "<<" ">>" / .
.
6
) . "+" "*",
, "-" "/" .
( ).
, ,
long.
b) Matrix , . "+"
, "-" , "=" , .
"==", "!=", "<", ">", .
, .
"[]" , [][] .
"<<" ">>" / .
7
) Bool . "+" ,
"*" "^" , , "==" "!="
. ,
, int. (
, , ).
b) String , . "+"
, "=" "+=" , .
"==", "!=", "<", ">", . String,
char*. "[]" .
/ .
8
b) Stack , . "+"
, "=" , "()" n
- . - "==", "!=", "<", ">",
. ,
. / .
) Complex .
, .
, / .
,
double.

22
9
) Time , : , , .
"+" "-", , "++" "--" ( )
. ,
, int ( ).
b) Queue - , . "+"
, "=" . "==",
"!=", "<", ">", .
, . "<<"
">>" / , / / .
10
) Bool . "+" ,
"*" "^" , ,
"==" "!=" . ,
, int. (
, , .)
b) Set , .
: "+" , "*" , "-" ,
, "+=" , "==" ,
. . "<<" ">>".
.
11
) Date , : , , . "+" "-",
, "++" "--" ( )
. "+"
int. (x=y+5;). .
b) String , . "+"
, "=" "+=" , .
"==", "!=", "<", ">", . String,
char*. "[]" .
"<<" ">>" / .
12
) 2-D . "+" "-" ,
.
, ,
.
b) List - . "+" , "-"
( ) . "==", "!=", "<",
">", . "<<" ">>" / ,
/ / . ,
.
ListItem ,
List.

23
:
:
, ;
;
;
;
;
;
.

-
. :
.
,
, .

. ( ,
).

:
- ;
- ;
- ;
- ;

:
.
, , , ,
.


,
. :

class Animal{
int NofLegs;
public:
void Say(){ cout<<!!!; }
};
class Dog: public Animal{ //
...
};
void main(){
Dog d;
d.Say();
}

, ,
. ,
, ,
, . ( ,
24
, , .)
.


, ,
:

void main(){
Animal *ptr = new Dog;
}


, , .


, ++ ,
. ,
.
- ;
- ;
- ;
,
. , ,
( )
. .
.
, , ,
, .

, ,
, .
"is a". ,
.
"has a".
.


,
.
, .
, .

class Car{
Engine e;
};

,
, , ,
.
. , ,
25
. .
,
3.
.

class Engine{
int power;
public:
Engine(int p){power=p;}
};
class Transport{
...
public:
Transport(char*);
};
class Car:public Transport{ //
Engine e; //
public:
Car():Transport(automobile),e(10){}
};

,
,
. ,
.


,
: .
, ,
: : (Dog is an Animal),
;
: (Car has a door)
.
, . ,
, .
, . .
:
- ?
- - ?
- ?
- ?
, 4.

1. ?
2. ?
3
, , ,
, .
4

, .
26
3. ?
4. ?
5. ?
6. , ?
7. ?

27

1
) . ,
, .
b) , . .
, -.
, .
, .
2
) , , , .
, , .
new. ,
. -,
. , .
b) , . .
, ( ),
( ). ,
. (
- ). , .

3
) , , (,
. ). .
: . , ,
.
b) , . .
, , ( ) .
( ). ,
.
4
) ,
. ,
,
.
b) , . .
- , . ,
, .
5
) , ( ), .
, . -
, . .

28
b) , . .
, .
. .
, .
6
) , ( ), , .
, . -
, .
, , .
.
b) :
( ),
, , . new.
, .
, , ,
.
7
) , .
, ,
.
b) , , , .
, , .
new. , .
( ). ,
, , , .
.
8
) : ..,
.. .. . / ,
,
.
b) , -, , .
, . :
. ,
, , , .
new.
9
) - , . :
, , , .
new. ,
, .
b) Processor - , ,
, .
Computer - , .
new. ,
.

29
10
) .
, ,
.
b) ,
. , .
. ,
.
11
) ,
.
.
, ,
.
b) .
.
.
12
) , .
,
. new.
, ,
.
b) , ,
8 8. .

(2). .

30
Window

Button Titled
Window Window

Button
Titled
Window
5

:
:
;
;
;
;

, .
, , ,
,
.
:
- : , , , ,
, 5.
, . ,
, ++. :
, ,
, .

. 1 .

, , .

. , ,
.
:
- .
, , , ,
.

. - ++.
5

1998 .
31
- ,
;
.
- , .
- .
- iostream : istream ostream.
, , - .

class Student{
public:
int mark;
...
};
class Worker{
public:
int salary;
};
class Practicant: public Student, public Worker{
};
void PutMark(Student& s, int mark){
s.mark = mark;
}
void PutSalary(Worker& w, int salary){
w. salary = salary;
}

void main(){
Practicant p;
PutMark(p,5);
PutSalary(p,200);
}

, ,
, , ,
! , ,
.

, "". , ,
,
. -
. ,
.

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

class C: public A, public B{

};
32
void main(){
C c;
c.x = 10;
}

,
, .
, .
.
.
, 6. ,
, :

c.A::x = 10;
c.B::x = 5;

A B ,
, 1, -
. ,
. , . ,
,
,
. , ,
. :
.

class A{
public:
int x;
A(int x){this->x=x;}
};
class B: virtual public A{
public:
B(int x):A(x){}
};
class C: virtual public A{
public:
C(int x):A(x){}
};
class D: public B, public C{
public:
D(int x):A(x),B(x),C(x){}
}

B C A.
. ,
, .
,
, .

6
,
.
33
:

1. ?
2. ?
3. ?
4. ?
5. ?
6. ?
7. ?

,
.

1
a) : , - .
b) : - , - .
2
a) : , .
b) : - , .
3
a) : , .
b) : - ,
.
4
a) : , .
b) : - ,
.

5
a) : , .
b) : - , .
6
a) : , -.
b) : - , -.
7
a) : , -
Boing 747
b) : - ,
- Boing 747

34
8
a) : , .
b) : - , .
9
a) : , .
b) : - , .
10
a) : , .
b) : - ,
.
11
a) : , .
b) : - , .
12
a) : , - .
b) : - , - .

35
6
: .
:
;
;
;
ad-hoc;
;
.


"
" (poly , morphos ). morphos
(Morphus), ,
.
,
. ,
, (,
). ,
, ( ).
(,
), , , .
, .
++ :
" ";
;
;
.
-
.
.
: ,
. . ,
,
. ( ). C++
.

-
.
.
,
.
. ,
.
( )
, ,
.

.

36


, .
,
, . ,
.


, ,
. ,
, virtual.
.
.
:

#include<iostream.h>
class Animal{
public:
void Say(){ cout<<"!!!\n";}
};
class Dog: public Animal{
public:
void Say(){ cout<<"GAV\n";}
};
class Cat: public Animal{
public:
void Say(){ cout<<"MIAU\n";}
};
void FunSay(Animal a){
a.Say();
}
void main(){
Animal a;
Dog d;
Cat c;
FunSay(a);
FunSay(d);
FunSay(c);
}

,
,
, , Say . ,
: Say
, .

ad-hoc
C++
.
,
, (, )
. , :
virtual void display (char *, int);

:
virtual void display (char *, short);
37
,
, . , ,
, . ,
,
.
, ,
. ,
,
.
ad-hoc.


( , C++
) .
. , ,
. .

class Shape {
public:
...
virtual void draw() = 0;
...
};

,
, .
.
, .

1. ?
2. . ?
3. ?
4. ? .
5. ?
6. ?
7. ad-hoc?
8. ?
9. ?

38

1
Worker .
StateWorker, HourlyWorker CommissionWorker,
. main ,
.
2
Figure - .
Square, Circle, Triangle, Trapeze .
main ,
. : S=(a+b)h/2.
3
- .
: .
double. - , (double) -
( ) ( ).
, .
aj=a0+jd, j=0,1,2,
: sn=(n+1)(a0+an)/2
: aj=a0rj, j=0,1,2,
: sn=(a0-anr)/(1-r)
4
Mammal - .
- Animal Human.
Dog - Cow , .
5
Lines f(x).
StraightLine, Ellipse, hyperbola .
main ,
. : y=ax+b , : x2/a2+y2/b2=1, : x2/a2-y2/b2=1
6
Figure - .
Rectangle, Circle, Triangle, Rhomb
. main ,
.
7
Container .
Stack Queue, .
main ,
.

39
8
Figure - .
, ,
.
: S=6xy.
: S=4 r2.
: S=a2 3
9
Number - .
Complex, Vector 10 , Matrix 2 2,
. main ,
.
10
- .
.., .. ..
. main ,
.
11
- .
,
.

12
Figure - .
, , ,
. main ,
.
- V=xyz (x,y,z ).
: V=xyh (x,y, - , h - ).
: V= a3 2/12.
: V=4 r3/3.

40
7

:
:
;
;
;
;

++, ,
.
.
, ,
.
. ,
, , , , ,
.
, , .,
.
. ,
,
, .
, , , , ,
. ,
.
,
. ,
, , ,
, ,
, .

template:

template <class T>


T& searchmax(T* ptr, int size);

Template <class T>


class Stack{
T mas[10];
public:
...
};

,
template, class7 ,
. ,
T, type. ,
template.

7
typename class.
41

.

void main(){
int masi[10];
float masf[20];
cout<<searchmax(masi,10);
cout<<searchmax(masf,20);
}

, . ,
. .
:

void main(){
Stack<int> sti;
Stack<float> stf;
}

, , ,
, .

.
:

template <class T>


T& max(T& a, T& b){
if(a>b)return a;
return b;
}

, int, float .
. , ,
. >.
:
String, >,
. , ,
, .
.
, .

. , .
, ,
.
. .
:

template <class T>


class Stack{
public:
void push(T& t);
void sort();
friend ostream& operator<<(ostream& os, Stack<T>& s);
};
template <class T>
void Stack<T>::sort(){

42
...
//
}
void Stack<char*>::sort(){
... //
}
template <class T>
ostream& operator<<(ostream& os, Stack<T>& s){
return os; //
}
void main(){
Stack<int> si;
si.push(5);
Stack<char*> sc;
sc.push("Hello");
si.sort(); //
sc.sort(); //
cout<<si<<sc;
//
}

, , , ,
.

class One{
};
template <class T>
class Two: public One{
};
template <class T>
class Three: public Two<T>{
};
class Four: public Three<int>{
};
template <class T>
class Five: public T{
};

,
. T
.

. , ,
. ,
,
, , ,
..

1. ?
2. ?
3. ?
4. ?
5. ?
6. ?
7. ?
8. ?

43

1
) , :
, . : 1 2 3 4 5 6 - 4 5 6 1 2 3.
. ,
.
b) Stack. ,
, push, pop, empty, full /.
new.
2
) , . : 1 2 3 4 5 6
- 2 1 4 3 6 5. .
, .
b) Vector. ,
, getLength, [], +, - /.
new.
3
) ,
. : - 0 2 3 4 3 6, - 3, - 2.
.
b) List ListItem.
, , add, in, remove, getLength, [] /.

4
) .
. : - 0 2 3 4 3 6, - 2, - 1.
.
.
b) Queue - . ,
, add, in, get, getLength, [] /.
5
) . :
- 0 2 3 4 3 6, - 4. .
b) Set - . ,
, add, in, remove, getLength, + - , * - ,
- - /.
6
) .
.
b) Map ,
. .
, , add, removeByKey, getLength, getByKey, getByValue,
[] /.

44
7
) , :
, .
: - 0 2 3 4 3 6, 2 2 7 1 9 3.
b) Matrix . ,
, getRows, getCols, [], +, -, * /.
8
) , .
: 1 2 3 4 5 6 - 6 5 4 3 2 1. .
b) Tree .
, , add, in, /.
9
) , ,
. : - 0 2 3 4 3 6, - 5, - 1.
.
b) Stack. ,
, push, pop, empty /.
new, n .
10
) .
.
b) MultiMap - ,
. ,
. , , add, removeByKey,
getLength, getByKey, getByValue, [] /.
11
) , ,
. : - 0 2 6 4 3 3, - 3, - 2.
.
b) PriorityQueue .

, - .
, , add, in, get, getLength, []
/.
12
) .
: - 0 2 3 4 3 6, - 2. .
b) Matrix . ,
, getRows, getCols, [], +=, -=, *= /.

45
++

: ..
. !!!

Bun de tipar Formatul hrtiei 61x84 1/16


Hrtie ofset. Tipar ofset Tirajul 100 ex.
Coli de tipar Comanda nr.

Universitatea Tehnic a Moldovei


MD-2004, Chiinu, bd. tefan cel Mare, 168.
Secia Redactarea i Editare a U.T.M.
MD-2068, Chiinu, str. Studenilor, 11

46

Вам также может понравиться