How do you initialize a class member,
class x {
const int i;
};
Answers were Sorted based on User's Feedback
Answer / pappu
class abc
{
int a=4,b=3;
virtual mul(int int)=0;
}
| Is This Answer Correct ? | 10 Yes | 1 No |
Answer / santosh patil
class members can be initialized directly like
class x{const int i=10;}
but its structures members tat cant be initialized lik above
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / harminder
It can be done in the intialization list of the constructor
x():i=10
{
}
| Is This Answer Correct ? | 3 Yes | 2 No |
Answer / gayatri
using constructor we can initialize a class member in public
part of class.
class x
{
private: int i;
public: x()
{
i = 10;
}
| Is This Answer Correct ? | 1 Yes | 0 No |
Answer / jp
const data members must be initialized using Initializer List. In the following example, “t” is a const data member of Test class and is initialized using Initializer List.
#include<iostream>
using namespace std;
class Test {
const int t;
public:
Test(int t):t(t) {} //Initializer list must be used
int getT() { return t; }
};
int main() {
Test t1(10);
cout<<t1.getT();
return 0;
}
/* OUTPUT:
10
*/
| Is This Answer Correct ? | 0 Yes | 0 No |
What is a modifier in c++?
What does flush do?
What are iterators in c++?
What is std namespace in c++?
How can an improvement in the quality of software be done by try/catch/throw?
What is the best c++ book for beginners?
When is a template better solution than a base class??
I was a c++ code and was asked to find out the bug in that. The bug was that he declared an object locally in a function and tried to return the pointer to that object. Since the object is local to the function, it no more exists after returning from the function. The pointer, therefore, is invalid outside.
How do you instruct your compiler to print the contents of the intermediate file showing the effects of the preprocessor?
What are the differences between java and c++?
They will ask u question about single linked list?. Write Code for to insert delete node.
Do we have private destructors?