Advertisement
Guest User

21333

a guest
Sep 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. Today's Agenda:
  2. 1) Overloading normal method
  3. 2) Method chaining
  4. 3) Overloading operators
  5. 4) Brainstorming sesson on project milestone-1
  6. ==============================================
  7. class ComplexNo{
  8. int real, img;
  9. public:
  10. //fill in the blacks so that your main works
  11. ComplexNo(){real=img=0;}
  12. ComplexNo(int x){
  13. real=x;
  14. cout<<"Enter img value: "; cin>>img;
  15. }
  16. ComplexNo(int x, int y){real=x; img=y;}
  17. void showCoplexNo(){
  18. cout<<real<<(img>=0?"+":"")<<img<<"i"<<endl;
  19. }
  20. void add(ComlexNo cn1, ComlexNo cn2, ComlexNo cn3){
  21. real = cn1.real + cn2.real + cn3.real;
  22. img = cn1.img + cn2.img + cn3.img;
  23. }
  24. ComlexNo add(ComplexNo cn2, ComplexNo cn3){
  25. ComplexNo temp;
  26. temp.real = real + cn2.real + cn3.real;
  27. temp.img = img + cn2.img + cn3.img;
  28. return temp;
  29. }
  30. ComlexNo& operator+(ComplexNo c){
  31. ComplexNo temp;
  32. temp.real = real + c.real;
  33. temp.img = img + c.img;
  34. return temp;
  35. }
  36.  
  37. };
  38.  
  39. int main(){
  40. ComplexNo c1, c2(10), c3(23,33), c4, c5, c6,c7;
  41. cout<<"set c1: "<<endl;
  42. cout<<"c1 before setting = "; c1.showComplexNo();
  43. c1.setComplexNo();
  44. c4.add(c1,c2,c3);
  45. //c5.add(c1,c2,c3,c4);
  46. c5 = c1.add(c2,c3);
  47. cout<<"c1 = "; c1.showComplexNo();
  48. cout<<"c2 = "; c2.showComplexNo();
  49. cout<<"c3 = "; c3.showComplexNo();
  50. cout<<"c4 = "; c4.showComplexNo();
  51. c6 = c1 + c2; cout<<"c6 = "; c7.showComplexNo();
  52. c7 = c3+c4+c5; cout<<"c7 = "; c7.showComplexNo();
  53. return 0;
  54. }
  55. ------------------
  56. RUN:
  57. Enter img value: 12
  58. set c1:
  59. c1 before setting = 0+0i
  60. Enter real value: 4
  61. Enter img value: -2
  62. c1 = 4-2i
  63. c2 = 10+12i
  64. c3 = 23+33i
  65. c4 = 37+43i
  66. c5 = 37+43i
  67. -----------------------------------
  68. 1 2 3
  69. x + y + z
  70. 3 + 3
  71.  
  72. c6 = c1 + c2 + c3 + c4 + c5;
  73.  
  74. operator function: special method
  75. format:
  76.  
  77. returnType operator"theOperator"(pType p1,...){
  78.  
  79. }
  80. returnType operator+(pType p1,...){
  81.  
  82. }
  83. c3 = c1+c2; //c3 = c1.operator+(c2);
  84. c4 = c1+c2+c3; // c1.operator+(c2)
  85. temp.operator+(c3);
  86. c4 = c1.operator+(c2).operator+(c3);
  87. cout<< getSum(x,y) + z;
  88. --------------------
  89. c9 = 5 + c2; //operator+(5,c2);
  90.  
  91. ---------------
  92.  
  93. RT operator=(ClassType c){
  94. field1 = c.field1;
  95. field2 = c.field2;
  96. field3 = c.field3;
  97. }
  98. ------------------------------
  99. class Person{
  100. int noOfTaskCompleted;
  101. public:
  102. Person(){noOfTaskCompleted=0;}
  103. int getNoOfTaskCompleted(){ return noOfTaskCompleted;}
  104. //void buySnack(){
  105. Person buySnack(){
  106. cout<<"Snacks are bought.. :-)"<<endl;
  107. noOfTaskCompleted++;
  108. return *this;
  109. }
  110. //void arrangeLivingRomm(){
  111. Person arrangeLivingRomm(){
  112. cout<<"Living room is arranged.. :-("<<endl;
  113. noOfTaskCompleted++;
  114. return *this;
  115. }
  116. void serveSnack(){
  117. cout<<"Snack is served.. !!!!"<<endl;
  118. noOfTaskCompleted++;
  119. }
  120. };
  121.  
  122. //RT callerIsAMethodRepresentingRabbi(){
  123. int main(){
  124. Person roudru;
  125. roudru.buySnack();
  126. roudru.arrangeLivingRomm();
  127. roudru.serveSnack();
  128. //roudru.buySnack().arrangeLivingRoom().serveSnack();
  129. cout<<"No of task completed is: "
  130. <<roudru.getNoOfTaskCompleted()<<endl;
  131. }
  132.  
  133.  
  134.  
  135.  
  136. ----------------------
  137. Student asif;
  138. Student* ptr = &asif;
  139.  
  140. asif.id; //legal
  141. ptr.id; //illegal
  142. ptr->id; //legal
  143. (*ptr).id; //legal
  144.  
  145.  
  146. ==================
  147. Lab exercise:
  148. --------------
  149.  
  150.  
  151.  
  152. class Book{
  153. //field: isbnNo, title, price;
  154. //string *autherPtr; int authorNo;
  155. public:
  156. //write default constructor [2 marks]
  157. //add other necessary member functions...
  158. //declare necessary friend functions...
  159. };
  160.  
  161. //need to overload >> operator to allow chaining
  162. //Should ask info for all the fields from user
  163.  
  164. //need to overload << operator to allow chaining
  165. //Should display all the fields of the object iff
  166. //the default values are replaced with user input
  167.  
  168. int main(){
  169. int n, i; cin>>n;
  170. Book bookArr[n]; //an array of Book objects
  171.  
  172. int noOfAuthors = populateBooks(bookArr, n);
  173. //write this global function
  174. //This function will replace the default values (set by constructor) of
  175. //n Books by using cin>>BookObject format in a loop. Finally it returns
  176. //total number of authors of all books of the array.
  177.  
  178. cout<<”No of authors of all books is: “<<noOfAuthors<<endl;
  179. cout << “Detail of the books are: “ << endl;
  180. for(i=0; i<n ;i++) cout << bookArr[i] << endl;
  181. int index = getBookIndexWithLowestPrice(bookArr, n); [2 marks]
  182. int x; cin>>x;
  183.  
  184. if(bookArr[index]<500.0) bookArr[index] += x;
  185. //bookArr[index] is a book and if it’s
  186. //price is <500 taka, then += will increase
  187. //the book’s price by x%. Write necessary
  188. //operator functions ( < , += )
  189.  
  190. Book b1, b2, b3, b4; cin>>b1>>b2>>b3>>b4;
  191.  
  192. //overload ‘+’ operator twice.
  193. //‘+’ means adding price, not books
  194. cout<<( (b1+b2+b3+b4 > 5000.0)
  195. ? "Customer is given 10% discount."
  196. : " customer is given 10% discount."
  197. ) << endl;
  198. return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement