A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FClass%2Fmember_variable</id>
		<title>C++ Tutorial/Class/member variable - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FClass%2Fmember_variable"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/member_variable&amp;action=history"/>
		<updated>2026-04-10T17:39:26Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/member_variable&amp;diff=2293&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/member_variable&amp;diff=2293&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/member_variable&amp;diff=2294&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/member_variable&amp;diff=2294&amp;oldid=prev"/>
				<updated>2010-05-25T10:29:22Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Assign value to member variable directly from assignment==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass{&lt;br /&gt;
  int a;&lt;br /&gt;
  public:&lt;br /&gt;
    MyClass(int j) {a = j;}&lt;br /&gt;
    int geta(void) {return a;}&lt;br /&gt;
};&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  MyClass ob = 99;              &lt;br /&gt;
  cout &amp;lt;&amp;lt; ob.geta();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Create a getter for member variable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass{&lt;br /&gt;
  int a;&lt;br /&gt;
  public:&lt;br /&gt;
    MyClass(int j) {a = j;}&lt;br /&gt;
    int geta(void) {return a;}&lt;br /&gt;
};&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  MyClass ob = 99;              &lt;br /&gt;
  cout &amp;lt;&amp;lt; ob.geta();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==increment counter variable with ++ operator==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;  &lt;br /&gt;
  using namespace std;  &lt;br /&gt;
  class Counter  &lt;br /&gt;
  {  &lt;br /&gt;
     private:  &lt;br /&gt;
        unsigned int count;              &lt;br /&gt;
     public:  &lt;br /&gt;
        Counter() : count(0){  }  &lt;br /&gt;
        unsigned int get_count(){ return count; }  &lt;br /&gt;
        void operator ++ (){  &lt;br /&gt;
           ++count;  &lt;br /&gt;
        }  &lt;br /&gt;
  };  &lt;br /&gt;
  int main(){  &lt;br /&gt;
     Counter c1, c2;                     &lt;br /&gt;
    &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;\nc1=&amp;quot; &amp;lt;&amp;lt; c1.get_count();  &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;\nc2=&amp;quot; &amp;lt;&amp;lt; c2.get_count();  &lt;br /&gt;
    &lt;br /&gt;
     ++c1;&lt;br /&gt;
     ++c2;&lt;br /&gt;
     ++c2;&lt;br /&gt;
    &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;\nc1=&amp;quot; &amp;lt;&amp;lt; c1.get_count();  &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;\nc2=&amp;quot; &amp;lt;&amp;lt; c2.get_count() &amp;lt;&amp;lt; endl;  &lt;br /&gt;
     return 0;  &lt;br /&gt;
  }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Initialize member variable==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
class MyClass &lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
   MyClass( int c = 0, int i = 1 );&lt;br /&gt;
   void add() &lt;br /&gt;
   { &lt;br /&gt;
      count += increment; &lt;br /&gt;
   }&lt;br /&gt;
   void print() const; &lt;br /&gt;
private:&lt;br /&gt;
   int count;&lt;br /&gt;
   const int increment; &lt;br /&gt;
};&lt;br /&gt;
MyClass::MyClass( int c, int i ): count( c ),increment( i )&lt;br /&gt;
{ &lt;br /&gt;
}&lt;br /&gt;
void MyClass::print() const&lt;br /&gt;
{&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;count = &amp;quot; &amp;lt;&amp;lt; count &amp;lt;&amp;lt; &amp;quot;, increment = &amp;quot; &amp;lt;&amp;lt; increment &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   MyClass value( 10, 5 );&lt;br /&gt;
   value.print();&lt;br /&gt;
   for ( int j = 1; j &amp;lt;= 3; j++ ) &lt;br /&gt;
   {&lt;br /&gt;
      value.add();&lt;br /&gt;
      value.print();&lt;br /&gt;
   }&lt;br /&gt;
   return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;count = 10, increment = 5&lt;br /&gt;
count = 15, increment = 5&lt;br /&gt;
count = 20, increment = 5&lt;br /&gt;
count = 25, increment = 5&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Initialize member variable in constructor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass&lt;br /&gt;
{&lt;br /&gt;
   public: &lt;br /&gt;
     MyClass::MyClass(void);&lt;br /&gt;
     void show_MyClass(void);&lt;br /&gt;
   private:&lt;br /&gt;
     int a;&lt;br /&gt;
     int b;&lt;br /&gt;
     int c;&lt;br /&gt;
};&lt;br /&gt;
MyClass::MyClass(void) : a(1), b(2), c(3) { };&lt;br /&gt;
void MyClass::show_MyClass(void)&lt;br /&gt;
{&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;a contains: &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;b contains: &amp;quot; &amp;lt;&amp;lt; b &amp;lt;&amp;lt; endl;&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;c contains: &amp;quot; &amp;lt;&amp;lt; c &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   MyClass numbers;&lt;br /&gt;
   numbers.show_MyClass();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Output variable initialization message==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
  int who;&lt;br /&gt;
  MyClass(int id);&lt;br /&gt;
} global_obj1(1), global_obj2(2);&lt;br /&gt;
MyClass::MyClass(int id)&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Initializing &amp;quot; &amp;lt;&amp;lt; id &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
  who = id;&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
  MyClass local_obj(3);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;This is NOT the first line displayed.\n&amp;quot;;&lt;br /&gt;
  MyClass local_obj2(4);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Share static variable among different class instances==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass {&lt;br /&gt;
    static int a;&lt;br /&gt;
    int b;&lt;br /&gt;
  public:&lt;br /&gt;
    void set(int i, int j) {&lt;br /&gt;
      a=i;&lt;br /&gt;
      b=j;&lt;br /&gt;
    }&lt;br /&gt;
    void show();&lt;br /&gt;
};&lt;br /&gt;
int MyClass::a;&lt;br /&gt;
void MyClass::show(){&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;This is static a: &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;This is non-static b: &amp;quot; &amp;lt;&amp;lt; b &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
int main(void){&lt;br /&gt;
  MyClass x, y;&lt;br /&gt;
  x.set(1,1);&lt;br /&gt;
  x.show();&lt;br /&gt;
  y.set(2,2);&lt;br /&gt;
  y.show();&lt;br /&gt;
  x.show();&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>