Skip to content

Instantly share code, notes, and snippets.

@AlexTalker
Created March 13, 2014 09:11
Show Gist options
  • Save AlexTalker/9524862 to your computer and use it in GitHub Desktop.
Save AlexTalker/9524862 to your computer and use it in GitHub Desktop.
#LyX 2.0 created this file. For more info see http://www.lyx.org/
\lyxformat 413
\begin_document
\begin_header
\textclass article
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman default
\font_sans default
\font_typewriter default
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100
\font_tt_scale 100
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\use_hyperref false
\papersize default
\use_geometry false
\use_amsmath 1
\use_esint 1
\use_mhchem 1
\use_mathdots 1
\cite_engine basic
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\use_refstyle 1
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\quotes_language english
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Part*
Лекция 5 - доступ к полям класса, public, private, protected.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
class SS{
\end_layout
\begin_layout Plain Layout
void setA();
\end_layout
\begin_layout Plain Layout
int getA();
\end_layout
\begin_layout Plain Layout
private:
\end_layout
\begin_layout Plain Layout
int a;
\end_layout
\begin_layout Plain Layout
} obj1;
\end_layout
\begin_layout Plain Layout
void SS::setA(int value){
\end_layout
\begin_layout Plain Layout
if(value > 100) value=100;
\end_layout
\begin_layout Plain Layout
a = value;
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
int SS::getA(){
\end_layout
\begin_layout Plain Layout
return a;
\end_layout
\begin_layout Plain Layout
}
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
То есть мы прячем данные за логикой.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Formula $ax^{2}+bx+c$
\end_inset
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
class One{
\end_layout
\begin_layout Plain Layout
public:
\end_layout
\begin_layout Plain Layout
void setA(...);
\end_layout
\begin_layout Plain Layout
void setB(...);
\end_layout
\begin_layout Plain Layout
void setC(...);
\end_layout
\begin_layout Plain Layout
vector getRoots();//(x1,x2), then if(roots-is-valide)
\end_layout
\begin_layout Plain Layout
private:
\end_layout
\begin_layout Plain Layout
bool roots-is-valide;//by default - false
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
void findRoots();// here set roots-in-valide in true.
\end_layout
\end_inset
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
то есть полезно добавлять переменную, отвечающую за состояние изменения
объекта.
\end_layout
\end_inset
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
каждый класс - минипрограмма, работающая со своими данными.
\end_layout
\end_inset
\end_layout
\begin_layout Standard
Конструктор - такая функция класса, которая вызывается в момент создания
объекта.
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
class Two{
\end_layout
\begin_layout Plain Layout
sometype x;// то есть выделяется память для x, а затем вызывается конструктор
\end_layout
\begin_layout Plain Layout
sometype y;
\end_layout
\begin_layout Plain Layout
};
\end_layout
\begin_layout Plain Layout
class One{
\end_layout
\begin_layout Plain Layout
One();
\end_layout
\begin_layout Plain Layout
Two a;// инициализируется 1-м
\end_layout
\begin_layout Plain Layout
Two b//инициализируется 2-м
\end_layout
\begin_layout Plain Layout
};
\end_layout
\begin_layout Plain Layout
One::One(){//на этото момент поля класса уже инициализированны значениями
по умолчанию для их типов.
\end_layout
\begin_layout Plain Layout
a = 3;
\end_layout
\begin_layout Plain Layout
b.selLength(17);
\end_layout
\begin_layout Plain Layout
}
\end_layout
\begin_layout Plain Layout
One obj1;
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
enum Color_t{ RED, YELLOW, BLUE};
\end_layout
\begin_layout Plain Layout
enum Contain{water,sand,rocks};
\end_layout
\begin_layout Plain Layout
class Bucket{
\end_layout
\begin_layout Plain Layout
public:
\end_layout
\begin_layout Plain Layout
Bucket(Color_t c=Blue, Contain n=Sand){col = c;cont = n;};
\end_layout
\begin_layout Plain Layout
private:
\end_layout
\begin_layout Plain Layout
Color_t col;
\end_layout
\begin_layout Plain Layout
Contain cont;
\end_layout
\begin_layout Plain Layout
public:
\end_layout
\begin_layout Plain Layout
void setColor(...);
\end_layout
\begin_layout Plain Layout
void setContain(...);
\end_layout
\begin_layout Plain Layout
};
\end_layout
\begin_layout Plain Layout
Bucket b1;
\end_layout
\begin_layout Plain Layout
Bucket b2(RED, water);
\end_layout
\begin_layout Plain Layout
class Builder{
\end_layout
\begin_layout Plain Layout
public:
\end_layout
\begin_layout Plain Layout
Builder(Color_t c, Contain n){
\end_layout
\begin_layout Plain Layout
bl.setColor(c);
\end_layout
\begin_layout Plain Layout
bl.setContain(n);
\end_layout
\begin_layout Plain Layout
br.setColor(c);
\end_layout
\begin_layout Plain Layout
br.setContain(n);
\end_layout
\begin_layout Plain Layout
}// way one
\end_layout
\begin_layout Plain Layout
Builder(Color_t c, Contain n): br(c,n), bl(c,n){}// way two
\end_layout
\begin_layout Plain Layout
private:
\end_layout
\begin_layout Plain Layout
Bucket bl, br;
\end_layout
\begin_layout Plain Layout
};
\end_layout
\end_inset
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
Убрав параметры по-умолчанию, вы не сможете инициализировать объект с параметрам
и по-молчанию.Example: Bucket b;
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset Graphics
filename 1.jpeg
\end_inset
\end_layout
\begin_layout Standard
Конструкторы вызываются в порядке A->B->S;
\end_layout
\begin_layout Part*
new и delete
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
class TypeA{...
\end_layout
\begin_layout Plain Layout
};
\end_layout
\begin_layout Plain Layout
TypeA d;
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
int* p = new int;
\end_layout
\begin_layout Plain Layout
*p = 7;
\end_layout
\begin_layout Plain Layout
*(p+1) = 3;//NO!
\end_layout
\begin_layout Plain Layout
//---------------
\end_layout
\begin_layout Plain Layout
int *p = new int[100];
\end_layout
\begin_layout Plain Layout
*p = 1;
\end_layout
\begin_layout Plain Layout
*(p+99) = 17;
\end_layout
\begin_layout Plain Layout
p[0] = 1;
\end_layout
\begin_layout Plain Layout
p[99] = 17;
\end_layout
\begin_layout Plain Layout
p[100] = 3;// this string not work
\end_layout
\begin_layout Plain Layout
delete []p;//free array memory
\end_layout
\end_inset
\begin_inset Foot
status collapsed
\begin_layout Plain Layout
деструктор - функция без параметров
\end_layout
\end_inset
\end_layout
\begin_layout Standard
\begin_inset listings
inline false
status open
\begin_layout Plain Layout
TypeA* pa=new TypeA(7);
\end_layout
\begin_layout Plain Layout
delete pa;
\end_layout
\begin_layout Plain Layout
//--------------------
\end_layout
\begin_layout Plain Layout
\end_layout
\end_inset
\end_layout
\end_body
\end_document
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment