..
This presentation is the property of its rightful owner.
Sponsored Links
1 / 32

Д.з. на 3 марта PowerPoint PPT Presentation


  • 85 Views
  • Uploaded on
  • Presentation posted in: General

Д.з. на 3 марта. Задача 1: треугольный массив. int* a[10]; // Создаем.. for (int i = 0; i<10; i++) { a[i] = new int[i+1]; } // Заполняем for (int i = 0; i<10; i++) { for (int j = 0; j<=i; j++) { a[i][j] = (i == j); } }. // Печатаем for (int i = 0; i<10; i++) {

Download Presentation

Д.з. на 3 марта

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


3 4426184

.. 3

++ - 4


3 4426184

1:

int* a[10];

// ..

for (int i = 0; i<10; i++) {

a[i] = new int[i+1];

}

//

for (int i = 0; i<10; i++) {

for (int j = 0; j<=i; j++) {

a[i][j] = (i == j);

}

}

//

for (int i = 0; i<10; i++) {

for (int j = 0; j<=i; j++) {

cout << a[i][j] << " ";

}

cout << "\n";

}

4.03.2008

++ - 4

2


3 4426184

3:

  • ,

  • , >1

++ - 4


3 4426184

3:

// , : > 1,

// (>=1) , (>=1)

int numEven(int* a, int size)

{

int beg = 0, end = size - 1;

while (end - beg > 1) {

int mid = (beg + end) / 2;

if (a[mid]%2 == 0)

beg = mid;

else

end = mid;

}

return end;// end - beg == 1

}

//

int a[10];

cout << numEven(a, 10);

10.03.2009

++ - 4

4


3 4426184

.. 10

10.03.2009

++ - 4

5


1 order

1: order

// :

//

void order(int& x, int& y)

{

if (x > y) {

int tmp = x;

x = y;

y = tmp;

}

}

void order(int& x, int& y, int& z)

{

order(x, y);

order(x, z);

order(y, z);

}

// :

int a = 3, b = 1, c = 2;

order(a, b, c);

cout << a << b << c;

10.03.2009

++ - 5

6


3 4426184

2:

class stack {

int stk[100];int size;

public:

stack() : size(0)

{}void push(int i){

stk[size++] = i;}

int pop(){

return stk[--size];}

};

stack s; //

s.push(5);

s.push(10);

s.push(2);

cout << s.pop(); // 2

int i = s.pop() s .pop();

// // !

10.03.2009

++ - 5

7


3 4426184

3:

class dynstack {

int size;//

int maxsize;// .

int* p; //

public:

dynstack() : size(0), maxsize(10)

{

p = new int[maxsize];

}

~dynstack()

{

delete [] p;// ..

}

void push(int val)

{

if (size == maxsize) {// ?

int* newp = new int[maxsize*2];

//

for (int i=0; i<size; i++) {

newp[i] = p[i];

} maxsize *= 2;

delete [] p;

p = newp;//

}//

p[size++] = val;

}

int pop()

{

return p[--size];

}

};

10.03.2009

++ - 4

8

8


3 4426184

()

10.03.2009

++ - 4

9


3 4426184

class abc;

class klm {

abc* p;

};

class abc {

klm* q;

};

++ - 4


3 4426184

:

, .

class abc { };

:abc x;abc a[10];

:abc (){}

- ..

class abc { };abc x;//

  • ,

++ - 4


3 4426184

++ - 4


3 4426184

sec = 0;

inc();

}

}

void time_with_sec::print()

{

time::print();

cout << "." << sec;

}

// time_with_sec

time_with_sec ts(12, 30, 45);

ts.inc();

ts.inc_sec();

ts.print();

class time_with_sec : public time {

int sec;

public:

time_with_sec(int h, int m, int s);

void inc_sec();

void print();

};

time_with_sec::time_with_sec(int h,int m,int s):

time(h, m),

sec(s)

{}

void time_with_sec::inc_sec()

{

sec++;

if ( sec == 60 ) {

++ - 4


3 4426184

  • public ( )

  • : /

  • ,

  • (sec, inc_sec)

  • time::print() ,

  • , ( )

++ - 4


Protected

protected

.

, .. time :class time {protected: int hour, min;// time

//

++ - 4


3 4426184

(!!!)

  • :

    , , .

    class time {virtual void print();

++ - 4


3 4426184

. .

time* p;if ( - ) {p = new time(8, 50);} else { p = new time_with_sec (11, 30, 15);}

// p ??

// - ..

p->print();// print// ?

  • , print: time::print() time_with_sect::print() p

  • :

    (dynamic binding) (late binding)

++ - 4


3 4426184

.

void my_print(time& p){ cout << " :"; p.print();}

time t1(8, 50);time_with_sec t2(11, 30, 15);

my_print(t1);my_print(t2);

print !

  • , !

    :

++ - 4


Virtual

: virtual...

  • , :class abc {virtual void f();

    • : f

++ - 4


3 4426184

?

, ( , vtable).

,

10.03.2009

++ - 4

20


3 4426184

,

++ - 4


3 4426184

:

: , , , ..

class shape {

protected:

int x, y;

public:virtual void draw()

= 0;

shape(int x_, y_) :x(x_), y(y_){} (, ..) };

++ - 4


3 4426184

  • =0;- (pure virtual). :

    • .

++ - 4


3 4426184

  • :

    • (abstract base class)

    • shape s;// : //

    • , , .

++ - 4


Virtual 0

virtual... = 0;

class abc {virtual void f() = 0;

  • : f .

++ - 4


3 4426184

10.03.2009

++ - 4

26


3 4426184

?

a - 10 a int

10

int

a

a[10]

*(a[10])

int *(a[10]);// int *a[10];

10.03.2009

++ - 4

27


3 4426184

?

int (*a)[10];

*a

(*a)[10]

int (*a)[10];

a

10

int

10.03.2009

++ - 4

28


Typedef

typedef

:

typedef ;

typedef unsigned long ulong;

typedef int* my_array[10];

my_array a; // , int* a[10];

- typedef

void f(unsigned long i)

{

}

void f(ulong j) // -

{//

//

}

10.03.2009

++ - 4

29


Typedef1

typedef ?

typedef unsigned long ulong;

typedef int MY_INTEGER;

(encapsulation)

:

typedef int* pint;

pint a[10];

p = new pint[20];

10.03.2009

++ - 4

30


3 4426184

17

++ - 4


3 4426184

17

  • a. ( , ). draw.

    ( draw ).. - , .

  • shape rhomb square. area () perim (). ( draw ).

  • ( ) . . n n, n-1, n-2, 3, 2, 1.. .(. ).

++ - 4


  • Login