.
, . , . , , . :
lass __: [] __
{__} [ ];
. -
, class struct. public. , (union) , .
. : private, protected public ( ). :
class base
{ private: private-;
public: public-;
protected: protected-;
};
class proizv_priv: private base { };
class proizv_publ: public base { };
class proizv_prot: protected base { };
-
:
public, public protected
public protected . private private- ; protected, public protected protected . private private- ; private, public protected private . private private- . : , .
, private protected -
. ( ).
#include <iostream>
using namespace std;
#include <string.h>
#define n 10
class book // book
{ protected:
char naz[20]; //
int kl; //
public:
book(char *,int); // book
~book(); // book
};
class avt: public book //
{ char fm[10]; //
public:
avt(char *,int,char *); // avt
~avt(); // avt
|
|
void see();
};
enum razd {teh,hyd,uch};
class rzd: public book //
{ razd rz; //
public:
rzd(char *, int, razd); // rzd
~rzd(); // rzd
void see();
};
book::book(char *s1,int i): kl(i)
{ cout << "\n book";
strcpy(naz,s1);
}
book::~book()
{cout << "\n book";}
avt::avt(char *s1,int i,char *s2): book(s1,i)
{ cout << "\n avt";
strcpy(fm,s2);
}
avt::~avt()
{cout << "\n avt";}
void avt::see()
{ cout<<"\n: "<<naz<<"\n: "<<kl;
}
rzd::rzd(char *s1,int i,razd tp): book(s1,i), rz(tp)
{ cout << "\n rzd";
}
rzd::~rzd()
{cout << "\n rzd";}
void rzd::see()
{ switch(rz)
{ case teh: cout << "\n "; break;
case hyd: cout << "\ n "; break;
case uch: cout << "\ n "; break;
}
}
int main()
{avt av(" 1",123," 1");// book avt
rzd rz(" 1",123,teh); // book rzd
av.see();
rz.see();
}
.
, -
, .
-
, .
. , , , . , , . . - . , , , , . (virtual) .
, virtual . ( ) , , . , , , .
#include "iostream"
#include "iomanip"
|
|
using namespace std;
#include "string.h"
class grup //
{ protected:
char *fak; //
long gr; //
public:
grup(char *FAK,long GR): gr(GR)
{ if (!(fak=new char[20]))
{ cout<<" "<<endl;
return;
}
strcpy(fak,FAK);
}
~grup()
{ cout << " grup " << endl;
delete fak;
}
virtual void see(void); //
};
class stud: public grup //
{ char *fam; //
int oc[4]; //
public:
stud(char *FAK,long GR,char *FAM,int OC[]): grup(FAK,GR)
{ if (!(fam=new char[20]))
{ cout<<" "<<endl;
return;
}
strcpy(fam,FAM);
for(int i=0;i<4;oc[i]=OC[i++]);
}
~stud()
{ cout << " stud " << endl;
delete fam;
}
void see(void);
};
void grup::see(void) //
{ cout << fak << gr << endl;}
void stud::see(void) //
{ grup::see(); //
cout <<setw(10) << fam << " ";
for(int i=0; i<4; cout << oc[i++]<< );
cout << endl;
}
int main()
{ int OC[]={4,5,5,3};
grup gr1(" 1",123456), gr2(" 2",345678), *p;
stud st(" 2",150502,"",OC);
p=&gr1; //
p->see(); // gr1
(&gr2)->see(); // gr2
p=&st; //
p->see(); // st
return 0;
}