using namespace std;
class Student_info
{
private:
char s_name[10];
int s_age = 0;
char s_university[100];
char s_major[100];
public:
Student_info();
Student_info(char* name, int age, char* univ, char* major);
bool find(char* name);
void print();
void change(char* name, int age, char* univ, char* major);
};
Student_info::Student_info() {
for (int i = 0; i < 10; i++) {
s_name[i] = NULL;
}
for (int i = 0; i < 100; i++) {
s_university[i] = NULL;
s_major[i] = NULL;
}
}
//Default constructor (initialize class array)
Student_info::Student_info(char* name, int age, char* univ, char* major) {
s_name[10] = *name;
s_age = age;
s_university[100] = *univ;
s_major[100] = *major;
}
bool Student_info::find(char* name){
if (s_name[10] == *name) {
return 1;
}
else
return 0;
}
void Student_info::print() {
cout << "--------------------------" << endl;
cout << "Name: " << s_name[10]<<endl;
cout << "Age : " << s_age<<endl;
cout << "uni : " << s_university[100] << endl;
cout << "major:" << s_major[100] << endl;
cout << "--------------------------" << endl;
}
void Student_info::change(char* name, int age, char* univ, char* major) {
s_name[10] = *name;
s_age = age;
s_university[100] = *univ;
s_major[100] = *major;
}
int main() {
string comd; //char* is readable only!!! It's impossible to write!!!!!!
char name[10]; int age; char univ[100]; char major[100];
int b = 0;
class Student_info* info = new class Student_info[9];
while (1) {
cout << "Please Enter Command(insert, find, change, print, exit)";
cin >> comd;
if (comd == "insert"){
cin >> name >> age >> univ >> major;
info[b].Student_info(name,age,univ,major); // Let's find a way to call constructors by index of this part
continue;
}
else if(comd =="find"){
continue;
}
else if(comd =="change"){
continue;
}
else if(comd =="print"){
info[b].print();
continue;
}
else if(comd =="exit"){
continue;
}
else{
break;
}
b++;
}
return 0;
}
In this code, the main function portion of the main function if (comd == "insert"){ cin >> name >> age >> univ >> major; info[b].Student_info(name,age,univ,major); // Let's find a way to call constructors by index of this part Here, info[b].Student_info (~) I know this part is wrong, but in that way... I want to call the constructor for each created object index, but I can't find a way. Is there anyone who can help me?
c++
The best way to call constructors for the code above is as follows.
info[b] = { name, age, univ, major };
info[b] itself is a class, and when calling the constructor, by default, simply put the input parameters in the class = { constructor in order.}
Did it work out? . Subsequent calls can only be called by normal functions (methods) within the class. If it's resolved, I'm happy. Please press it.~
It's a different story from the question, so I'll write it down below. Other than this, there are more than one or two questions. The first question is why s_name[10] only has the corresponding value for one address. If you use Nemspace std, I think you can exchange it with a string. You'll notice something strange if you just type in two letters. And the second one is counted, so if you proceed like this, I understand the intention, but I don't think you can go to b++ forever. (Laughs) The countinue is a jump. Repeat the while statement, but the sentence under count cannot be executed!
925 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
585 PHP ssh2_scp_send fails to send files as intended
618 GDB gets version error when attempting to debug with the Presense SDK (IDE)
574 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
© 2024 OneMinuteCode. All rights reserved.