raju02

raihan.cpp

Feb 18th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 KB | None | 0 0
  1. /// For virtual Function
  2. /// Code number 1:
  3. #include <iostream>
  4. using namespace std;
  5. class A{
  6. int a;
  7. public:
  8. A(){a = 0;}
  9. virtual void dis(){cout<<a<<endl;}
  10.  
  11.  
  12. };
  13. class B: public A{
  14. int b;
  15. public:
  16. B(){b = 2;}
  17. void dis(){cout<< b << endl;}
  18.  
  19. };
  20. int main()
  21. {
  22. A *p;
  23. B x;
  24.  
  25. p = &x;
  26.  
  27. p->dis();
  28.  
  29.  
  30. }
  31. /// Code number 2
  32. /// Pure virtual Function
  33. #include <iostream>
  34. using namespace std;
  35. class A{
  36. int a;
  37. public:
  38. A(){a = 0;}
  39. virtual void dis()= 0;
  40. };
  41. class B: public A{
  42. int b;
  43. public:
  44. B(){b = 2;}
  45. void dis(){cout<< b << endl;}
  46.  
  47. };
  48. int main()
  49. {
  50. A *p;
  51. B x;
  52. p = &x;
  53. p->dis();
  54. }
  55. ///Topix File read
  56. ///Code Number
  57. #include <iostream>
  58. #include <fstream>
  59. using namespace std;
  60. int main()
  61. {
  62. ifstream in("test.txt");
  63.  
  64. char c;
  65.  
  66. while(1)
  67. {
  68. in >> c;
  69.  
  70. if(in.eof());
  71.  
  72. break;
  73.  
  74. cout<<c<<endl;
  75. }
  76.  
  77. in.close();
  78. }
  79. /// Create a file and read
  80. /// Constructor for inheritance
  81. #include <iostream>
  82. using namespace std;
  83. class A
  84. {
  85. int x;
  86.  
  87. public:
  88. A(int i){x = i;}
  89.  
  90. void show() {cout<< x << endl;}
  91. };
  92.  
  93. class B : public A{
  94. int y;
  95. public:
  96. B(int a , int l): A(l){y = a;}
  97. void dis(){cout<< y << endl;}
  98.  
  99. };
  100. int main()
  101. {
  102. A p(3);
  103. p.show();
  104.  
  105. }
  106. /// Generic Function (Template)
  107. Code 1
  108. #include <iostream>
  109. using namespace std;
  110. template <class t>
  111. t max1(t a , t b)
  112. {
  113. if(a > b)
  114. return a;
  115. else
  116. return b;
  117. }
  118. int main()
  119. {
  120. cout<< max1(1, 4) << endl;
  121. cout<< max1('c' , 'b')<< endl;
  122. cout<< max1(2.5 , 3.1)<< endl;
  123. }
  124. /// Generic class (template)
  125. #include <iostream>
  126. using namespace std;
  127.  
  128. template <class t>
  129. class A
  130. {
  131. t a;
  132. public:
  133. A(){a = 0;}
  134. void set(t k)
  135. {
  136. a = k;
  137. }
  138. void dis(){cout<< a << endl;}
  139. };
  140.  
  141. int main()
  142. {
  143. A <int> ob;
  144. ob.set(1);
  145. ob.dis();
  146. A <char> ob1;
  147. ob1.set('a');
  148. ob1.dis();
  149. }
  150. /// execption Handiling (Try , catch , Throw)
  151. #include <iostream>
  152. using namespace std;
  153. int main()
  154. {
  155. try{
  156. cout<< "try"<< endl;
  157. throw 1;
  158. }
  159. catch(int i)
  160. {
  161. cout<< "Catch " << i << endl;
  162. }
  163. }
  164. /// Virtual overrid (এই কোড টা ২ বার আসছে ) ট্রাইএঙ্গেল , রেক্ট্যেংল দিয়ে
  165. #include <iostream>
  166. using namespace std;
  167. class figure
  168. {
  169. public:
  170. double a , b;
  171. figure(double x , double y){a = x , b = y;}
  172. virtual double dis() = 0;
  173.  
  174. };
  175. class rectangle: public figure
  176. {
  177. public:
  178. rectangle(double x, double y): figure(x , y){};
  179. double dis(){return a * b;}
  180. };
  181. class triangle: public figure
  182. {
  183. public:
  184. triangle(double x , double y): figure(x , y){};
  185. double dis(){return a * b;}
  186. };
  187. int main()
  188. {
  189. rectangle ob(2, 3);
  190. figure *a;
  191. a = &ob;
  192. cout<<a -> dis() << endl;
  193. triangle ob2(4 ,6);
  194. a = &ob2;
  195.  
  196. cout<< a -> dis() << endl;
  197. }
Add Comment
Please, Sign In to add comment