> n ; // ввод из потока">
C++
This presentation is the property of its rightful owner.
Sponsored Links
1 / 43

C++ PowerPoint PPT Presentation


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

C++. Объектно-ориентированное программирование. Первая программа C++. # include < iostream . h > int n = 5; // глобальная переменная void main () { int n ; // локальная переменная cout << " Enter n " ; // вывод в поток cin >> n ; // ввод из потока

Download Presentation

C++

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


C

C++

-


C

C++

#include <iostream.h>

int n = 5; //

void main()

{int n; //

cout << "Enter n" ; //

cin >> n; //

cout << "n = " << n; //

cout << "global n = " << ::n; //

}


C

#include <iostream.h>

#include <alloc.h>

class vector

{

private:

float *p; //

int n; //

public:

vector(int i = 3); // ( )

~vector(); // ( )

float item(int i); //

void assign(int i, float x); //

float num()

{ return n; }; // (inline)

float norm(); //

};


C

vector::vector(int i)

{ int j;

n=i;

p=new float[n];

for (j=0; j<n; j++) *(p+j)=0;

cout << "vector created " << n << "\n";

}

vector::~vector()

{ delete p;

cout << "vector destroyed\n";

}

float vector::item(int i)

{ if ((i>=0) && (i<n)) return *(p+i);

else {cout << "Error in vector::item"; return 0;}

}


C

void vector::assign(int i, float x)

{

if ((i>=0) && (i<n)) *(p+i)=x;

else cout << "Error in vector::assign";

}

float vector::norm()

{ int i;

float x=0;

for (i=0; i<n; i++) x+=p[i]*p[i];

return x;

}


C

main()

{ int i;

vector a(100);

vector b;

for (i=0; i<a.num(); i++) a.assign(i,i);

cout << a.norm() << "\n";

a.~vector();

}

vector created 100

vector created 3

328350

vector destroyed 100

vector destroyed 3


C

{

...

float operator()(int i); //

void operator=(vector &x); // ... //

}

float vector::operator()(int i)

{ if ((i>=0) && (i<n)) return *(p+i);

else {cout << "Error in vector::item"; return 0;}

}

void vector::operator=(vector &x)

{ if (x.num()==n)

for (int i=0; i<n; i++) p[i]=x(i);

else cout << "Error in operator =\n";

}

// main():

c=a;

cout << c(10) << "\n";


C

class matrix: public vector

{protected:

int M,N;

int lineaddres(int i, int j) { return i*N+j; };

public:

matrix(int m=2, int n=2): vector(m*n)

{ M=m; N=n; }

float item(int i,int j);

void assign(int i,int j, float x);

int m() { return M;};

int n() { return M;};

};

float matrix::item(int i, int j)

{ return vector::operator()(lineaddres(i,j)); }

void matrix::assign(int i, int j, float x)

{ vector::assign(lineaddres(i,j),x); }


C

11 12 13

21 22 23

31 32 33

main()

{ int i,j;

for (i=0; i<z.m(); i++)

{

for (j=0; j<z.n(); j++)

{

z.assign(i,j,(i+1)*10+j+1);

cout << z.item(i,j) << " ";

}

cout << "\n";

}

}


C

  • private

  • protected

  • private


Vector

Vector

class vector

{

public:

vector(int i = 3); //

vector(float x, float y, float z); //

vector & operator=(vector &x); // .

vector & operator*(float c); //

float operator*(vector &x); //

float & operator[](int i); //

}


Vector1

Vector

vector::vector(float x, float y, float z)

{ n=3;

p=new float[n];

vector::assign(0,x);

vector::assign(1,y);

vector::assign(2,z);

cout << "3d vector created " << n << "\n";

}

vector& vector::operator=(vector &x)

{

if (x.num()==n)

for (int i=0; i<n; i++) p[i]=x(i);

else cout << "Error in operator =\n";

return *this;

}


Vector2

Vector

vector & vector::operator*(float c) //

{

for (int i=0; i<n; i++) p[i]*=c;

return *this;

}

float vector::operator*(vector &x) //

