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%2Fbase_class</id>
		<title>C++ Tutorial/Class/base class - История изменений</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%2Fbase_class"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/base_class&amp;action=history"/>
		<updated>2026-04-10T22:18:04Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/base_class&amp;diff=2323&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/base_class&amp;diff=2323&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/base_class&amp;diff=2324&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/base_class&amp;diff=2324&amp;oldid=prev"/>
				<updated>2010-05-25T10:29:27Z</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;==Add a constructor to Base class==&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;
#include &amp;lt;cstring&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
class Shape { &lt;br /&gt;
  // private &lt;br /&gt;
  double width; &lt;br /&gt;
  double height; &lt;br /&gt;
public: &lt;br /&gt;
  Shape(double w, double h) { &lt;br /&gt;
    width = w; &lt;br /&gt;
    height = h; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  void display() { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Width and height are &amp;quot; &amp;lt;&amp;lt; width &amp;lt;&amp;lt; &amp;quot; and &amp;quot; &amp;lt;&amp;lt; height &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  double getWidth() { return width; } &lt;br /&gt;
  double getHeight() { return height; } &lt;br /&gt;
  void setWidth(double w) { width = w; } &lt;br /&gt;
  void setHeight(double h) { height = h; } &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
class Triangle : public Shape { &lt;br /&gt;
  char style[20]; // now private &lt;br /&gt;
public: &lt;br /&gt;
  Triangle(char *str, double w, double h) : Shape(w, h) { &lt;br /&gt;
    strcpy(style, str); &lt;br /&gt;
  } &lt;br /&gt;
   &lt;br /&gt;
  double area() { &lt;br /&gt;
    return getWidth() * getHeight() / 2; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  void showStyle() { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Triangle is &amp;quot; &amp;lt;&amp;lt; style &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  } &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
int main() { &lt;br /&gt;
  Triangle t1(&amp;quot;isosceles&amp;quot;, 4.0, 4.0); &lt;br /&gt;
  Triangle t2(&amp;quot;right&amp;quot;, 8.0, 12.0); &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Info for t1:\n&amp;quot;; &lt;br /&gt;
  t1.showStyle(); &lt;br /&gt;
  t1.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t1.area() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  t2.showStyle(); &lt;br /&gt;
  t2.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t2.area() &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;Info for t1:&lt;br /&gt;
Triangle is isosceles&lt;br /&gt;
Width and height are 4 and 4&lt;br /&gt;
Area is 8&lt;br /&gt;
Triangle is right&lt;br /&gt;
Width and height are 8 and 12&lt;br /&gt;
Area is 48&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Add a overloaded constructor to base class==&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;
#include &amp;lt;cstring&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
class Shape { &lt;br /&gt;
  // private &lt;br /&gt;
  double width; &lt;br /&gt;
  double height; &lt;br /&gt;
public: &lt;br /&gt;
 &lt;br /&gt;
  Shape() { &lt;br /&gt;
    width = height = 0.0; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  Shape(double w, double h) { &lt;br /&gt;
    width = w; &lt;br /&gt;
    height = h; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  Shape(double x) { &lt;br /&gt;
    width = height = x; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  void display() { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Width and height are &amp;quot; &amp;lt;&amp;lt; width &amp;lt;&amp;lt; &amp;quot; and &amp;quot; &amp;lt;&amp;lt; height &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  double getWidth() { return width; } &lt;br /&gt;
  double getHeight() { return height; } &lt;br /&gt;
  void setWidth(double w) { width = w; } &lt;br /&gt;
  void setHeight(double h) { height = h; } &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
class Triangle : public Shape { &lt;br /&gt;
  char style[20]; // now private &lt;br /&gt;
public: &lt;br /&gt;
 &lt;br /&gt;
  /* A default constructor. This automatically invokes &lt;br /&gt;
     the default constructor of Shape. */ &lt;br /&gt;
  Triangle() { &lt;br /&gt;
    strcpy(style, &amp;quot;unknown&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  Triangle(char *str, double w, double h) : Shape(w, h) { &lt;br /&gt;
    strcpy(style, str); &lt;br /&gt;
  } &lt;br /&gt;
   &lt;br /&gt;
  Triangle(double x) : Shape(x) { &lt;br /&gt;
    strcpy(style, &amp;quot;isosceles&amp;quot;);  &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  double area() { &lt;br /&gt;
    return getWidth() * getHeight() / 2; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  void showStyle() { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Triangle is &amp;quot; &amp;lt;&amp;lt; style &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  } &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
int main() { &lt;br /&gt;
  Triangle t1; &lt;br /&gt;
  Triangle t2(&amp;quot;right&amp;quot;, 8.0, 12.0); &lt;br /&gt;
  Triangle t3(4.0); &lt;br /&gt;
 &lt;br /&gt;
  t1 = t2; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Info for t1: \n&amp;quot;; &lt;br /&gt;
  t1.showStyle(); &lt;br /&gt;
  t1.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t1.area() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  t2.showStyle(); &lt;br /&gt;
  t2.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t2.area() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  t3.showStyle(); &lt;br /&gt;
  t3.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t3.area() &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;Info for t1:&lt;br /&gt;
Triangle is right&lt;br /&gt;
Width and height are 8 and 12&lt;br /&gt;
Area is 48&lt;br /&gt;
Triangle is right&lt;br /&gt;
Width and height are 8 and 12&lt;br /&gt;
Area is 48&lt;br /&gt;
Triangle is isosceles&lt;br /&gt;
Width and height are 4 and 4&lt;br /&gt;
Area is 8&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Call base&amp;quot;s default constructor automatically==&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;
#include &amp;lt;cstring&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
class Shape { &lt;br /&gt;
  // private &lt;br /&gt;
  double width; &lt;br /&gt;
  double height; &lt;br /&gt;
public: &lt;br /&gt;
 &lt;br /&gt;
  Shape() { &lt;br /&gt;
    width = height = 0.0; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  Shape(double w, double h) { &lt;br /&gt;
    width = w; &lt;br /&gt;
    height = h; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  Shape(double x) { &lt;br /&gt;
    width = height = x; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  void display() { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Width and height are &amp;quot; &amp;lt;&amp;lt; width &amp;lt;&amp;lt; &amp;quot; and &amp;quot; &amp;lt;&amp;lt; height &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  double getWidth() { return width; } &lt;br /&gt;
  double getHeight() { return height; } &lt;br /&gt;
  void setWidth(double w) { width = w; } &lt;br /&gt;
  void setHeight(double h) { height = h; } &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
class Triangle : public Shape { &lt;br /&gt;
  char style[20]; // now private &lt;br /&gt;
public: &lt;br /&gt;
 &lt;br /&gt;
  /* A default constructor. This automatically invokes &lt;br /&gt;
     the default constructor of Shape. */ &lt;br /&gt;
  Triangle() { &lt;br /&gt;
    strcpy(style, &amp;quot;unknown&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  Triangle(char *str, double w, double h) : Shape(w, h) { &lt;br /&gt;
    strcpy(style, str); &lt;br /&gt;
  } &lt;br /&gt;
   &lt;br /&gt;
  Triangle(double x) : Shape(x) { &lt;br /&gt;
    strcpy(style, &amp;quot;isosceles&amp;quot;);  &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  double area() { &lt;br /&gt;
    return getWidth() * getHeight() / 2; &lt;br /&gt;
  } &lt;br /&gt;
 &lt;br /&gt;
  void showStyle() { &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Triangle is &amp;quot; &amp;lt;&amp;lt; style &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  } &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
int main() { &lt;br /&gt;
  Triangle t1; &lt;br /&gt;
  Triangle t2(&amp;quot;right&amp;quot;, 8.0, 12.0); &lt;br /&gt;
  Triangle t3(4.0); &lt;br /&gt;
 &lt;br /&gt;
  t1 = t2; &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Info for t1: \n&amp;quot;; &lt;br /&gt;
  t1.showStyle(); &lt;br /&gt;
  t1.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t1.area() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  t2.showStyle(); &lt;br /&gt;
  t2.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t2.area() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  t3.showStyle(); &lt;br /&gt;
  t3.display(); &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Area is &amp;quot; &amp;lt;&amp;lt; t3.area() &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;Info for t1:&lt;br /&gt;
Triangle is right&lt;br /&gt;
Width and height are 8 and 12&lt;br /&gt;
Area is 48&lt;br /&gt;
Triangle is right&lt;br /&gt;
Width and height are 8 and 12&lt;br /&gt;
Area is 48&lt;br /&gt;
Triangle is isosceles&lt;br /&gt;
Width and height are 4 and 4&lt;br /&gt;
Area is 8&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Call base virtual function explicitly==&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 Base {&lt;br /&gt;
public:&lt;br /&gt;
   virtual void foo( ) {cout &amp;lt;&amp;lt; &amp;quot;Base::foo( )&amp;quot; &amp;lt;&amp;lt; endl;}&lt;br /&gt;
};&lt;br /&gt;
class Derived : public Base {&lt;br /&gt;
public:&lt;br /&gt;
   virtual void foo( ) {cout &amp;lt;&amp;lt; &amp;quot;Derived::foo( )&amp;quot; &amp;lt;&amp;lt; endl;}&lt;br /&gt;
};&lt;br /&gt;
int main( ) {&lt;br /&gt;
   Derived* p = new Derived( );&lt;br /&gt;
   p-&amp;gt;foo( );       // Calls the derived version&lt;br /&gt;
   p-&amp;gt;Base::foo( ); // Calls the base version&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Derived::foo( )&lt;br /&gt;
Base::foo( )&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Pass parameter to two base classes==&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 base1 {&lt;br /&gt;
protected:&lt;br /&gt;
  int i;&lt;br /&gt;
public:&lt;br /&gt;
  base1(int x) { &lt;br /&gt;
     i=x; &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Constructing base1\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
  ~base1() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Destructing base1\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
class base2 {&lt;br /&gt;
protected:&lt;br /&gt;
  int k;&lt;br /&gt;
public:&lt;br /&gt;
  base2(int x) { &lt;br /&gt;
     k=x; &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Constructing base2\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
  ~base2() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Destructing base2\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
class derived: public base1, public base2 {&lt;br /&gt;
  int j;&lt;br /&gt;
public:&lt;br /&gt;
  derived(int x, int y, int z): base1(y), base2(z){ &lt;br /&gt;
     j=x; &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Constructing derived\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
  ~derived() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Destructing derived\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
  void show() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; j &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; k &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  derived ob(3, 4, 5);&lt;br /&gt;
  ob.show();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;Constructing base1&lt;br /&gt;
Constructing base2&lt;br /&gt;
Constructing derived&lt;br /&gt;
4 3 5&lt;br /&gt;
Destructing derived&lt;br /&gt;
Destructing base2&lt;br /&gt;
Destructing base1&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Virtual base classes==&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 base {&lt;br /&gt;
public:&lt;br /&gt;
  int i;&lt;br /&gt;
};&lt;br /&gt;
class derived1 : virtual public base {&lt;br /&gt;
public:&lt;br /&gt;
  int j;&lt;br /&gt;
};&lt;br /&gt;
class derived2 : virtual public base {&lt;br /&gt;
public:&lt;br /&gt;
  int k;&lt;br /&gt;
};&lt;br /&gt;
/* derived3 inherits both derived1 and derived2.&lt;br /&gt;
   This time, there is only one copy of base class. */&lt;br /&gt;
class derived3 : public derived1, public derived2 {&lt;br /&gt;
public:&lt;br /&gt;
  int sum;&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  derived3 ob;&lt;br /&gt;
  ob.i = 10; // now unambiguous&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>