Advertisement
udaykumar1997

pompey

Feb 18th, 2017
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1. --------------------------
  2. Post Title 'OOC Assignment-2 Answers'
  3. Post by user 'Adusumilli-Uday-Kumar'
  4. Post encryped with secure 256-bit MD5 Encryption
  5. Post accesable only VIA link
  6. Post will Expire on '3-OCT-2017'
  7. --------------------------
  8.  
  9. Q1.
  10.  
  11. C is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<. The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object. It's an alternative to the printf() function and is used in combination with the 'cin' and 'cout' operators, which are objects of the istream_withassign and ostream_withassign classes respectively.
  12.  
  13. Eg...
  14. Let Distance be a class with a 2-parameter constructor with elemnts like feet, inches, etc.
  15. We can use the insertion operators as follows operators as shown below,
  16.  
  17. Distance D1(11, 10), D2(5, 11), D3;
  18. cin >> D3;
  19. cout << "First value : " << D3.feet << endl;
  20. cout << "Second value :" << 234 << endl;
  21. cin >> SOME_VARIABLE ;
  22. cout << "Third value :" << SOME_VARIABLE << endl;
  23.  
  24. Q2.
  25.  
  26. Any C/C / Java program starts from main function and all functionss are called directly or indirectly through the main function. Just like variables we also need to declare function before using it in our programs. Function declaration is also called as function prototype.
  27. If function definition is written after main then and only then do we write the prototype declaration in global declaration section.
  28.  
  29. Examples of prototype declaration...
  30.  
  31. Function with two integer arguments and integer as return type is represented using below syntax
  32.  
  33. int sum(int,int);
  34.  
  35. Function with integer argument and integer as return type is represented using below syntax
  36.  
  37. int square(int);
  38.  
  39. Why do we need them ?
  40.  
  41. Program Execution always starts from main , but during lexical analysis (1st Phase of Compiler) token generation starts from left to right and from top to bottom. Thus, If function definition is written after main function, then, during the code generation phase of compiler it may face issue of backward reference and throws an error.
  42. to avoid this we use prototype declaration, which tells compiler that we are going to define this function somewhere in the program. Now, the compiler will have prior information ,during function calling compiler looks forward in the program for the function definition.
  43.  
  44. Q3.
  45.  
  46. Members of a class are private by default and members of struct are public by default. STRUCT is a type of Abstract Data Type that divides up a given chunk of memory according to the structure specification.
  47. Classes are a different type of abstract data type that attempt to ensure information hiding. Internally, there can be a variety of machinations, methods, temp variables, state variables. etc. that are all used to present a consistent API to any code which wishes to use the class.
  48.  
  49. In effect, structs are about data, classes are about code.
  50.  
  51. Q4.
  52.  
  53. Class
  54.  
  55. Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  56. For example : Class of birds, all birds can fly and they all have wings and beaks. So here flying is a behavior and wings and beaks are part of their characteristics.
  57. And there are many different birds in this class with different names but they all posses this behavior and characteristics.
  58. Similarly, class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.
  59.  
  60. Objects
  61.  
  62. Class is mere a blueprint or a template. No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  63. Each object has different data variables. Objects are initialised using special class functions called Constructors. We will study about constructors later.
  64. And whenever the object is out of its scope, another special class member function called Destructor is called, to release the memory reserved by the object. C doesn't have Automatic Garbage Collector like in JAVA, in C Destructor performs this task.
  65. Example :
  66.  
  67. class Abc
  68. {
  69. int x;
  70. void display(){} //empty function
  71. };
  72.  
  73. in main()
  74. {
  75. Abc obj; // Object of class Abc created
  76. }
  77.  
  78. Q5.
  79.  
  80. class cuboid
  81. {
  82. public:
  83. float lenght, breadth, height;
  84. void input(){cout<<"enter the lenght, breadth and height : "; cin >>lenght>>breadth>>height;}
  85. void output(){cout<<"lenght is "<<lenght<<"\n breadth is "<<breadth<<"\n height is"<<height<<endl;}
  86. void surface-area(){cout<<"surcafe area is<<(2*(lenght breadth height));}
  87. }
  88.  
  89. Q6.
  90.  
  91. Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.
  92. Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.
  93. C classes provides great level of data abstraction. They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.e., state without actually knowing how class has been implemented internally.
  94.  
  95. For example, your program can make a call to the sort() function without knowing what algorithm the function actually uses to sort the given values. In fact, the underlying implementation of the sorting functionality could change between releases of the library, and as long as the interface stays the same, your function call will still work.
  96. Example...
  97. class Adder {
  98. public:
  99. // constructor
  100. Adder(int i = 0) {
  101. total = i;
  102. }
  103.  
  104. // interface to outside world
  105. void addNum(int number) {
  106. total = number;
  107. }
  108.  
  109. // interface to outside world
  110. int getTotal() {
  111. return total;
  112. };
  113.  
  114. private:
  115. // hidden data from outside world
  116. int total;
  117. };
  118.  
  119. Q7.
  120.  
  121. A member function can either be defined inside the class itself, or, the func can be prototyoed in side the class and defined outside.
  122. defining a func inside a class makes it an inline func.
  123. Example...
  124. class abc{
  125. int a(){return 232;} // inline function
  126. int b; // function prototype
  127. }
  128.  
  129. int abc::b(){return 9993;} // function definition outside the class
  130.  
  131. Q8.
  132.  
  133. Every object in C has access to its own address through an important pointer called this pointer. The this pointer is an implicit parameter to all member functions. Therefore, inside a member function, this may be used to refer to the invoking object. 'this' pointer is crutial in the process of compilation where, c code is first converted to c lang code before converting it into an object file.
  134. Example...
  135. class Test
  136. {
  137. private:
  138. int x;
  139. public:
  140. void setX (int x)
  141. {
  142. // The 'this' pointer is used to retrieve the object's x
  143. // hidden by the local variable 'x'
  144. this->x = x;
  145. }
  146. void print() { cout << "x = " << x << endl; }
  147. };
  148.  
  149. int main()
  150. {
  151. Test obj;
  152. int x = 20;
  153. obj.setX(x);
  154. obj.print();
  155. return 0;
  156. }
  157.  
  158. Q9.
  159.  
  160. Member functions of a class can be overloaded the same way we overload regular functions, except, we do so within a class.
  161.  
  162. Q10.
  163.  
  164. When a normal function call instruction is encountered, the program stores the memory address of the next instruction, loads the function being called into the memory, copies argument values, jumps to the memory location of the called function, executes the function codes, stores the return value of the function, and then jumps back to the address of the instruction that was saved just before executing the called function. Too much run time overhead.
  165. The inline functions are a C enhancement feature to increase the execution time of a program. Functions can be instructed to compiler to make them inline so that compiler can replace those function definition wherever those are being called. Compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.
  166. NOTE- This is just a suggestion to compiler to make the function inline, if function is big (in term of executable instruction etc) then, compiler can ignore the β€œinline” request and treat the function as normal function.
  167. To make any function as inline, start its definitions with the keyword β€œinline”.
  168. Pros :-
  169. 1. It speeds up your program by avoiding function calling overhead.
  170. 2. It save overhead of variables push/pop on the stack, when function calling happens.
  171. 3. It save overhead of return call from a function.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement