0

Can i initialize variables with value when we creatjng class??

Example: Class temp{ Private: Int a[3] = {12,2,3}; };

9th Apr 2020, 10:35 AM
Gaurav Rawat
Gaurav Rawat - avatar
8 Respuestas
+ 3
In any of these cases, in case of doubt, you should do this: Just try it. Make a little code, testing if what you want to do works. #include <iostream> using namespace std; class C{ int a[3] = {12,2,3}; public: void f() { cout << a[0] << a[1] << a[2] << endl; } }; int main() { C x = C(); C y = C(); x.f(); y.f(); return 0; }
9th Apr 2020, 1:02 PM
HonFu
HonFu - avatar
+ 2
I think you can but the 'i' of int must be small 😅
9th Apr 2020, 12:12 PM
Bhavya Lohami
Bhavya  Lohami - avatar
+ 1
Do you mean values for each instance? Or static class variables? Or something else? Can you explain with more detail, what exactly you want to do?
9th Apr 2020, 12:26 PM
HonFu
HonFu - avatar
+ 1
For each instances
9th Apr 2020, 12:52 PM
Gaurav Rawat
Gaurav Rawat - avatar
+ 1
If i create object of temp t then t has a[3] variable with values 12,2 and 3. Is it right to do?
9th Apr 2020, 12:54 PM
Gaurav Rawat
Gaurav Rawat - avatar
+ 1
It can be done. That's part of what constructors are for. You can have default values for some fields.
9th Apr 2020, 10:28 PM
Bill Lugo
Bill Lugo - avatar