{ float s=0.0;

for (int i=0; i<n; i++) s+=operator()(i)*x(i);

return s;

}

float & vector::operator[](int i)

{

if ((i>=0) && (i<n)) return p[i];

else {cout << "Error in vector::item"; return p[0];}

}


Vector3

Vector

main()

{

vector d(1,1,1);

b=d*2.0;

b[0]=3;

for (i=0; i<b.num(); i++) cout << b(i) << " ";

}

3 2 2


C

//

class point {

private:

int x,y; //

int c; //

public:

point(int u, int v, int w = WHITE); //

~point(); //

void show(); //

void hide(); //

void move(int u, int v); //

void change(int w); //

};


C

// point

point::point(int u, int v, int w) { x=u; y=v; c=w; }

point::~point() { hide(); }

void point::show() { putpixel(x,y,c); }

void point::hide() { putpixel(x,y,BLACK); }

void point::move(int u, int v) { hide(); x=u; y=v; show(); }

void point::change(int w) { hide(); c=w; show(); }


C

//

void main(void)

{

int gdriver = DETECT, gmode; //

initgraph(&gdriver, &gmode, "C:/BC5/BGI");

point p(0,240);

for (int i=0; i<640; i++) { p.move(i,240); delay(10); }

p.~point();

getch();

closegraph();

}


C

//

class point {

protected:

int x,y; //

int c; //

public:

point(int u, int v, int w = WHITE); //

virtual ~point(); //

virtual void show(); //

virtual void hide(); //

void move(int u, int v); //

void change(int w); //

int getx() { return x; };

int gety() { return y; };

int getc() { return c; };

};


C

// point

point::point(int u, int v, int w) { x=u; y=v; c=w; }

point::~point() { hide(); }

void point::show() { putpixel(x,y,c); }

void point::hide() { putpixel(x,y,BLACK); }

void point::move(int u, int v) { hide(); x=u; y=v; show(); }

void point::change(int w) { hide(); c=w; show(); }


C

//

class circ: public point

{

protected:

int r; //

public:

circ(int u, int v, int w = WHITE, int s = 10);

void show();

void hide();

void size(int s);

int getr() { return r; }

};

//

circ::circ(int u, int v, int w, int s) : point(u,v,w) { r=s; }

void circ::show() { setcolor(c); circle(x,y,r); }

void circ::hide() { setcolor(BLACK); circle(x,y,r); }

void circ::size(int s) { hide(); r=s; show(); }


C

//

class circ: public point

{

protected:

int r; //

public:

circ(int u, int v, int w = WHITE, int s = 10);

void show();

void hide();

void size(int s);

int getr() { return r; }

};

//

circ::circ(int u, int v, int w, int s) : point(u,v,w) { r=s; }

void circ::show() { setcolor(c); circle(x,y,r); }

void circ::hide() { setcolor(BLACK); circle(x,y,r); }

void circ::size(int s) { hide(); r=s; show(); }


C

//

class square: public circ

{public:

square(int u, int v, int w, int s) : circ(u,v,w,s) {};

void show();

void hide();

};

//

void square::show() { setcolor(c); rectangle(x-r,y-r,x+r,y+r); }

void square::hide() { setcolor(BLACK); rectangle(x-r,y-r,x+r,y+r); }


C

class obj { }


C

void main(void)

{

point *figures[3]; //

//

figures[0]=new point(320,240);

figures[1]=new square(320,240,20,WHITE);

figures[2]=new circ(320,240,30);

//

for (int i=0; i<3; i++) figures[i]->show();

}


C

,

class line; //

class box { int color;

public : friend int same_color(line l, box b);

}

class line { int color;

public : friend int same_color(line l, box b);

}

//

int same_color(line l, box b)

{ return (l.color==b.color) }


C

-

#include <iostream.h>

// /

class vector

{

float x,y,z;

public:

vector (float a, float b, float c) {x=a; y=b; z=c; }

friend ostream & operator << (ostream & stream, vector obj);

friend istream & operator >> (istream & stream, vector &obj);

};


C

-

// vector inserter

ostream & operator << (ostream & stream, vector obj)

