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

	<entry>
		<id>http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/destructor&amp;diff=2315&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/destructor&amp;diff=2315&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://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/destructor&amp;diff=2316&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://www.cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/destructor&amp;diff=2316&amp;oldid=prev"/>
				<updated>2010-05-25T10:29:26Z</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;==A simple constructor and destructor.==&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;
 &lt;br /&gt;
class MyClass { &lt;br /&gt;
public: &lt;br /&gt;
  int x; &lt;br /&gt;
 &lt;br /&gt;
  MyClass();  // constructor &lt;br /&gt;
  ~MyClass(); // destructor &lt;br /&gt;
};   &lt;br /&gt;
 &lt;br /&gt;
// Implement MyClass constructor. &lt;br /&gt;
MyClass::MyClass() { &lt;br /&gt;
  x = 10; &lt;br /&gt;
}   &lt;br /&gt;
 &lt;br /&gt;
// Implement MyClass destructor. &lt;br /&gt;
MyClass::~MyClass() { &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Destructing...\n&amp;quot;; &lt;br /&gt;
} &lt;br /&gt;
   &lt;br /&gt;
int main() {   &lt;br /&gt;
  MyClass ob1; &lt;br /&gt;
  MyClass ob2; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; ob1.x &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; ob2.x &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &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;10 10&lt;br /&gt;
Destructing...&lt;br /&gt;
Destructing...&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Close stream in destructor==&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;fstream&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass&lt;br /&gt;
{&lt;br /&gt;
private:&lt;br /&gt;
       char msg[20];&lt;br /&gt;
       int loopcounter;&lt;br /&gt;
       fstream myfile;&lt;br /&gt;
       &lt;br /&gt;
public:&lt;br /&gt;
  void display(); &lt;br /&gt;
  MyClass();&lt;br /&gt;
  ~MyClass();&lt;br /&gt;
};&lt;br /&gt;
MyClass::MyClass()&lt;br /&gt;
{&lt;br /&gt;
       myfile.open(&amp;quot;input.txt&amp;quot;,ios::in);&lt;br /&gt;
       myfile.getline(msg,20);&lt;br /&gt;
}&lt;br /&gt;
MyClass::~MyClass()&lt;br /&gt;
{&lt;br /&gt;
  myfile.close();&lt;br /&gt;
}&lt;br /&gt;
void MyClass::display()&lt;br /&gt;
{&lt;br /&gt;
         cout &amp;lt;&amp;lt; msg &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
int main() {&lt;br /&gt;
       MyClass obj;&lt;br /&gt;
       obj.display();&lt;br /&gt;
       return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Demonstrating the order in which constructors and destructors are called.==&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;string&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::string;&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, string ); // constructor&lt;br /&gt;
   ~MyClass();             // destructor&lt;br /&gt;
private:&lt;br /&gt;
   int objectID;&lt;br /&gt;
   string message; &lt;br /&gt;
};&lt;br /&gt;
MyClass::MyClass( int ID, string s )&lt;br /&gt;
{&lt;br /&gt;
   objectID = ID;&lt;br /&gt;
   message = s; &lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Object &amp;quot; &amp;lt;&amp;lt; objectID &amp;lt;&amp;lt; &amp;quot; constructor runs &amp;quot; &amp;lt;&amp;lt; message &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
MyClass::~MyClass()&lt;br /&gt;
{ &lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;Object &amp;quot; &amp;lt;&amp;lt; objectID &amp;lt;&amp;lt; &amp;quot; destructor runs &amp;quot; &amp;lt;&amp;lt; message &amp;lt;&amp;lt; endl; &lt;br /&gt;
}&lt;br /&gt;
void create( void );&lt;br /&gt;
MyClass first( 1, &amp;quot;(global before main)&amp;quot; );&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   MyClass second( 2, &amp;quot;(local automatic in main)&amp;quot; );&lt;br /&gt;
   static MyClass third( 3, &amp;quot;(local static in main)&amp;quot; );&lt;br /&gt;
   &lt;br /&gt;
   create(); // call function to create objects&lt;br /&gt;
   MyClass fourth( 4, &amp;quot;(local automatic in main)&amp;quot; );&lt;br /&gt;
   return 0;&lt;br /&gt;
}&lt;br /&gt;
void create( void )&lt;br /&gt;
{&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;\nCREATE FUNCTION: EXECUTION BEGINS&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
   MyClass fifth( 5, &amp;quot;(local automatic in create)&amp;quot; );&lt;br /&gt;
   static MyClass sixth( 6, &amp;quot;(local static in create)&amp;quot; );&lt;br /&gt;
   cout &amp;lt;&amp;lt; &amp;quot;\nCREATE FUNCTION: EXECUTION ENDS&amp;quot; &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Object 1 constructor runs (global before main)&lt;br /&gt;
Object 2 constructor runs (local automatic in main)&lt;br /&gt;
Object 3 constructor runs (local static in main)&lt;br /&gt;
CREATE FUNCTION: EXECUTION BEGINS&lt;br /&gt;
Object 5 constructor runs (local automatic in create)&lt;br /&gt;
Object 6 constructor runs (local static in create)&lt;br /&gt;
CREATE FUNCTION: EXECUTION ENDS&lt;br /&gt;
Object 5 destructor runs (local automatic in create)&lt;br /&gt;
Object 4 constructor runs (local automatic in main)&lt;br /&gt;
Object 4 destructor runs (local automatic in main)&lt;br /&gt;
Object 2 destructor runs (local automatic in main)&lt;br /&gt;
Object 6 destructor runs (local static in create)&lt;br /&gt;
Object 3 destructor runs (local static in main)&lt;br /&gt;
Object 1 destructor runs (global before main)&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==virtual destructor methods==&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;
 &lt;br /&gt;
 class Animal&lt;br /&gt;
 {&lt;br /&gt;
 public:&lt;br /&gt;
     Animal():itsAge(1) { std::cout &amp;lt;&amp;lt; &amp;quot;Animal constructor...\n&amp;quot;; }&lt;br /&gt;
     virtual ~Animal() { std::cout &amp;lt;&amp;lt; &amp;quot;Animal destructor...\n&amp;quot;; }&lt;br /&gt;
     virtual void Speak() const { std::cout &amp;lt;&amp;lt; &amp;quot;Animal speak!\n&amp;quot;; }&lt;br /&gt;
 protected:&lt;br /&gt;
     int itsAge;&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 class Cat: public Animal&lt;br /&gt;
 {&lt;br /&gt;
 public:&lt;br /&gt;
     Cat() { std::cout &amp;lt;&amp;lt; &amp;quot;Cat constructor...\n&amp;quot;; }&lt;br /&gt;
     ~Cat() { std::cout &amp;lt;&amp;lt; &amp;quot;Cat destructor...\n&amp;quot;; }&lt;br /&gt;
     void Speak()const { std::cout &amp;lt;&amp;lt; &amp;quot;Meow!\n&amp;quot;; }&lt;br /&gt;
 };&lt;br /&gt;
 &lt;br /&gt;
 int main()&lt;br /&gt;
 {&lt;br /&gt;
     Animal *pCat = new Cat;&lt;br /&gt;
     pCat-&amp;gt;Speak();&lt;br /&gt;
     return 0;&lt;br /&gt;
 }&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Animal constructor...&lt;br /&gt;
Cat constructor...&lt;br /&gt;
Meow!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>