.


:




:

































 

 

 

 


, , , .




ᒺ- , 7.091401 , , .

, ᒺ- . ᒺ- ++ . ' ++, ' , , , . ++ , , , .

. , , . .

:

1 - 4 Turbo C++ 3.0

5 - 8 Borland C++ 5.02.


1.

' ++

. ++.

.

, ', . .

.

++ ++. '. ᒺ- : , , .

++ , .

. :

_ '_ {__};

_ class, struct, union;

'_ ;

__ .

, '.

', . '.

struct date //

{int month,day,year; // : , ,

void set(int,int,int); // -

void get(int*,int*,int*); // -

void next(); // -

void print(); // -

};

struct class complex //

{double re,im;

double real(){return(re);}

double imag(){return(im);}

void set(double x,double y){re = x; im = y;}

void print(){cout<<re = <<re; cout<<im = <<im;}

};

' ( ) :

'_ '_';

date today,my_birthday;

date *point = &today; // ' date

date clim[30]; // '

date &name = my_birthday; // '

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

'_'. '_

'_'. '_

:

complex x1,x2;

x1.re = 1.24;

x1.im = 2.3;

x2.set(5.1,1.7);

x1.print();

'

__'>'_

complex *point = &x1; // point = new complex;

point >re = 1.24;

point >im = 2.3;

point >print();

. - , , ' . () '. : public, private, protected.

(public) - . - , . ' ':

'_'.'__;

__ᒺ.'__;

__'->'__;

(private) . - - , .

(protected) .

class. .

.

class complex

{

double re, im; // private

public:

double real(){return re;}

double imag(){return im;}

void set(double x,double y){re = x; im = y;}

};

.

'. ' set ( complex), '. ' , .

'_(__){__}

' ++ ' . ' new ' .

omplex(double re1 = 0.0,double im1 = 0.0){re = re1; im = im1;}

' ' - .

:

* , . void .

* .

* .

* virtual, static, const, mutuable, valatile.

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

, (T&). () '. . :

'_ '_'(_);

'_(_);

. ' :

complex ss(5.9,0.15);

' :

complex ss = complex(5.9,0.15);

' . , . . .

'_()

.

class CLASS_A

{

int i; float e; char c;

public:

CLASS_A(int ii,float ee,char cc): i(8),e(i * ee + ii),(){}

...

};

.

#include <string.h>

#include <iostream.h>

class string

{

char *ch; //

int len; //

public:

//

// '

string(int N = 80): len(0){ch = new char[N+1]; ch[0] = '\0';}

// '

string(const char *arch){len = strlen(arch);

ch = new char[len+1];

strcpy(ch,arch);}

// -

//

int& len_str(void){return len;}

//

char *str(void){return ch;}

...};

string , .

T::T(const T&), ' . , ', . :

) ' ;

) ' , ;

) ' '.

, , '. . . , string:

string(const string& st)

{len=strlen(st.len);

ch=new char[len+1];

strcpy(ch,st.ch); }

', ( ).

' , .

class demo{

int x;

public:

demo(){x=0;}

demo(int i){x=i;}

};

void main(){

class demo a[20]; // ( )

class demo b[2]={demo(10),demo(100)};//

.

' ' ' '. , ' , , , ' , ' . ' . . :

~_(){__}

' ' , ~ ().

, . (), ' .

, delete '.

string *p=new string();

delete p;

, , ', '. , ' ', , ch ' string, : ~string(){delete []ch;}

, .

-.

-.

__('_::*'___)(__);

.

double(complex::*ptcom)(); //

ptcom = &complex:: real; //

// '

complex A(5.2,2.7);

cout<<(A.*ptcom)();

typedef double&(complex::*PF)();

PF ptcom=&complex::real;

.

1. ( ).

2. : , , .

3. .

4. - .

5. -.

6. .

7. , ' ( ', ).

8. ' -.

.

1. .

const int LNAME=25;

class STUDENT{

char name[LNAME]; // '

int age; //

float grade; //

public:

STUDENT(); //

STUDENT(char*,int,float); //

STUDENT(const STUDENT&); //

~STUDENT();

char * GetName();

int GetAge() const;

float GetGrade() const;

void SetName(char*);

void SetAge(int);

void SetGrade(float);

void Set(char*,int,float);

void Show(); };

name : char* name. - .

2. .

STUDENT::STUDENT(char*NAME,int AGE,float GRADE)

{

strcpy(name,NAME); age=AGE; grade=GRADE;

cout<<\n ' <<this<<endl;

}

3. . , :

) ' '

STUDENT a(,19,50), b=a;

) '

void View(STUDENT a){a.Show;}

) ' ,

STUDENT NoName(STUDENT & student)

{STUDENT temp(student);

temp.SetName(NoName);

return temp;}

STUDENT c=NoName(a);

