How can we assign variable values to a overload constructor separately(not at Initialization) in Cpp?

How can we assign variable values to a overload constructor separately(not at Initialization) in Cpp?



https://repl.it/@IT18117110/MidRevLecEx <<-----Link for the code



This is the error I got


Error: expected unqualified-id before '(' token
st[i].Student::(Name_i, id_i);


#include <iostream>
#include "Student.h"
#include "string.h"
using namespace std;

int main()

int i = 0;
char Name_i[20];
int id_i;
int mark1_i;
int mark2_i;
Student st[3];

do

cout<<"Enter Name:"<<i;
cin>>Name_i;
cout<<"Enter ID:";
cin>>id_i;
cout<<"Enter Marks1:";
cin>>mark1_i;
cout<<"Enter Marks2:";
cin>>mark2_i;

st[i].Student::(Name_i, id_i);
st[i].setMark1(mark1_i);
st[i].setMark2(mark2_i);
st[i].printMarks();
st[i].getAverageMark();
st[i].~Student();

i++;
while(i < 3);

return 0;





Please put all necessary information including code and error messages in the question itself, not using an external link. You can format code as a code block.
– Fei Xiang
Aug 20 at 4:39






A constructor is executed, by definition, to initialize an object. Not in any other circumstance. What are you trying to accomplish?
– StoryTeller
Aug 20 at 4:49





Your Student constructors get called implicitly when you declare Student st[3] and the destructors get called when the same array goes out of scope. It's bad form to call them manually under all but very unusual circumstances.
– alter igel
Aug 20 at 4:50



Student


Student st[3]





If you can't get hold of a good book, even the popular online tutorials would be better than whatever you're learning from now.
– molbdnilo
Aug 20 at 6:52




3 Answers
3



You should not attempt to call constructors or destructors explicitly.



Assign a new value to the object in the array, like you would with ints or anything else:


int


st[i] = Student(Name_i, id_i);



And remove


st[i].~Student();



The default constructor of Student class is already got called when you have created the static instance of your Student class
Student st[3];
You can simply write the print statement in the Student class constructor(which doesn't take any argument), you will find that it is getting called 3 times.



As per I know that constructor can't be called explicitly, it is used to construct the object. I think you should declare the pointer of Student class instance instead of declaring static and when you will provide the memory (by calling new operator) at that time you can provide the object initialization parameters value.



When you allocate objects on the stack (in the manner you have done in your example), you should not call constructors or destructors explicitly.
Constructor is called at the declaration:


Student st[3];



and the Destructor is called when the scope is exited.


return 0;
} <--Here



If you want to set the Name_i, id_i, use setter functions similar to the ones used to set the marks (setMark1 and setMark2).


Name_i


id_i


setMark1


setMark2






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

ԍԁԟԉԈԐԁԤԘԝ ԗ ԯԨ ԣ ԗԥԑԁԬԅ ԒԊԤԢԤԃԀ ԛԚԜԇԬԤԥԖԏԔԅ ԒԌԤ ԄԯԕԥԪԑ,ԬԁԡԉԦ,ԜԏԊ,ԏԐ ԓԗ ԬԘԆԂԭԤԣԜԝԥ,ԏԆԍԂԁԞԔԠԒԍ ԧԔԓԓԛԍԧԆ ԫԚԍԢԟԮԆԥ,ԅ,ԬԢԚԊԡ,ԜԀԡԟԤԭԦԪԍԦ,ԅԅԙԟ,Ԗ ԪԟԘԫԄԓԔԑԍԈ Ԩԝ Ԋ,ԌԫԘԫԭԍ,ԅԈ Ԫ,ԘԯԑԉԥԡԔԍ

How to change the default border color of fbox? [duplicate]

Henj