{ stream << "(" << obj.x << ",";

stream << obj.y << ",";

stream << obj.y << ")\n";

return stream;

}

// vector extractor

istream & operator >> (istream & stream, vector & obj)

{ stream >> obj.x >> obj.y >> obj.z;

return stream;

}

void main()

{ vector a(1.0,2.0,3.0);

cout << a;

cin >> a;

cout << a;

}


C

,

class color

{protected:

int c; //

public:

color (int z) { c=z; } //

int getcolor() { return c; }

};

classfigure

{protected:

int x,y; //

int r; //

public:

figure (int a, int b, int z) { x=a; y=b; r=z; };

~figure() { hide(); };

int getsize() { return r; };

virtual void show() = 0; //

virtual void hide() = 0; //

void move(int u, int v);

};

void figure::move(int u, int v){ hide(); x=u; y=v; show();}


C

,

//

class circus : public color, public figure

{public:

circus(int x, int y, int r, int c):

color(c), figure (x,y,r) {}; //

void show();

void hide();

};

void circus::show() { setcolor(c); circle(x,y,r); }

void circus::hide() { setcolor(BLACK); circle(x,y,r); }

void main(void)

{ int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, "C:/BC5/BGI");

// figure f(320,240,10); // !

circus cr(320,240,10,YELLOW);

cr.show();

getch(); closegraph();

}


C

class st {

static int c;

public:

static int incr(void) { return c++; }

st(void) {cin << incr(); }

~st(void) { c--; }

}

. . . .

st::c=0;

st::incr();


C

template <class T> //

T sqr ( T x )

{

return x*x ;

}

main()

{ int i = 3;

float x = 3.1;

cout << sqr(i) << \n;

cout << sqr(x) << \n;

}


C

template <class T, int size> //

class vec

{ T *q;

int n;

public:

vec (void);

};

template <class T, int size>

vec<T,size>::vec(void)

{ q = new T[size];

};

void main()

{ vec <double, 200> d;

vec <int, 100> k; }


C

++

try () - ;

catch () - , "" ;

  • Finally ()

    throw () - , "" ("") .

    Try Try...Catch...Finally , . - , Visual Basic Catch Try...Catch...Finally, , . Catch. Catch , Catch Try...Catch...Finally, , . , Catch . , .

    Finally , , , , Catch. Finally , .


C

void func()

{

try { throw 1;

}

catch(int a)

{ printf("Caught exception number: %d\n",a);

return;

}

printf("No exception detected!\n");

return;

}

  • , : Caught exception number: 1

  • throw 1; No exception detected!


C

catch "" , . .. - :

catch(dumbclass) { }

,

catch(dumbclass&) { }

"" :

catch(...) { }


C

try { throw 1;

// throw 'a';

}

catch (long b)

{ cout << " long: " << b << endl; }

catch (char b)

{ cout << " char: " << b << endl; }


C

try {

Main.Setup();

Main.Loop();

Main.Close();

}

catch (Exception &e)

{ // , .

log("Exception thrown: %s", e.String());

// .

}


Microsoft

Microsoft

#include <stdio.h>

#include <excpt.h>

main () //

{int i=9;

int c;

__try

{

i=i+1;

i=i/(i-i);

}

__except(EXCEPTION_EXECUTE_HANDLER)

{ c=GetExceptionCode();

printf("Error - %d\n",c);

}

printf("%d\n",i);

}


Microsoft1

Microsoft

set_terminate( term_func );


C

#define MAX 100

#define NAME Borland C++

#define MIN(a,b) ( (a)<(b)) ? (a) : (b)

printf( x y %d, MIN(x,y));

#include <_>

#include _

#include _


C

void main ()

{

#if MAX>99

printf(MAX>99\n);

#else

printf(MAX<99\n);

#endif

return 0;

}


C

#if defined(MAX) && defined(MIN)

#ifdef MAX

#ifndef MAX

#if defined(MAX)

#elif defined (MIN)

#else

#endif


C

#pragma warn +xxx, -xxx, and .xxx

#error Error N 3


  • Login