4. ' , ', '.

.

) '

STUDENT gruppa[3];

gruppa[0].Set(,19,50);

..

STUDENT gruppa[3]={STUDENT(,19,50),

STUDENT(,18,25.5),

STUDENT(,18,45.5)};

) '

STUDENT *p;

p=new STUDENT[3];

p-> Set(,19,50);

..

5.

void (STUDENT::*pf)();

pf=&STUDENT::Show;

(p[1].*pf)();

6.

* h-

* cpp-

* pp-

-

#ifndef STUDENTH

#define STUDENTH

// STUDENT.H

...

#endif

.

1. : , , , ', , .

2. . , , , - ..

3. .

4. .

5. , ' .

6.˳ , .

.

-

1. 2. 3.
' char* ' char* ' char*
int int -int
- int(bool) int - int
4.в 5.˲ 6.
'- char* '- char* ' -char*
char* char* - int
- int - float - int
7. 8. 9.ֲ
'- char* '- char* - int
- char* - int - int
- int - float - float
10. 11. 12.
'- char* '- char* -char*
char* int - int
- int - int(bool) - float
13. 14. 15.
'- char* '- char* '-char*
char* -char* - int
float int - char*

2.

. .

.

, . ' ' , . .

(static). ', . .

'_:: '_ ;

, int complex:: count = 0;

. ҳ ' . ' '

'_'.'_

, ' . ' ', '

'_:: '_

public .

private -. ֳ ' .

'_:: '__

.

#include <iostream.h>

class TPoint

{

double x,y;

static int N; // - :

public:

TPoint(double x1 = 0.0,double y1 = 0.0){N++; x = x1; y = y1;}

static int& count(){return N;} // -

};

int TPoint:: N = 0; // -

void main (void)

{TPoint A(1.0,2.0);

TPoint B(4.0,5.0);

TPoint C(7.0,8.0);

cout<<\n <<TPoint:: count()<<.; }

this

- ', ', . ' this

'_ *const this = _'

this . this ', . ' .

this . , - this '.

this ' .

. .

, . ; , - . , .

, .

' ' (public, protected).

' . :

private - - . .

protected , private, - - , .

public - , , public - ' '.

, friend .

:

class '_: __

{__};

private, class, public, struct

, , - private, protected public, .

, - . . . .

.

class Basis

{ int a,b;

public:

Basis(int x,int y){a=x;b=y;}

};

class Inherit:public Basis

{int sum;

public:

Inherit(int x,int y, int s):Basis(x,y){sum=s;}

};

' -: , -' ( ), . ' ᒺ ' .

' : , -', '.

, ' .

³ .

, . , , .

³ () '. - , virtual.

.

class base

{

public:

virtual void print(){cout<<\nbase;}

...

};

class dir: public base

{

public:

void print(){cout<<\ndir;}

};

void main ()

{

base B,*bp = &B;

dir D,*dp = &D;

base *p = &D;

bp >print(); // base

dp >print(); // dir

p >print(); // dir

}

, ', .

, ', ( ) , .

³ -.

³ . ϳ , ( ) , virtual .

, . , , .

, () .

, :

virtual '_(__) = 0;

. . .

, . . . ³ . . , .

.

class Base{

public:

Base(); //

Base(const Base&); //

virtual ~Base(); //

virtual void Show()=0; //

//

protected: //

private:

// ,

};

class Derived: virtual public Base{

public:

Derived(); //

Derived(const Derived&); //

Derived(); //

virtual ~Derived(); //

void Show(); //

//

//

protected:

// private,

private:

//

};

' , . ' , , ', . '.

. . , , . - .

: 䳺 . , Draw, : TTriangle, TCircle, TSquare. TGraphObject. , , TGraphObject -. Draw ' () TGraphObject, TTriangle, TCircle, TSquare.

, . ᒺ- , ++, , , . , TGraphObject, . '. Draw , , . Draw TGraphObject , Draw TGraphObject . , , Draw .

.

1. ( ).

2. - ' ' .

3. .

4. , ' , .

5. , .

6. , ' , ( 6 ).

.

1. ' , ( ). , . .

2. .

3. protected.

4. :

static person* begin; //

static void print(void); //

5. - , .

6. ' , ' . , a.Add() - ' a .

' ', . , ' . .

7. Show '.

8. ', .

9. , , .

.

1. : , , , ', , .

2. . , , , - ..

3. .

4. .

5. .

6. ' .

7. .

8.˳ .

9. . , .

.

.

, , , .

2. , , ,

3. , , ,

4. , , ,

5. , , ,

6. , , ,

7. , , ,

8. , , ,





:


: 2016-07-29; !; : 882 |


:

:

,
==> ...

1744 - | 1736 -


© 2015-2024 lektsii.org - -

: 0.231 .