Guest User

Untitled

a guest
Jan 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. ## Concept of single and multi-instance design
  2.  
  3. Below code shows how C implements multi-instance design and how it is done by C++.
  4. Also, the efficiency of C++ is equivalent to C, which can be shown with the compiler
  5. generated code.
  6.  
  7. In code, it is also been shown that templates instead of creating any overhead,
  8. makes the code even faster by compiling only the relevant part.
  9. ```C++
  10. // design_pattern4_day3.cpp : Defines the entry point for the console application.
  11. //
  12.  
  13. #include "stdafx.h"
  14.  
  15.  
  16. using namespace std;
  17.  
  18.  
  19. // Implement Stack data structure in C language
  20.  
  21. // Version - 1 - Single Instance design in C
  22. /*int items[10];
  23. int top = 0;
  24.  
  25. void push(int item)
  26. {
  27. items[top] = item;
  28. top++;
  29. }
  30.  
  31. int pop()
  32. {
  33. top--;
  34. return items[top];
  35. }*/
  36.  
  37. // Version - 2 - Multi Instance design in C
  38.  
  39. /*struct Stack
  40. {
  41. int items[10];
  42. int top = 0;
  43. };
  44.  
  45. void push(Stack *s, int item)
  46. {
  47. s->items[s->top] = item;
  48. s->top++;
  49. }
  50.  
  51. int pop(Stack *s)
  52. {
  53. s->top--;
  54. return s->items[s->top];
  55. }*/
  56.  
  57. // Version - 3 - Multi Instance design in C++
  58.  
  59. struct Stack
  60. {
  61. int items[10];
  62. int top = 0;
  63. // gauranteed initialization
  64. Stack()
  65. {
  66.  
  67. }
  68. // gauranteed de-initialization
  69. ~Stack()
  70. {
  71.  
  72. }
  73.  
  74. //operator overaloding, which is not in C
  75.  
  76. void push(int item) // void push(Stack *this, int item)
  77. {
  78. items[top] = item; // this->items[this->top] = item;
  79. top++; // this->top++;
  80. }
  81.  
  82. int pop()
  83. {
  84. top--;
  85. return items[top];
  86. }
  87. };
  88.  
  89.  
  90. // Templated class are never classes, instead they are used to create new classes out of them
  91. // And once class object is created, only those functions are compiled which are called.
  92. template<class T>
  93. class LinkedList
  94. {
  95. public:
  96. void add()
  97. {
  98. abdhdfff;
  99. }
  100. };
  101.  
  102. // ---------------------------Client--------------------------
  103. int main()
  104. {
  105. // Multi-instance design will require address
  106. /*Stack s1, s2, s3;
  107.  
  108. push(&s1, 100);
  109. push(&s1, 200);
  110. push(&s1, 300);*/
  111. Stack s1, s2, s3;
  112.  
  113. s1.push(100); //push(&s1, 100);
  114. s1.push(200); //push(&s1, 100);
  115. s1.push(300); //push(&s1, 100);
  116.  
  117. LinkedList<int> l1;
  118. //l1.add(5); // If this is uncommented, then only compilation error will happen.
  119. return 0;
  120. }
  121. ```
Add Comment
Please, Sign In to add comment