.


:




:

































 

 

 

 


.




, . , . , , . . , .

 

: , (, ). std::string (#include <string>), , . , ( , ):

struct Person /* "" */

{

std::string firstname, lastname; /* , */

};

, : , , , . (, , ), Person :

struct Employee: Person /* ""; "" */

{

std::string department; /* */

};

Employee department, firstname lastname, Person. , Employee:

Employee e;

e.firstname = "Ivan";

e.lastname = "Petrov";

e.department = "sales";

std::cout << e.firstname << ", " << e.lastname << ", " <<

e.department << "\n";

e Person , Employee. , ( Person), (base), (Employee) (derived). : , . :

class Person

{

public:

std::string firstname, lastname;

};

C++ , private. , Employee, (firstname lastname . :

class Employee: public Person

{

public:

std::string department;

};

, public.

 

Person Employee : () public ( ). , . Person, firstname lastname :

class Person

{

std::string firstname, lastname; /* */

public:

Person(std::string f, std::string l): firstname(f),

lastname(l)

{ }

std::string getFirstname() { return firstname; }

std::string getLastname() { return lastname; }

};

, Person(std::string f, std::string l): firstname(f), lastname(l) { } . , firstname lastname. Person ( { }). , : Person(std::string f, std::string l) { firstname = f; lastname = l; } firstname lastname , . Person, :

Person p("Ivan", "Petrov");

std::cout << p.getFirstname() << ", " <<

p.getLastname() << "\n";

? , getFirstname() getLastname() Employee, . . public . , firstname lastname Employee, Employee : firstname(f) . , , , Person :

class Employee: public Person

{

std::string department;

public:

Employee(std::string f, std::string l, std::string d):

Person(f, l), department(d)

{ }

std::string getDepartment() { return department; }

};

:

Employee e("Ivan", "Petrov", "sales");

std::cout << e.getFirstname() << ", " <<

e.getLastname() << ", " <<

e.getDepartment() << "\n";

Person ( firstname lastname Ivan Petrov ), Employee. : , .

 

11. . ?

, (, , ). , , ~. .

class Rational

{

...

public:

~Rational()

{

}

};

( Rational) . , , .

. , ( ) , . : , , , .

 

:

1) ;

2) ;

3) ( this).

static, . ++, friend, . , , , . , , . :

 

class X {

int a;

friend void friend_set(X*, int);

public void member_set(int);

};

 

void friend_set(X* p, int i) {p->a = i;};

void X::member_set(int i) {a = i};

 

void f {

X obj;

friend_set(&obj, 10);

obj.member_set(10);

};

, , . , , . , matrix vector . . , . :

class matrix; //

 

class vector {

float v[4];

//...

friend vector multiply(const matrix&, const vector&);

};

class matrix {

vector v[4];

//...

friend vector multiply(const matrix&, const vector&);

};

, :

vector multiply(const matrix& m, const vector& v) {

vector r;

for (int i = 0; i < 4; i++) { // r[i] = m[i] * v

r.v[i] = 0;

for (int j = 0; j < 4; j++) r.v[i] += m.v[i][j] * v.v[j];

};

return r;

}

, , . , , , , . . this .

:

class X {

//...

void f();

};

class Y {

//...

friend void X::f();

};

Y, :

class Y {

//...

friend class X;

};

, friend, . :

static void f() { /*... */};

class X { friend g(); };// extern g()

class Y {

friend void f();// : f()

};

static g() { /*... */};// :

 





:


: 2015-10-01; !; : 471 |


:

:

, , . , .
==> ...

1579 - | 1415 -


© 2015-2024 lektsii.org - -

: 0.037 .