Advertisement
udaykumar1997

kkks

Feb 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.83 KB | None | 0 0
  1. --------------------------
  2. Post Title 'OOC Assignment-3 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.  
  10.  
  11. Q1.
  12.  
  13. Once a const class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed. This includes both changing member variables directly (if they are public), or calling member functions that set the value of member variables.
  14. Sometimes there is requirement to modify one or more data members of class / struct through const function even though you don’t want the function to update other members of class / struct. This task can be easily performed by using mutable keyword.
  15. Eg...
  16. class hello{
  17. int a,b;
  18. mute int bc;
  19. const void ppp(){
  20. bc= a*b; // valid stmt
  21. a=b; // invalid stmt
  22. }// ppp ends
  23.  
  24. }; // class ends
  25.  
  26.  
  27.  
  28. Q2.
  29.  
  30. In certain cases, we might need to access the private members of a class from a certain function outside the class. In such cases instead of declaring those class members as public we can just declare that certain non Member function as a friend function. Once that is done, we will be able to access the private members from that non Member function.
  31. C offers us the facility to declare entire classes as friends, which means that all the number functions of that class will be able to access the Private data elements of the class in which we declare that as friend.
  32.  
  33. Functions that are declared or defined inside a class are called member functions and other standalone functions are called non member functions.
  34. Marember functions that are defined as friends are called friend member functions, these are usually member of other classes which are declared as friends. Non member friend functions are those standalone functions which do not belong to any class and are yet declared as friend functions.
  35. Eg...
  36.  
  37. class C;
  38. class A {
  39. private:
  40. int numA;
  41. public:
  42. A(): numA(12) { }
  43. friend int add(A, B); // friend function declaration
  44. friend class C; // friend class declaration
  45. };
  46.  
  47.  
  48.  
  49. Q3.
  50.  
  51. A friend class members has access to the Private data elements of the class. It is declared using the keyword 'friend'. Friend functions and classes can be used as bridges between two classes.
  52. Eg...
  53.  
  54. class B;
  55. class C;
  56. class A {
  57. private:
  58. int numA;
  59. public:
  60. A(): numA(12) { }
  61. // friend function declaration
  62. friend int add(A, B);
  63. friend class C;
  64. };
  65.  
  66. class B {
  67. private:
  68. int numB;
  69. public:
  70. B(): numB(1) { }
  71. // friend function declaration
  72. friend int add(A , B);
  73. friend class C;
  74. };
  75.  
  76. // Function add() is the friend function of classes A and B
  77. // that accesses the member variables numA and numB
  78. int add(A objectA, B objectB)
  79. {
  80. return (objectA.numA objectB.numB);
  81. }
  82.  
  83. class C {
  84.  
  85. int Multiply(A objectA, B objectB)
  86. {
  87. return (objectA.numA * objectB.numB);
  88. }
  89.  
  90. int Divide(A objectA, B objectB)
  91. {
  92. return (objectA.numA / objectB.numB);
  93. }
  94.  
  95. };
  96.  
  97.  
  98. In the above example, we can see that class C acts as a bridge betweeen the classes A and B. It's member functions can access and modify the private variables of those classes which we cannot normally access without declaring them as friend.
  99.  
  100.  
  101.  
  102. Q4.
  103.  
  104. Static members can be defined with the keyword 'static'. When we declare a member of a class as static it means that no matter how many objects of that class we declare, there will be only one copy of the static member. Static members are shared by all objects of the class.
  105. All static data is initialized to zero when the first object is created, if no other initialization is present. They can also be initialized outside the class using the scope resolution operator :: to identify which class it belongs to.
  106.  
  107. By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::
  108.  
  109. A static member function can only access static data member, other static member functions and any other functions from outside the class.
  110. Eg...
  111. class Box {
  112. private:
  113. double length; // Length of a box
  114. double breadth; // Breadth of a box
  115. double height; // Height of a box
  116. public:
  117. static int objectCount; // static variable
  118. // Constructor definition
  119. Box() {
  120. cout <<"Constructor called." << endl;
  121. length = 12;
  122. breadth = 12;
  123. height = 12;
  124. // Increase every time object is created
  125. objectCount ;
  126. }
  127.  
  128. double Volume() {
  129. return length * breadth * height;
  130. }
  131.  
  132. static int getCount() { // static function
  133. return objectCount;
  134. }
  135.  
  136. };
  137.  
  138. // Initialize static member of class Box
  139. int Box::objectCount = 0;
  140.  
  141.  
  142.  
  143. Q5.
  144.  
  145. Consider a situation where you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
  146.  
  147. A namespace is designed to overcome this difficulty and is used as additional information to differentiate similar functions, classes, variables etc. with the same name available in different libraries. Using namespace, you can define the context in which names are defined. In essence, a namespace defines a scope.
  148.  
  149. A namespace definition begins with the keyword namespace followed by the namespace name.
  150. To call the namespace-enabled version of either function or variable, prepend the namespace name.
  151. Eg....
  152. ----
  153.  
  154. #include <iostream>
  155. using namespace std;
  156.  
  157. // first name space
  158. namespace first_space{
  159. void func(){
  160. cout << "Inside first_space" << endl;
  161. }
  162. }
  163.  
  164. // second name space
  165. namespace second_space{
  166. void func(){
  167. cout << "Inside second_space" << endl;
  168. }
  169. }
  170.  
  171. int main () {
  172.  
  173. // Calls function from first name space.
  174. first_space::func();
  175.  
  176. // Calls function from second name space.
  177. second_space::func();
  178.  
  179. return 0;
  180. }
  181.  
  182. ----
  183. You can also avoid prepending of namespaces with the using namespace directive. This directive tells the compiler that the subsequent code is making use of names in the specified namespace.
  184. Eg....
  185. ----
  186. #include <iostream>
  187. using namespace std;
  188.  
  189. // first name space
  190. namespace first_space{
  191. void func(){
  192. cout << "Inside first_space" << endl;
  193. }
  194. }
  195.  
  196. // second name space
  197. namespace second_space{
  198. void func(){
  199. cout << "Inside second_space" << endl;
  200. }
  201. }
  202.  
  203. using namespace first_space;
  204. int main () {
  205.  
  206. // This calls function from first name space.
  207. func();
  208.  
  209. return 0;
  210. }
  211. ----
  212.  
  213.  
  214.  
  215. Q6.
  216.  
  217. A class constructor is a special member function of a class that is executed whenever we create new objects of that class.
  218.  
  219. A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.
  220.  
  221. A default constructor does not have any parameter, but if you need, a constructor can have parameters. This helps you to assign initial value to an object at the time of its creation as shown in the following example:
  222. ----
  223. #include <iostream>
  224.  
  225. using namespace std;
  226.  
  227. class TIME {
  228. private:
  229. double hr,min,sec;
  230. public:
  231. void getTime( void );
  232. TIME( double h, double m, double s );; // This is the parameterised constructor
  233. TIME(); // This is the default constructor
  234.  
  235. };
  236.  
  237. // Member functions definitions including constructor
  238. TIME::TIME( double h, double m, double s ){
  239. hr = h;
  240. min = m;
  241. sec = s;
  242. }
  243.  
  244. void TIME::getTime( void ) {
  245. cout << "Time is " << hr << " : " << min << " : " << sec << endl;
  246. }
  247.  
  248. TIME::TIME(void) {
  249. hr = 0; min = 0; sec =0;
  250. }
  251.  
  252. // Main function for the program
  253. int main( ) {
  254. TIME tt(10 , 23 , 12);
  255. TIME ts;
  256.  
  257. // time in the object with default constructor
  258. cout << "Time is : " << ts.getTime() <<endl;
  259.  
  260. // time in the object with parametered constructor
  261. cout << "Time is : " << tt.getTime() <<endl;
  262.  
  263. return 0;
  264. }
  265. ----
  266.  
  267.  
  268.  
  269. Q7.
  270.  
  271. The copy constructor is a constructor which creates an object by initializing it with an object of the same class, which has been created previously. The copy constructor is used to:
  272.  
  273. Initialize one object from another of the same type.
  274.  
  275. Copy an object to pass it as an argument to a function.
  276.  
  277. Copy an object to return it from a function.
  278.  
  279. If a copy constructor is not defined in a class, the compiler itself defines one.If the class has pointer variables and has some dynamic memory allocations, then it is a must to have a copy constructor. The most common form of copy constructor is shown here:
  280. ----
  281. #include<iostream>
  282. using namespace std;
  283.  
  284. class Point
  285. {
  286. private:
  287. int x, y;
  288. public:
  289. Point(int x1, int y1) { x = x1; y = y1; }
  290.  
  291. // Copy constructor
  292. Point(const Point \
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement