Published on

ICT training in Bannari Amman

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. C++<br />
  2. 2. Explain the different forms of Polymorphism.<br />
  3. 3. There are two types of polymorphism. One is Compile time polymorphism and the other is run time polymorphism. Compile time polymorphism is method overloading. Runtime time polymorphism is done using inheritance.<br />
  4. 4. What are the differences between Constructors and Methods?<br />
  5. 5.
  6. 6. All mammals have some common characteristics such as having hair, being warm-blooded and nurturing their young. Dog is a type of mammal and has some additional characteristics such as slobbering and running in circles. The object oriented concept that allows the dog to derive the common characteristics of the mammal is called _____________<br />Polymorphism<br />Inheritance<br />Encapsulation<br />Prototyping<br />
  7. 7. b. Inheritance <br />Inheritance is the mechanism by which one object acquires the properties of another object. <br />
  8. 8. What are the major benefits of object oriented programming? <br />Ease of maintenance<br />Reduced development time due to reusability<br />Consumes less resources during execution<br />Top-down approach<br />
  9. 9. Ease of maintenance<br />Reduced development time due to reusability<br />
  10. 10. Consider the following inheritance diagram. The Get_vehicle_type function is present in both the super class (Four wheeler) and the derived class (Car). If the Car receives a message for Get_vehicle_type, which method is executed?<br />Four wheeler<br />Member function<br />Get_vehicle_type<br />Car<br />Member function<br />Get_vehicle_type<br />
  11. 11. The method in the Car object is executed. A derived class can override the base class member function by supplying a new version of that function with the same signature. When an object receives a message, it first checks for the availability of the method in this object. If it is not available, then it consults its super-class. The first method found in the hierarchy takes precedence and executed. <br />
  12. 12. Explain in simple terms why an object-oriented language like C++ is different from a Classical language like C.<br />
  13. 13. In a classical language like C, data-structures and their procedures tended to group logically, but it is the programmer's duty to devise and enforce these groupings. In OOP, the language groups procedures with their data type. This produces a decomposition grouped around the types in a program. The programmer does not have to match up the right procedure with the right data-type. Instead, variables know which operations they implement. <br />OOP yields better decomposition and more opportunities for code reuse. These strengths are especially important for writing large programs, packaging up code into libraries for use by others, and programming in teams.<br />
  14. 14. Consider the following statements:<br />Real world objects contain _________ and _________.<br />A software objects’ state is stored in ____________<br />A software object’s behaviour is exposed through _____________<br />Hiding internal data from the outside world and accessing it only through publicly exposed methods is known as data ______________<br />A blue print for a software object is called as a ________ <br />Pick and fill the right words from the following.<br /> (Behaviour, Methods, Encapsulation, State, fields, Class)<br />
  15. 15. State<br />Behaviour<br />Fields<br />Methods<br />Encapsulation<br />Class<br />
  16. 16. Scenario:<br />Consider a Stack data structure (Last-in, First-Out). You might have a program that requires three types of stack. One stack is used for Integer values one stack is used for floating-point values, and one for characters. The algorithm that implements each stack is the same, even though the data being stored differs. In a non-Object Oriented language like C, you would be required to create three different sets of stack routines, with each set using different names. In OOP language you specify a general set of stack routines that all share the same names.<br />Which feature of OOP allows you to implement the above?<br />
  17. 17. Polymorphism based on the principle that of ‘One interface, multiple methods’<br />
  18. 18. Scenario:<br />Real-world objects share two characteristics: They all have state and behavior. Identifying them is a great way to begin thinking in terms of Object oriented programming.<br />Give some examples of state and behavior for the following <br />Animal ( say dog)<br />Vehicle( say bicycle)<br />
  19. 19. Dogs have <br />state (name, color, breed, hungry) <br />behavior (barking, fetching, wagging tail)<br />Bicycles have <br />state (current gear, current pedal, current speed) <br />behavior (changing gear, changing pedal, applying brakes)<br />
  20. 20. Scenario:<br />Imagine a programming environment where all the code is contained in a single continuous block<br />The execution of flow statements are implemented through use of Goto statements so that one can jump to a specified section of code. The source code is notoriously difficult to read and debug. But being a scripting language used for automating batch execution, the program typically runs fast and does its job. <br />What kind of a programming environment are we referring to in the above?<br />Is it object oriented, Structured or Unstructured?<br />
  21. 21. Unstructured<br />
  22. 22. What is the output of the following code?<br />14 <br />13 <br />12 <br />11<br />#include<iostream.h><br />#include<string.h><br />void main()<br />{<br /> cout<<strlen(“Hello, World.n” )<<” n” ;<br />}<br />
  23. 23. a. 14<br />
  24. 24. What is the output of the following code?<br />123 <br />Compile time error <br />321 <br />Run time error<br />#include<iostream.h><br />void main()<br />{<br />/* this is /* an example */ of nested comment */<br />cout<<123<<endl;<br />}<br />
  25. 25. b. Compile time error <br />
  26. 26. What is the output of the following code?<br />1 <br />Compile time error <br />0 <br />Run time error<br />#include<iostream.h><br />void main()<br />{<br />cout<<;<br />}<br />
  27. 27. b. Compile time error <br /> (expected primary-expression before ';' token)<br />
  28. 28. What is the output of the following code?<br />20, 20 <br />20, 21 <br />21, 22 <br />22, 22 <br />#include<iostream.h><br />void main()<br />{<br /> int a = 20;<br /> int &n = a;<br /> n=a++;<br /> a=n++;<br /> cout<<a <<”,”<<n<<endl;<br />}<br />
  29. 29. a. 20, 20<br />
  30. 30. What is the output of the following code?<br />21, 21 <br />20, 21 <br />21, 22 <br />Compile Time Error<br />#include<iostream.h><br />void main()<br />{<br />int a = 20,b=100;<br />int &n = a;<br />n=a++;<br />n = &b;<br />cout<<a <<” ,”<<n<<endl;<br />}<br />
  31. 31. a. Compile Time Error<br /> Explanation: Invalid conversion from int* to int<br />
  32. 32. What is the output of the following code?<br />10<br />False <br />1<br />Error<br />#include<iostream.h><br />void main()<br />{<br />bool a=10;<br /> cout<<a<<endl;<br />}<br />
  33. 33. c. 1<br />
  34. 34. What is the output of the following code?<br />101<br />100<br />None<br />Error : Cannot use main as identifier<br />#include<iostream.h><br />void main()<br />{<br />int main;<br /> main = 100;<br /> cout<<main++<<endl;<br />}<br />
  35. 35. b. 100<br />
  36. 36. What is the output of the following code?<br />0, 0, 0 <br />1, 0, 0 <br />2, 2, 2 <br />3, 2, 2<br />#include<iostream.h><br />void main()<br />{<br />int a=0,x;<br />x = ++a * --a;<br />cout<<++a<< “,“ << a++ << ”,” << x <<endl;<br />}<br />
  37. 37. b. 1, 0, 0<br />
  38. 38. What is the output of the following code?<br />32<br />0<br />Compile Time Error<br />Run Time Error<br />#include<iostream.h><br />void main()<br />{<br /> a=32;<br /> cout<<a<<endl;<br /> int a;<br />}<br />
  39. 39. c. Compile Time Error<br />
  40. 40. What is wrong with the following program?<br />There is nothing wrong in the program. <br />Variable ‘b’ must not be initialized in the loop <br />Variable ‘b’ must declared before do-while the loop<br />The condition for while loop is not valid<br />#include<iostream.h><br />void main()<br />{<br />do<br />{<br />int b=0;<br />cout<<b;<br />b++;<br />}while(b!=10);<br />}<br />
  41. 41. c. Variable ‘b’ must declared before do-while the loop<br />
  42. 42. What is the output of the following program?<br />14, 14 <br />15, 14<br />14, 15<br />15, 15<br />#include<iostream.h><br />void main()<br />{<br />char p[]="This is a test";<br />cout<<sizeof(p)<<","<<strlen(p);<br />}<br />
  43. 43. b. 15, 14<br />
  44. 44. What is wrong with the following program?<br />Array ‘a’ is not initialized properly <br />There is no problem<br />Redeclaration of variable ‘i’ <br />There is a run time error<br />#include<iostream.h><br />void main()<br />{<br />int a[5] = {0};<br />for(int i=0;i<2;i++)<br />a[i]=i;<br />for(int i=0;i<5;i++)<br />cout<<a[i]<<endl;<br />}<br />
  45. 45. b. There is no problem<br />
  46. 46. What is the output of the following program?<br />100, 2,3,22,400 <br />0,0,0,0,0 <br />Error <br />400,22,3,2,100<br />#include<iostream.h><br />void main()<br />{<br />int a[5] = {100,2,3,22,400};<br />int b[5];<br />b=a;<br />for(int i=0;i<5;i++)<br />cout<<b[i]<<endl;<br />}<br />
  47. 47. c. Error <br />C++ forbids assignment of arrays<br />
  48. 48. What is the output of the following program?<br />No output <br />1 2 3 4 5 <br />1 2 3 0 0 <br />There is a run time error<br />#include<iostream.h><br />void main()<br />{<br />int a[5] = {1,2,3};<br />for(int i=0;i<5;i++)<br />cout<<a[i]<<endl;<br />}<br />
  49. 49. c. 1 2 3 0 0 <br />
  50. 50. What is the output of the following program?<br />51, 42, 33, 24, 15, <br />40, 30, 20, 10, 00, <br />41, 32, 23, 14, 05, <br />00, 10, 20, 30, 40,<br />#include<iostream.h><br />void main()<br />{<br />int i=5,j=0;<br />while(i-- || j++)<br />{<br /> cout<<i<<""<<j<<” ,” ;<br />}<br />}<br />
  51. 51. b. 40, 30, 20, 10, 00, <br />
  52. 52. What is the output of the following program?<br />Error <br />1 0<br />0 1<br />0 0 <br />#include<iostream.h><br />void main()<br />{<br />int a;<br />bool b;<br />a = 12 > 100;<br />b = 12 >= 100;<br />cout<<a<<" "<<b<<endl;<br />}<br />
  53. 53. d. 0 0<br />
  54. 54. What is the output of the following program?<br />Error <br />100, 200, 300, 100, <br />300, 200, 100, 0, <br />300, 200, 100, 1, <br />#include<iostream.h><br />void main()<br />{<br />int a = 100;<br />{<br /> int a = 200;<br /> { <br /> int a = 300<br /> cout<<a<<",";<br /> }<br />cout<<a<<",";<br />}<br />cout<<a<<",";<br />cout<<::a<<",";<br />}<br />
  55. 55. d. 300, 200, 100, 1, <br />
  56. 56. What is the output of the following program?<br />Error <br />1000<br />100<br />0<br />#include<iostream.h><br />void main()<br />{<br />int x=10;<br />(x<0)?(int a =100):(int a =1000);<br />cout<<a;<br />}<br />
  57. 57. a. Error<br />
  58. 58. What is the output of the following program?<br />0<br />1<br />Compile Time Error<br />Run Time Error<br />#include<iostream.h><br />void main()<br />{<br />int a = 0;<br />cout<<(a = 10/a);<br />}<br />
  59. 59. d. Run Time Error<br />
  60. 60. What is the output of the following program?<br />1 2 3 4 5 <br />2 4 6 8 10 <br />Compile Time Error<br />Run Time Error<br />#include<iostream.h><br />void main()<br />{<br />int x=0;<br />while(x++<5)<br />{<br />static x;<br />x+=2;<br />cout<<x<<" ";<br />}<br />}<br />
  61. 61. c. Compile Time Error<br /> C++ forbids declaration of `x' with no type<br />
  62. 62. What is the output of the following program?<br />Both the strings are same <br />Both the strings are not same<br />Compile Time Error<br />Run Time Error<br />#include<iostream.h><br />void main()<br />{<br />char str1[]=” India” , str2[]=” India”;<br />if(str1==str2)<br />cout<<”Both the strings are same”;<br />else<br />cout<<”Both the strings are not same”;<br />}<br />
  63. 63. b. Both the strings are not same<br />
  64. 64. What is the output of the following code if user enters “This is a test”? <br />This is a test <br />This is a <br />This<br />Error<br />#include<iostream.h><br />#include<string.h><br />void main()<br />{<br />char str[8];<br />cin>>str;<br />cout<<str;<br />}<br />
  65. 65. c. This<br />
  66. 66. What is the output of the following code? <br />10 20 <br />10 10<br />20 20<br />20 10<br />#include<iostream.h><br />void main()<br />{<br />int arr[] = {10,20,30,40,50};<br />int *ptr = arr;<br />cout<< *ptr++<<" "<<*ptr;<br />}<br />
  67. 67. a. 10 20 <br />
  68. 68. What is the output of the following code? <br />6 <br />3<br />Compile Time Error<br />0<br />#include<iostream.h><br />void main()<br />{<br />int arr[] = {10,20,30,40,50};<br />int x,*ptr1 = arr, *ptr2=&arr[3];<br />x = ptr2 - ptr1;<br />cout<<x;<br />}<br />
  69. 69. b. 3<br />
  70. 70. What is the output of the following code? <br />55 33 <br />33 55 <br />11 55 <br />11 33 <br />#include<iostream.h><br />void main()<br />{<br />int arr[][3]={0,11,22,33,44,55};<br />int *a = &arr[0][0];<br />cout<<arr[1][2]<<" "<<*(a+3);<br />}<br />
  71. 71. a. 55 33 <br />
  72. 72. What is the output of the following code? <br />10<br />3<br />0<br />11<br />#include<iostream.h><br />void main()<br />{<br />int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};<br />cout<<(*(*(*arr+1)+2)+0)+7;<br />}<br />
  73. 73. a. 10<br />
  74. 74. What is the output of the following code? <br />16<br />7<br />11<br />10<br />#include<iostream.h><br />void main()<br />{<br />int arr[2][3][2]={{{2,4},{7,8},{3,4},}, {{2,2},{2,3},{3,4}, }};<br />cout<<**(*arr+1)+2+7;<br />}<br />
  75. 75. a. 16<br />
  76. 76. What is the namespace? <br />
  77. 77. Namespaces allow to group entities like classes, objects and functions under a name. This way the global scope can be divided in "sub-scopes", each one with its own name<br />The format of namespaces is: <br />namespace identifier{ entities}<br />
  78. 78. What is the Basic nature of "cin" and "cout" and what concept or principle we are using on those two?<br />
  79. 79. Basically "cin and cout" are INSTANCES of istream and ostream classes respectively, and the concept which is used on cin and cout is operator overloading. Extraction and Insertion operators are overloaded for input and output operations. <br />
  80. 80. What is scope resolution operator?<br />
  81. 81. The scope resolutions operator is also used to find the value of a variable out of the scope of the variable.<br />Example: <br />::i refers to the value just before the scope (i.e. 10)<br />int i=10;main(){ int i=5; cout<<::i; cout<<i;}<br />
  82. 82. Can main() be overridden? <br />
  83. 83. In any application, there can be only one main function. In c++, main is not a member of any class. There is no chance of overriding.<br />
  84. 84. In what way macros are different from template?<br />
  85. 85. In case of macros, there is no way for the compiler to verify that the macro parameters are of compatible types. The macro is expanded without any special type checking.<br />If macro parameter has a post incremented variable (like c++), the increment is performed two times.<br />Because macros are expanded by the preprocessor, compiler error messages will refer to the expanded macro, rather than the macro definition itself. Also, the macro will show up in expanded form during debugging. <br />Example: Macro:<br />#define min(i, j) (i < j ? i : j)<br />template:template<class T> T min (T i, T j) {  return i < j ? i : j;}<br />
  86. 86. The design of classes in a way that hides the details of implementation from the user is known as:<br />Encapsulation <br />Information Hiding <br />Data abstraction <br />Inheritance<br />
  87. 87. a. Encapsulation<br />
  88. 88. Which of the following keywords do you think can be used when declaring static members in a class? Encapsulation <br /> (i) Public (ii) Private (iii) Protected<br />i, ii and iii <br />i and ii<br />Only i <br />i and iii<br />
  89. 89. c. Only i<br />
  90. 90. I want a nonmember function to have access to the private members of a class. The class must declare that function: <br />friend <br />inline<br />static<br />virtual<br />
  91. 91. a. friend <br />
  92. 92. What is a base class? <br />An abstract class that is at the top of the inheritance hierarchy<br />A class with a pure virtual function in it<br />A class that inherits from another class<br />A class that is inherited by another class, and thus is included in that class<br />
  93. 93. b. A class with a pure virtual function in it<br />
  94. 94. When do preprocessor directives execute? <br />Before the compiler compiles the program <br />After the compiler compiles the program <br />At the same time as the compiler compiles the program <br />Never executes<br />
  95. 95. a. Before the compiler compiles the program <br />
  96. 96. An address is a ________ while a pointer is a _________. <br />Variable, Location <br />Variable, Constant<br />Constant, Variable <br />Constant, Location<br />
  97. 97. a. Variable, Location <br />
  98. 98. What is the difference between declaration and definition?<br />
  99. 99. The definition is the one that actually allocates space, and provides an initialization value, if any. <br />There can be many declarations, but there must be exactly one definition. <br />A definition tells the compiler to set aside storage for the variable. <br />A declaration makes the variable known to parts of these program that may wish to use it. <br />A variable might be defined and declared in the same statement.<br />
  100. 100. What is Object?<br />
  101. 101. An Object is an instance of a class.<br />Example:<br />Ferrari and maruthi are objects of class car. <br />An Object is basic runtime entities of its class<br />
  102. 102. We can overload assignment operator as a normal function. But we cannot overload assignment operator as friend function why?<br />
  103. 103. Because, Friend functions do not have a "this" pointer<br />
  104. 104. What is an inline function?<br />
  105. 105. Inline functions are like macros in c language. The functions are expanded in the line where it is invoked at the time of compilation. These functions should not include any loops or static members inside it. And also it should not be a recursive one. Inline functions should return a value.<br />Keyword ‘inline’ should be included in the function definition.<br />inline int add(int a,int b){return a+b;}void main(){int x=5;int y=10;cout<<?The sum is : ?<<add(x,y);}<br />
  106. 106. What is the difference between deep copy and shallow copy?<br />
  107. 107. A Shallow copy of an object copies all of the member field values. If there are fields that point to dynamically allocated memory, Shallow copy copies the pointer but the memory it points to will not be copied. The field in both the original object and the copy will then point to the same dynamically allocated memory.<br />Whereas Deep copy copies all the fields and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, one needs to overwrite the copy constructor and assignment operator<br />
  108. 108. Can we make any program in c++ without using any header file?<br />
  109. 109. No<br />
  110. 110. Can we have more than one constructor in a class?<br />
  111. 111. Yes<br />
  112. 112. What is public, private and protected?<br />
  113. 113. Public, protected and private are three access specifiers in C++.<br />Public data members and member functions are accessible outside the class.<br />Protected data members and member functions are only available to derived classes.<br />Private data members and member functions cannot be accessed outside the class. <br />
  114. 114. What are pointers?<br />
  115. 115. A pointer is an address location of another variable. It is a value that designates the address or memory location of some other value (usually value of a variable). <br />The value of a variable can be accessed by a pointer which points to that variable. <br />To do so, the reference operator (&) is pre-appended to the variable that holds the value. <br />A pointer can hold any data type, including functions.<br />
  116. 116. What is NULL pointer?<br />
  117. 117. A pointer that points to a no valid location is known as null pointer. Null pointers are useful to indicate special cases. <br />Example:<br />No next node pointer in case of a linked list, which is an indication of errors that pointer returned from functions.<br />
  118. 118. What is reference variable in C++?<br />
  119. 119. A reference variable is just like pointer with few differences. <br />It is declared using & operator. <br />A reference variable must always be initialized. <br />The reference variable once defined to refer to a variable cannot be changed to point to other variable. <br />You cannot create an array of references the way it is possible with pointer.<br />
  120. 120. What is the output of the following program?<br />#include <iostream.h><br />class myclass<br />{<br />int val;<br />Public:<br />myclass(int I)<br />{ val = I; cout<<”constructing n”;}<br />~myclass( ) { cout<<"destructingn";}<br />int getval( ) { return val;}<br />};<br />void display(myclass ob)<br />{<br />cout<<ob.getval()<<'n';<br />}<br />main()<br />{<br />myclass a(10);<br />display(a);<br />return 0;<br />}<br />
  121. 121. constructing<br />10<br />destructing<br />When a copy of an object is created to be used as an argument to a function, the “constructor" function is not called, but "copy constructor " is called. However, when a copy is destroyed (usually by going out of scope when the function returns), the destructor function is called.<br />
  122. 122. What is the output of the following program?<br />#include "iostream.h"<br />class base {<br />public:<br />base(){ cout << "constructing base n";}<br />~base(){ cout << "destructing base n";}<br />};<br />class derived: public base{<br />public:<br />derived() {cout<<"constructing derived n";}<br />~derived() {cout<<"destructing derived n";}<br />};<br />main()<br />{<br />derived ob;<br />return 0;<br />}<br />
  123. 123. constructing base <br />constructing derived <br />destructing derived <br />destructing base <br />Constructors are called in the order of derivation and destructors are called in the reverse order.<br />
  124. 124. Observe the following piece of code and reduce the amount of duplicate code.<br />if(book::title=new char[256] == 0)<br />{<br />cerr <<"Error allocating memoryn";<br /> exit(0);<br />}<br />if(book::author=new char[64] ==0)<br />{<br />cerr<<"Error allocating memoryn";<br /> exit(0);<br />}<br />if(book::publisher=new char[128] ==0)<br />{<br />cerr<<"Error allocating memoryn";<br /> exit(0);<br />} <br />
  125. 125. book::title=new char[256] ;<br />book::author=new char[64] ;<br />book::publisher=new char[128];<br />if((book::title&&book::author&&book::publisher) ==0)<br />{<br />cerr<<"Error allocating memoryn";<br />exit(0);<br />}<br />The following code snippet is appearing 3 times in the original piece of code.<br />We can have it just one time. Remaining two times it is redundant<br />cerr<<"Error allocating memoryn";<br />exit(0);<br />
  126. 126. When the interface exposed by C++ component is not compatible to the client, what needs to be done?<br />
  127. 127. Client has to use Adapter design pattern to make interface compatible. Adapter publicly implements the interface to be used by the client<br />
  128. 128. What is the output of following program? Why?<br />#include <iostream.h> <br />class ConstFromConst<br />{ <br />private : <br /> int num1; <br /> int num2; <br />public: <br />ConstFromConst(){num1=100;num2=101;ConstFromConst(109);}; <br />ConstFromConst(int i1){num1 = 109;}; <br />ConstFromConst(int i1,int i2){}; <br /> void print() <br /> { <br /> cout << num1<<endl; <br /> cout << num2<<endl; <br /> } <br /> virtual ~ConstFromConst(){}; <br />}; <br />void main() <br />{ <br />//your code <br />ConstFromConst a; <br />a.print(); <br />} <br />
  129. 129. 100<br />101<br />num1 and num2 will be 100 and 101 and not 109 and 101. <br />So when you call another constructor, another object is created and then destroyed with in the scope of the constructor that calls another constructor.<br />So calling another constructor is pretty useless in the body of a constructor.<br />
  130. 130. Instead of char* what type can be used in pure C++ to describe the String?<br />
  131. 131. basic_string is the class defined for this purpose, string is typedefed as basic_string<char><br />
  132. 132. What happens if there is no catch block suitable to the throw type?<br />
  133. 133. Application crashes due to unhandled exception<br />
  134. 134. Is the program given below a valid one?<br />#include<iostream.h><br />int func(int i);<br />double func(int i);<br />void main(void)<br />{<br />cout<<func(10);<br />cout<<func(10.201);<br />}<br />int func(int i)<br />{<br /> return i;<br />}<br />double func(int i)<br />{<br /> return i;<br />} <br />
  135. 135. No, because you cannot overload functions if they differ only in terms of the data type they return. They should vary with arguments they take in, that are called as function overloading.<br />
  136. 136. Determine the output for the following program.<br />#include <iostream><br />using namespace std;<br />class arith {<br />public:<br />void calc(int num1)<br />{<br /> cout<<"Square of a given number: " <<num1*num1 <<endl;<br />}<br />void calc(int num1, int num2 )<br />{<br /> cout<<"Product of two whole numbers: " <<num1*num2 <<endl;<br />}<br />};<br />int main() //begin of main function<br />{<br />arith a;<br />a.calc(5);<br />a.calc(6,7);<br />}<br />
  137. 137. Square of a given number: 25<br />Product of two whole numbers: 42<br />
  138. 138. Determine the output for the following program.<br />#include <iostream><br />using namespace std;<br />class arith {<br />public:<br />void calc(int num1)<br />{<br /> cout<<"Square of a given number: " <<num1*num1 <<endl;<br />}<br />void calc(int num1, int num2 )<br />{<br /> cout<<"Product of two whole numbers: " <<num1*num2 <<endl;<br />}<br />};<br />int main() //begin of main function<br />{<br />arith a;<br />a.calc(5);<br />a.calc(6,7);<br />}<br />
  139. 139. Square of a given number: 25<br />Product of two whole numbers: 42<br />
  140. 140. How does the C++ compiler process inline functions included in a header file?<br />
  141. 141. The compiler, when it encounters a definition of an inline function as a header, puts the function type (the signature combined with the return value) and the function body in its symbol table. When the function is used somewhere in the program, the compiler not only checks to ensure that the call is correct but the return value is also used correctly. It then substitutes the function body for the function call, thereby eliminating the overhead. An inline function in a header file has a special status, since one must include the header file containing the function and its definition in every file where the function is used, and doesn’t end up with multiple definition errors (however, the definition must be identical in all places where the inline function is included).<br />
  142. 142. What is a template?<br />
  143. 143. Templates allow creating generic functions that admit any data type as parameters and return values without having to overload the function with all the possible data types.<br />Its prototype is any of the two following ones:<br />The only difference between both prototypes is the use of keyword class or typename, its use is indistinct since both expressions have exactly the same meaning and behave exactly the same way<br />template <class indetifier> function_declaration;<br />template <typename indetifier> function_declaration;<br />
  144. 144. We want to swap int, char , float.<br />We need not define 3 functions; instead we can implement it by using templete<br />function definition<br />template <class T><br />T fn_name(argument name)<br />