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%2FClass%2FConstructor</id>
		<title>C++/Class/Constructor - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B%2FClass%2FConstructor"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B/Class/Constructor&amp;action=history"/>
		<updated>2026-04-18T14:36:42Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B/Class/Constructor&amp;diff=1346&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/Class/Constructor&amp;diff=1346&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:06Z</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/Class/Constructor&amp;diff=1347&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/Class/Constructor&amp;diff=1347&amp;oldid=prev"/>
				<updated>2010-05-25T10:25:38Z</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;==Call constructor from base class==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class IntPair {&lt;br /&gt;
public:&lt;br /&gt;
  int a;&lt;br /&gt;
  int b;&lt;br /&gt;
  IntPair(int i, int j) : a(i), b(j) { }&lt;br /&gt;
};&lt;br /&gt;
class MyClass {&lt;br /&gt;
  IntPair nums;&lt;br /&gt;
public:&lt;br /&gt;
  // Initialize nums object using initialization syntax.&lt;br /&gt;
  MyClass(int x, int y) : nums(x,y) { }&lt;br /&gt;
  int getNumA() { &lt;br /&gt;
     return nums.a; &lt;br /&gt;
  }&lt;br /&gt;
  int getNumB() { &lt;br /&gt;
     return nums.b; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  MyClass object1(7, 9), object2(5, 2);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Values in object1 are &amp;quot; &amp;lt;&amp;lt; object1.getNumB() &amp;lt;&amp;lt;&lt;br /&gt;
          &amp;quot; and &amp;quot; &amp;lt;&amp;lt; object1.getNumA() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Values in object2 are &amp;quot; &amp;lt;&amp;lt; object2.getNumB() &amp;lt;&amp;lt;&lt;br /&gt;
          &amp;quot; and &amp;quot; &amp;lt;&amp;lt; object2.getNumA() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Constructing and Destructing sequence for three level inheritance ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class BaseClass {&lt;br /&gt;
public:&lt;br /&gt;
  BaseClass() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Constructing base\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
  ~BaseClass() { &lt;br /&gt;
      cout &amp;lt;&amp;lt; &amp;quot;Destructing base\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
class DerivedClass1 : public BaseClass {&lt;br /&gt;
public:&lt;br /&gt;
  DerivedClass1() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Constructing DerivedClass1\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
  ~DerivedClass1() { &lt;br /&gt;
      cout &amp;lt;&amp;lt; &amp;quot;Destructing DerivedClass1\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
class DerivedClass2: public DerivedClass1 {&lt;br /&gt;
public:&lt;br /&gt;
  DerivedClass2() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Constructing DerivedClass2\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
  ~DerivedClass2() { &lt;br /&gt;
     cout &amp;lt;&amp;lt; &amp;quot;Destructing DerivedClass2\n&amp;quot;; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  DerivedClass2 ob;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Constructor: different parameter type==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdlib&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 x) { &lt;br /&gt;
     a = x; &lt;br /&gt;
  } &lt;br /&gt;
  myclass(char *str) { &lt;br /&gt;
     a = atoi(str); &lt;br /&gt;
  }&lt;br /&gt;
  int geta() { &lt;br /&gt;
     return a; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  myclass object1 = 4;     &lt;br /&gt;
  myclass object2 = &amp;quot;123&amp;quot;; &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object1: &amp;quot; &amp;lt;&amp;lt; object1.geta() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object2: &amp;quot; &amp;lt;&amp;lt; object2.geta() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Constructor with 2 parameters==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&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;
  int h;&lt;br /&gt;
  int i;&lt;br /&gt;
public:&lt;br /&gt;
  MyClass(int j, int k) { &lt;br /&gt;
     h = j; &lt;br /&gt;
     i = k; &lt;br /&gt;
  } &lt;br /&gt;
  int getInt() {&lt;br /&gt;
     return i;&lt;br /&gt;
  }&lt;br /&gt;
  int getHeight() {&lt;br /&gt;
     return h;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  MyClass myObject[3] = {&lt;br /&gt;
    MyClass(1, 2), // initialize&lt;br /&gt;
    MyClass(3, 4),&lt;br /&gt;
    MyClass(5, 6)&lt;br /&gt;
  };&lt;br /&gt;
  int i;&lt;br /&gt;
  for(i=0; i&amp;lt;3; i++) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; myObject[i].getHeight();&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;, &amp;quot;;&lt;br /&gt;
    cout &amp;lt;&amp;lt; myObject[i].getInt() &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;;&lt;br /&gt;
  }&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Constructor with parameter value checking==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;   &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;iomanip&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class Book &lt;br /&gt;
{&lt;br /&gt;
  public: &lt;br /&gt;
    char *title;&lt;br /&gt;
    char *author;&lt;br /&gt;
    float price;&lt;br /&gt;
    Book(char *title, char *author, char *publisher, float price);&lt;br /&gt;
    void show_title(void) { cout &amp;lt;&amp;lt; title &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; };&lt;br /&gt;
    float get_price(void) { return(price); };&lt;br /&gt;
    void show_book(void) &lt;br /&gt;
    { &lt;br /&gt;
      show_title(); &lt;br /&gt;
      show_publisher(); &lt;br /&gt;
    };&lt;br /&gt;
    void assign_publisher(char *name) { strcpy(publisher, name); };&lt;br /&gt;
  private:&lt;br /&gt;
    char *publisher;&lt;br /&gt;
    void show_publisher(void) { cout &amp;lt;&amp;lt; publisher &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; };&lt;br /&gt;
};&lt;br /&gt;
Book::Book(char *title, char *author, char *publisher, float price)&lt;br /&gt;
 {&lt;br /&gt;
   if ((Book::title = new char[256]) == 0)&lt;br /&gt;
     {&lt;br /&gt;
       cerr &amp;lt;&amp;lt; &amp;quot;Error allocating memory\n&amp;quot;;&lt;br /&gt;
       exit(0);&lt;br /&gt;
     }&lt;br /&gt;
   if ((Book::author = new char[64]) == 0)&lt;br /&gt;
     {&lt;br /&gt;
       cerr &amp;lt;&amp;lt; &amp;quot;Error allocating memory\n&amp;quot;;&lt;br /&gt;
       exit(0);&lt;br /&gt;
     }&lt;br /&gt;
   if ((Book::publisher = new char[128]) == 0)&lt;br /&gt;
     {&lt;br /&gt;
       cerr &amp;lt;&amp;lt; &amp;quot;Error allocating memory\n&amp;quot;;&lt;br /&gt;
       exit(0);&lt;br /&gt;
     }&lt;br /&gt;
   strcpy(Book::title, title);&lt;br /&gt;
   strcpy(Book::author, author);&lt;br /&gt;
   strcpy(Book::publisher, publisher);&lt;br /&gt;
   Book::price = price;&lt;br /&gt;
 }&lt;br /&gt;
int main(void)&lt;br /&gt;
 {&lt;br /&gt;
   Book tips(&amp;quot;A&amp;quot;, &amp;quot;B&amp;quot;, &amp;quot;B&amp;quot;,49.95);&lt;br /&gt;
   Book diary(&amp;quot;C&amp;quot;, &amp;quot;D&amp;quot;, &amp;quot;D&amp;quot;, 9.95);&lt;br /&gt;
   tips.show_book();&lt;br /&gt;
   diary.show_book();&lt;br /&gt;
 }&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Define constructor outside a class definition==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;   &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;iomanip&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class Book &lt;br /&gt;
{&lt;br /&gt;
  public: &lt;br /&gt;
    char title[256];&lt;br /&gt;
    char author[64];&lt;br /&gt;
    float price;&lt;br /&gt;
    Book(char *btitle, char *bauthor, char *bpublisher, float bprice);&lt;br /&gt;
    void show_title(void) { cout &amp;lt;&amp;lt; title &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; };&lt;br /&gt;
    float get_price(void) { return(price); };&lt;br /&gt;
    void show_book(void) &lt;br /&gt;
    { &lt;br /&gt;
      show_title();&lt;br /&gt;
      show_publisher();&lt;br /&gt;
    };&lt;br /&gt;
    void assign_publisher(char *name) { strcpy(publisher, name); };&lt;br /&gt;
  private:&lt;br /&gt;
    char publisher[256];&lt;br /&gt;
    void show_publisher(void) { cout &amp;lt;&amp;lt; publisher &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; };&lt;br /&gt;
};&lt;br /&gt;
Book::Book(char *btitle, char *bauthor, char *bpublisher, float bprice)&lt;br /&gt;
{&lt;br /&gt;
   strcpy(title, btitle);&lt;br /&gt;
   strcpy(author, bauthor);&lt;br /&gt;
   strcpy(publisher, bpublisher);&lt;br /&gt;
   price = bprice;&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   Book tips(&amp;quot;A&amp;quot;, &amp;quot;B&amp;quot;, &amp;quot;C&amp;quot;, 49.95);&lt;br /&gt;
   Book diary(&amp;quot;D&amp;quot;, &amp;quot;E&amp;quot;, &amp;quot;F&amp;quot;, 9.95);&lt;br /&gt;
   tips.show_book();&lt;br /&gt;
   diary.show_book();&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==overloading class constructors==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class CRectangle {&lt;br /&gt;
    int width, height;&lt;br /&gt;
  public:&lt;br /&gt;
    CRectangle ();&lt;br /&gt;
    CRectangle (int,int);&lt;br /&gt;
    int area (void) {return (width*height);}&lt;br /&gt;
};&lt;br /&gt;
CRectangle::CRectangle () {&lt;br /&gt;
  width = 5;&lt;br /&gt;
  height = 5;&lt;br /&gt;
}&lt;br /&gt;
CRectangle::CRectangle (int a, int b) {&lt;br /&gt;
  width = a;&lt;br /&gt;
  height = b;&lt;br /&gt;
}&lt;br /&gt;
int main () {&lt;br /&gt;
  CRectangle rect (3,4);&lt;br /&gt;
  CRectangle rectb;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;rect area: &amp;quot; &amp;lt;&amp;lt; rect.area() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;rectb area: &amp;quot; &amp;lt;&amp;lt; rectb.area() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
  &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Parameterized Constructors==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&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;
  int a, b;&lt;br /&gt;
public:&lt;br /&gt;
  myclass(int i, int j) {&lt;br /&gt;
     a=i; &lt;br /&gt;
     b=j;&lt;br /&gt;
  }&lt;br /&gt;
     &lt;br /&gt;
  void show() {&lt;br /&gt;
     cout &amp;lt;&amp;lt; a &amp;lt;&amp;lt; &amp;quot; &amp;quot; &amp;lt;&amp;lt; b;&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  myclass ob(3, 5);&lt;br /&gt;
  ob.show();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==string type constructor==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstring&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdlib&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class StringClass {&lt;br /&gt;
  char *p;&lt;br /&gt;
  int len;&lt;br /&gt;
public:&lt;br /&gt;
  StringClass(char *ptr);&lt;br /&gt;
  ~StringClass();&lt;br /&gt;
  void show();&lt;br /&gt;
};&lt;br /&gt;
StringClass::StringClass(char *ptr)&lt;br /&gt;
{&lt;br /&gt;
  len = strlen(ptr);&lt;br /&gt;
  p = new char [len+1];&lt;br /&gt;
  if(!p) {&lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;Allocation error\n&amp;quot;;&lt;br /&gt;
    exit(1);&lt;br /&gt;
  }&lt;br /&gt;
  strcpy(p, ptr);&lt;br /&gt;
}&lt;br /&gt;
StringClass::~StringClass()&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Freeing p\n&amp;quot;;&lt;br /&gt;
  delete [] p;&lt;br /&gt;
}&lt;br /&gt;
void StringClass::show()&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; p &amp;lt;&amp;lt; &amp;quot; - length: &amp;quot; &amp;lt;&amp;lt; len;&lt;br /&gt;
  cout &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  StringClass stringObject1(&amp;quot;www.java2s.com&amp;quot;), stringObject2(&amp;quot;www.java2s.com&amp;quot;);&lt;br /&gt;
  stringObject1.show();&lt;br /&gt;
  stringObject2.show();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use automatic conversions to assign new values==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;  &lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;cstdlib&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 x) { &lt;br /&gt;
     a = x; &lt;br /&gt;
  } &lt;br /&gt;
  myclass(char *str) { &lt;br /&gt;
     a = atoi(str); &lt;br /&gt;
  }&lt;br /&gt;
  int geta() { &lt;br /&gt;
     return a; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
 &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  myclass object1 = 4;     // converts to myclass(4)&lt;br /&gt;
  myclass object2 = &amp;quot;123&amp;quot;; // converts to myclass(&amp;quot;123&amp;quot;);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object1: &amp;quot; &amp;lt;&amp;lt; object1.geta() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object2: &amp;quot; &amp;lt;&amp;lt; object2.geta() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  &lt;br /&gt;
  object1 = &amp;quot;1776&amp;quot;;        // converts into object1 = myclass(&amp;quot;1776&amp;quot;);&lt;br /&gt;
  object2 = 2001;          // converts into object2 = myclass(2001);&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object1: &amp;quot; &amp;lt;&amp;lt; object1.geta() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;object2: &amp;quot; &amp;lt;&amp;lt; object2.geta() &amp;lt;&amp;lt; endl;&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use constructor to init member variables==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&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;
  int i, j;&lt;br /&gt;
public:&lt;br /&gt;
  myclass(int x, int y) { &lt;br /&gt;
    i = x; &lt;br /&gt;
    j = y; &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; &lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  myclass count(2, 3);&lt;br /&gt;
  count.show();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Use Double value as the constructor parameter==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&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;
  double l, w, h;&lt;br /&gt;
  double volume;&lt;br /&gt;
public:&lt;br /&gt;
  MyClass(double a, double b, double c);&lt;br /&gt;
  void vol();&lt;br /&gt;
};&lt;br /&gt;
MyClass::MyClass(double a, double b, double c)&lt;br /&gt;
{&lt;br /&gt;
  l = a;&lt;br /&gt;
  w = b;&lt;br /&gt;
  h = c;&lt;br /&gt;
  volume = l * w * h;&lt;br /&gt;
}&lt;br /&gt;
void MyClass::vol()&lt;br /&gt;
{&lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;Volume is: &amp;quot; &amp;lt;&amp;lt; volume &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
  MyClass x(2.2, 3.97, 8.09), y(1.0, 2.0, 3.0);&lt;br /&gt;
  x.vol();&lt;br /&gt;
  y.vol();&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
           &lt;br /&gt;
         &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>