Languages [TCS Placement]: Sample Questions 104 - 104 of 131

Get unlimited access to the best preparation resource for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 104

Question

MCQ▾

Which of the following term is used for a function defined inside a class?

Choices

Choice (4)

a.

Member variable

b.

Classic function

c.

Class function

d.

Member function

Answer

d.

Explanation

  • “Member functions” are the functions defined inside a class
  • Member function has definition or prototype within the class definition like other variables.
  • Operates on object of the class of which it is a member with access to all the members of a class for that object.
  • Member functions can be defined within the class definition or separately using scope resolution operator (:) . Defining a member function within the class definition declares the function inline, even if you do not use the inline specifier.
  • Example
  1. #include<iostream>
  2. usingnamespacestd;
  3. classBox
  4. {
  5.     public:
  6.     doublelength;
  7.     doublebreadth;
  8.     doubleheight;
  9.     // Member functions declaration
  10.     doublegetVolume(void);
  11.     voidsetLength( doublelen);
  12.     voidsetBreadth( doublebre);
  13.     voidsetHeight( doublehei);
  14. };
  15. // Member functions definitions
  16. doubleBox::getVolume(void)
  17. {
  18.     returnlength ⚹breadth ⚹height;
  19. }
  20. voidBox::setLength( doublelen )
  21. {
  22.     length =len;
  23. }
  24. voidBox::setBreadth( doublebre )
  25. {
  26.     breadth =bre;
  27. }
  28. voidBox::setHeight( doublehei )
  29. {
  30.     height =hei;
  31. }
  32. // Main function for the program
  33. intmain()
  34. {
  35.     BoxBox1;
  36.     BoxBox2;
  37.     doublevolume =0.0;
  38.     // box 1 specification
  39.     Box1.setLength(4.0);
  40.     Box1.setBreadth(7.0);
  41.     Box1.setHeight(8.0);
  42.     // box 2 specification
  43.     Box2.setLength(16.0);
  44.     Box2.setBreadth(18.0);
  45.     Box2.setHeight(10.0);
  46.     // volume of box 1
  47.     volume =Box1.getVolume();
  48.     cout ≪ "Volume of Box1 :" ≪ volume ≪endl;
  49.     // volume of box 2
  50.     volume =Box2.getVolume();
  51.     cout ≪ "Volume of Box2 :" ≪ volume ≪endl;
  52.     return0;
  53. }
  • Output:

Volume of Box1: 224

Volume of Box2: 2880

Developed by: