Languages-C & C Plus Plus [TCS Placement]: Sample Questions 27 - 28 of 119

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 27

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Can we have a pointer to a function? Given an example.

Explanation

  • Yes we can have pointer to function, in following example, foo is a pointer to a function taking one argument, an integer, and that returns void.
  • Function called “⚹ foo” , takes an int and returns void; now, if ⚹ foo is a function, then foo must be a pointer to a function.
  • Example:
  1. #include<stdio.h>
  2. voidfunc(inta)
  3. {
  4.     printf("Hello");
  5. }
  6. voidmain()
  7. {
  8.     void (⚹fp)(int);
  9.     fp=func;
  10.     fp(1);
  11. }
  • Output:

Hello

Question 28

C & C Plus Plus

Question

MCQ▾

Which of the following statement is correct?

Choices

Choice (4)

a.

A constructor is called at the time of creation of an object

b.

A constructor is called at the time of use of an object.

c.

A constructor is called at the time of use of a class.

d.

A constructor is called at the time of declaration of a class.

Answer

a.

Explanation

  • Constructor is called at the time of creating of an object dynamically at run time.
  • Constructor has same name as the class
  • Syntax: Classname reference variable = new classname () ;
  • Constructor name is also classname- calling the constructor at the time of object creation.
  • Example:
  1. #include<iostream>
  2. usingnamespacestd;
  3. classLine
  4. {
  5.     public:
  6.     voidsetLength( doublelen );
  7.     doublegetLength( void );
  8.     Line(); // This is the constructor
  9.     private:
  10.     doublelength;
  11. };
  12. // Member functions definitions including constructor
  13. Line::Line(void)
  14. {
  15.     cout ≪ "Object is being created" ≪ endl;
  16. }
  17. voidLine::setLength( doublelen )
  18. {
  19.     length =len;
  20. }
  21. doubleLine::getLength( void )
  22. {
  23.     returnlength;
  24. }
  25. // Main function for the program
  26. intmain( )
  27. {
  28.     Lineline;
  29.     // set line length
  30.     line.setLength(6.0);
  31.     cout ≪ "Length of line :" ≪ line.getLength() ≪endl;
  32.     return0;
  33. }
  • Output:

Object is being created

Length of line: 6

Developed by: