Guest User

Untitled

a guest
Mar 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Foo {
  4. public:
  5. int bar;
  6. Foo(int num): bar(num) {};
  7. };
  8.  
  9. int main(void) {
  10. std::cout << Foo(42).bar << std::endl;
  11. return 0;
  12. }
  13.  
  14. Foo(int num): bar(num)
  15.  
  16. Foo(int num): bar(num) {};
  17.  
  18. Foo(int num)
  19. {
  20. bar = num;
  21. }
  22.  
  23. Cost of Member Initialization = Object Construction
  24. Cost of Member Assignment = Object Construction + Assignment
  25.  
  26. Foo(int num) : bar() {bar = num;}
  27.  
  28. Foo(int num): bar(num){}
  29.  
  30. class MyClass
  31. {
  32. public:
  33. //Reference member, has to be Initialized in Member Initializer List
  34. int &i;
  35. int b;
  36. //Non static const member, must be Initialized in Member Initializer List
  37. const int k;
  38.  
  39. //Constructor’s parameter name b is same as class data member
  40. //Other way is to use this->b to refer to data member
  41. MyClass(int a, int b, int c):i(a),b(b),k(c)
  42. {
  43. //Without Member Initializer
  44. //this->b = b;
  45. }
  46. };
  47.  
  48. class MyClass2:public MyClass
  49. {
  50. public:
  51. int p;
  52. int q;
  53. MyClass2(int x,int y,int z,int l,int m):MyClass(x,y,z),p(l),q(m)
  54. {
  55. }
  56.  
  57. };
  58.  
  59. int main()
  60. {
  61. int x = 10;
  62. int y = 20;
  63. int z = 30;
  64. MyClass obj(x,y,z);
  65.  
  66. int l = 40;
  67. int m = 50;
  68. MyClass2 obj2(x,y,z,l,m);
  69.  
  70. return 0;
  71. }
  72.  
  73. // Example 1
  74. Foo(Bar b)
  75. {
  76. bar = b;
  77. }
  78.  
  79. // Example 2
  80. Foo(Bar b)
  81. : bar(b)
  82. {
  83. }
  84.  
  85. Bar bar(); // default constructor
  86. bar = b; // assignment
  87.  
  88. Bar bar(b) // copy constructor
  89.  
  90. class Foo {
  91. public:
  92. string str;
  93. Foo(string &p)
  94. {
  95. str = p;
  96. };
  97. };
  98.  
  99. class Foo {
  100. public:
  101. string str;
  102. Foo(string &p): str(p) {};
  103. };
  104.  
  105. string();
  106.  
  107. string& operator=( const string& s );
  108.  
  109. string( const string& s );
  110.  
  111. Foo(int num): bar{num} {}
Add Comment
Please, Sign In to add comment