, . , , (vtable), . vtable.
, , vptr . , vptr .
vtable, vptr , . ( ), ( ). set_year (), , print(), .
Birthday Birthday*, Date*. .
.
ß
Date *pd;
pd = new date(10, 10, 2000);
pd->print(); // date
delete pd;
pd = new birthday(10, 10, 2000, Peter);
pd->print(); // birthday
, - (pd->print() print() set_year() ), . , vptr , , .
. ++ .
++ , .
class AB: public A, public B {...};
AB A B. , :
A:: B::.
, . .
, A B R ( ).
R R R
| | / \
A B A B
\ / \ /
AB AB
) )
AB R R AB , R :
A:: B::.
AB , R, A B.
|
|
ß
class A: virtual public R {...
class B: virtual public R {...
, .1-, .
. .
ß
class C0 { public: void f() { cout << "f from C0" << endl; }; }; class C1: public C0 { public: void f() { cout << "f from C1" << endl; }; }; class C2: public C0 { public: void f() { cout << "f from C2" << endl; }; }; class C3: public C1, public C2 { public: void f() { cout << "f from C3" << endl; }; }; ///////////////////////// /////////////// C3 c; c.C1::f(); // uses vtbl for C1 c.C2::f(); // uses vtbl for C2 // ((C1)c).f(); - ! ((C0)(C1)c).f(); |
, .
. .
ß
//
class Complex {
public:
Complex(float re, float im)
{real = re; imag = im;};
}
//
class Triplex: public Complex {
public:
Triplex(float re, float im, int co):
Complex(re, im) { color = co;};
}
- , .
ß
Triplex(float re, float im, int co):Complex(re, im), color(co) {};
-, - -, .
#include <iostream>
using namespace std;
class base {
int i, j;
public:
void set(int a, int b) { i=a; j=b; }
void show() { cout << i << " " << j << "\n"; }
};
class derived: public base {
int k;
public:
derived(int x) { k=x; }
void showk() { cout << k << "\n"; }
};
int main()
{
derived ob(3);
ob.set(1, 2); // base
ob.show(); // base 1 2
ob.showk(); // derived 3
cin.get();
return 0;
}
//////////
#include <iostream>
using namespace std;
class base {
int i, j;
public:
void set(int a, int b) { i=a; j=b; }
void show() { cout << i << " " << j << "\n"; }
int y;
};
class derived: private base {
int k;
public:
derived(int x) { k=x; }
void showk() { cout << k << "\n"; }
base::show;//
};
int main()
{
derived ob(3);
// ob.set(1, 2); // base
ob.show(); // base 1 2
ob.showk(); // derived 3
cin.get();
return 0;
}
#include <iostream>
using namespace std;
class base {
int i;
public:
base() { cout<<"create base\n";}
|
|
~base() { cout<<"delete base\n";}
};
class derived: public base {
public:
derived() { cout<<"create derived\n"; }
~derived() { cout<<"delete derived\n"; }
};
int main()
{
{
derived ob;
derived *pob;
pob=new derived;
delete pob;
}
cin.get();
return 0;
}
//////////////////////////
#include <iostream>
using namespace std;
class base1 {
public:
base1() { cout<<"create base1\n";}
~base1() { cout<<"delete base1\n";}
};
class base2 {
public:
base2() { cout<<"create base2\n";}
~base2() { cout<<"delete base2\n";}
};
class derived: public base1, public base2{
public:
derived() { cout<<"create derived\n"; }
~derived() { cout<<"delete derived\n"; }
};
int main()
{
{
derived ob;
}
cin.get();
return 0;
}
:
create base1
create base2
create derived
delete derived
delete base2
delete base1
///////////////////
#include <iostream>
using namespace std;
class base {
int i;
public:
base(){i=5; cout<<"create "<<i<<" base \n";}
~base(){cout<<"delete "<<i<<" base \n";}
};
class derived: private base {
int k;
public:
derived(){k=6; cout<<"create "<<k<<" derived \n";}
~derived(){cout<<"delete "<<k<<" derived \n";}
};
int main()
{
{
derived ob;
}
cin.get();
return 0;
}
:
create 5 base
create 6 derived
delete 6 derived
delete 5 base
#include <iostream>
using namespace std;
class base {
int i;
public:
base(int x){i=x; cout<<"create "<<i<<" base \n";}
~base(){cout<<"delete "<<i<<" base \n";}
};
class derived: private base {
int k;
public:
derived(int x):base(x)
{k=x; cout<<"create "<<k<<" derived \n";}
~derived(){cout<<"delete "<<k<<" derived \n";}
};
int main()
{
{
derived ob(1);
derived *pob;
pob=new derived(2);
}
cin.get();
return 0;
}
:
create 1 base
create 1 derived
create 2 base
create 2 derived
delete 1 derived
delete 1 base
#include <iostream>
using namespace std;
class base1 {
int i;
public:
base1(int x){i=x; cout<<"create "<<i<<" base1 \n";}
~base1(){cout<<"delete "<<i<<" base1 \n";}
};
class base2 {
int i;
public:
base2(int x){i=x; cout<<"create "<<i<<" base2 \n";}
~base2(){cout<<"delete "<<i<<" base2 \n";}
};
class derived: public base1,public base2 {
int k;
public:
derived(int x, int y):base1(x),base2(y)
{k=x+y; cout<<"create "<<k<<" derived \n";}
~derived(){cout<<"delete "<<k<<" derived \n";}
};
int main()
{
{
derived ob(1,2);
derived *pob;
pob=new derived(3,4);
delete pob;
}
cin.get();
return 0;
}
create 1 base1
create 2 base2
create 3 derived
create 3 base1
create 4 base2
create 7 derived
delete 7 derived
delete 4 base2
delete 3 base1
delete 3 derived
delete 2 base2
delete 1 base1
:
int main()
{
{
base1 *pob;
pob=new derived(3,4);
delete pob;
}
cin.get();
return 0;
}
:
create 3 base1
create 4 base2
create 7 derived
delete 3 base1
:
|
|
int main()
{
{
base1 *pob;
pob=new derived(3,4);
delete (derived*)pob;
}
cin.get();
return 0;
}
create 3 base1
create 4 base2
create 7 derived
delete 7 derived
delete 4 base2
delete 3 base1
/////////////
#include <iostream>
using namespace std;
class base{
int i;
public:
base(int x){i=x; cout<<"create "<<i<<" base \n";}
~base(){cout<<"delete "<<i<<" base \n";}
void showdata(){cout<<" "<<i<<" base \n";}
};
class derived: public base{
int k;
public:
derived(int x):base(x)
{k=x+x; cout<<"create "<<k<<" derived \n";}
~derived(){cout<<"delete "<<k<<" derived \n";}
void showdata(){cout<<" "<<k<<" derived \n";}
};
int main()
{
{
base *pob;
pob=new derived(3);
pob->showdata();
delete (base*)pob;
}
cin.get();
return 0;
}
:
create 3 base
create 6 derived
3 base //.. ..
delete 3 base
/////////////
#include <iostream>
using namespace std;
class base{
int i;
public:
base(int x){i=x; cout<<"create "<<i<<" base \n";}
virtual~base(){cout<<"delete "<<i<<" base \n";}
virtual void showdata(){cout<<" "<<i<<" base \n";}
};
class derived: public base{
int k;
public:
derived(int x):base(x)
{k=x+x; cout<<"create "<<k<<" derived \n";}
virtual~derived(){cout<<"delete "<<k<<" derived \n";}
void showdata(){cout<<" "<<k<<" derived \n";}
};
int main()
{
{
base *pob;
pob=new derived(3);
pob->showdata();
delete pob;
}
cin.get();
return 0;
}
:
6.4
.
6.6
1
. , . . .
, , STUDENT, :
- ; - ; - ( ).
.
:
- , , , , .
, :
- , STUDENT;
- , , 4.0;
2
. , . . .
|
|
, , STUDENT, ;
- ; - ; - ( ).
.
:
- , , , , .
, :
- , STUDENT;
- , 4 5, , .
3
. , . . .
, , STUDENT, :
- ; - ; - ( ).
.
:
- , , , , .
, :
- , STUDENT; ;
- , 2, , .
4
. , . . .
, , AEROFLOT, :
- ; - ; - .
.
:
- , , , , .
, :
- , AEROFLOT;
- , , , ;
5
. , . . .
, , AEROFLOT, :
- ;- ; - .
.
:
- , , , .
, :
- , AEROFLOT;
- , , ;
- , .
6
. , . . .
, , WORKER, :
|
|
- ; - ; - .
.
:
- , ( ), , .
, :
- , WORKER; - , , .
7
. , . . .
, , TRAIN, :
- ; - ; - .
.
:
- , ( ), , .
, :
- , TRAIN;
- , .
8
. , . . .
, , TRAIN, :
- ; - ;- .
.
:
- , ( ), , .
, :
- , TRAIN;
- , , .
9
. , . . .
, , TRAIN, :
- ; - ; - .
.
:
- ( ).
, :
- , TRAIN;
- , .
10
. , . . .
, , MARSH, :
- ; - ; - .
.
:
- ( ), , ..
, :
- , MARSH; - , ;
11
. , . . .
, , MARSH, :
- ; - ; - .
.
:
- , ( ), , .
, :
- , MARSH;- , , ;
- , .
12
. , . . .
, , NOTE, :
- , ; - ; - ( ).
.
:
- , ( ).
.
:
- ( ), , .
, :
- , NOTE;
- , .
13
. , . . .
, , NOTE, :
- , ; - ; - ( ).
.
:
- , ( ), , .
, :
- , NOTE;
- , ,
14
. , . . .
, , NOTE, :
- , ; - ; - ( ).
.
:
- ( ), , .
, :
- , NOTE;
- , .
15
. , . . .
, , ZNAK, :
- , ; - ; - ( ).
.
:
- ( ), , .
, :
- , ZNAK;
- , .
16
. , . . .
, , ZNAK, :
- , ; - ; - ( ).
.
:
- , ( ), , .
, :
- , ZNAK;
- , , !.
17
. , . . .
, , ZNAK, : - , ; - ; - ( ).
.
:
- , ( ), , .
, :
- , ZNAK;
- , , .
18
. , . . .
, , PRICE, :
- ;- , ; - .
. :
- ( ), , .
, :
- , PRICE;
- , .
19
. , . . .
, , PRICE, :
- ; - , ; - .
.
:
- , ( ), , .
.
:
- , ( ), , .
, :
- , PRICE;
- , , .
20
. , . . .
, , ORDER, :
- ; - ; - .
.
:
- ( ), , .
, :
- , ORDER;
- , , ;
21
. , . . .
, , STUDENT, ; - ;
- ; - ( ).
.
:
- , .
, :
- , STUDENT; - , ;
22
. , . . .
, , STUDENT, ; - ;
- ; - ( ).
.
:
- , ( ), , .
, :
- , STUDENT;
- , 2.0 4.5.
23
. , . . .
, , PRICE, :
- ; - , ; - .
.
:
- , ( ), , .
, :
- , PRICE;
- , , .
24
. , . . .
, , PRICE, :
- ; - , ; - .
.
:
- , ( ), , .
, :
- , PRICE;
- , , .
25
. , . . .
, , COMP, :
- ; - ; - .
.
:
- .
, :
- , COMP;
- ;
26
. , . . .
, , COMP, :
- ; - ; - .
.
:
- ( ), , .
, :
- , COMP;
- 64 1024.
27
. , . . .
, , COMP, :
- ; - ; - .
:
- ( ), , .
, :
- , COMP;
- 128 1000.
//
//
#include <iostream>
#include <string>
using namespace std;
struct TStud
{
string Name;
int Bal;
};
template <typename T>
class TMen
{
protected:
T *mas;
int n;
public:
TMen()
{n=2;mas=new T[n];}
TMen(int k)
{n=k;mas=new T[n];}
TMen(const TMen &Men)
{
if (this==&Men) return;
this->n=Men.n;
mas=new T[n];
for(int i=0;i<n;i++)
mas[i]=Men.mas[i];
}
virtual ~TMen()
{ delete []mas; }
virtual void input()=0;
virtual void output()=0;
};
class Stud:public TMen<TStud>
{
public:
Stud():TMen<TStud>(){;};
Stud(int k):TMen<TStud>(k){;};
Stud(const Stud &St):TMen<TStud>(St){;};
virtual void input()
{
cout<<"Input info for "<<n <<" students"<<endl;
for(int i=0;i<n;i++)
{
cout<<"Input name: ";
cin>>mas[i].Name;
cout<<"Input bal: ";
cin>>mas[i].Bal;
}
};
virtual void output()
{
for(int i=0;i<n;i++)
cout<<mas[i].Name<<'\t'<<mas[i].Bal<<'\n';
};
};//class Stud
int main(int argc, char* argv[])
{
Stud st1(3);
st1.input();
Stud st2(st1);
st2.output();
cin.get();
cin.get();
return 0;
}
//---------------------------------------------------------------------------
˲
1. .. /++. .: 2004., 461 .
2. . C++: . - .: , 2003. 521 .
3. .. C C++. .: , 2006. 647 .
4. ., . : . . .: , 1992. 272 .
5. . ++. 1. : "", 1993. 264 .
6. . ++. 2. : "", 1993. 296 .
7. .. +: . . .: , 1995. 400 .
8. .. . ++. : , 2002. 500 .
9. .., .. ++.- .: ̻, 2000 . 1024 .
10. . , , .-M.:,1985.
11. .. . . . .:, 1993. 224.
12. . : .-: , 1994. 221.
13. . : . . .: , 1994. 160 .
Ͳ Dz
_____________
.
:
ij
³ ..
..
..
2006, . 29
ϳ. 8.07.06. 6084 1/16. -
. . . 2,6. . .2,3. 75 .
. 1-122. ֳ .
. 61166 , . , 14.
³ -
-
61166 , . , 14.