Skip to main content
 int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
    for(int i = 0;i < n;i++){
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
        per[i]->getdata(); // Get the data from the user.
 
    }
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
    return 0;
 
}
 int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
    for(int i = 0;i < n;i++){
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
        per[i]->getdata(); // Get the data from the user.
 
    }
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
    return 0;
 
}
 int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
    for(int i = 0;i < n;i++){
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
        per[i]->getdata(); // Get the data from the user.
 
    }
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
    return 0;
 
}
 int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
    for(int i = 0;i < n;i++){
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
        per[i]->getdata(); // Get the data from the user.
 
    }
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
    return 0;
 
}
deleted 9 characters in body; edited title
Source Link
Blasco
  • 753
  • 8
  • 22

HackerRank - Virtual Function Practice

int main(){
 int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
  
    for(int i = 0;i < n;i++){
  
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
  
        }
        else per[i] = new Student; // Else the current object is of type Student
  
        per[i]->getdata(); // Get the data from the user.
  
    }
  
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
  
    return 0;
  
}

HackerRank Virtual Function Practice

int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
     for(int i = 0;i < n;i++){
         std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
         }
        else per[i] = new Student; // Else the current object is of type Student
         per[i]->getdata(); // Get the data from the user.
     }
     for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
     return 0;
 }

HackerRank - Virtual Function Practice

 int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
    for(int i = 0;i < n;i++){
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
        per[i]->getdata(); // Get the data from the user.
 
    }
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
    return 0;
 
}
planted link to problem origin; decorated block quote
Source Link

This is just simple code to practice the use of virtual functionsvirtual functions from a practice challenge in HackerRank. Could you give me some advice about the best practices and style?

int main(){
int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
 
    for(int i = 0;i < n;i++){
 
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
 
        per[i]->getdata(); // Get the data from the user.
 
 
    }
 
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
 
    return 0;
 
 
}

This is just simple code to practice the use of virtual functions from a practice challenge in HackerRank. Could you give me some advice about the best practices and style?

int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
    for(int i = 0;i < n;i++){
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
        per[i]->getdata(); // Get the data from the user.
 
    }
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
    return 0;
 
}

This is just simple code to practice the use of virtual functions from a practice challenge in HackerRank. Could you give me some advice about the best practices and style?

int main(){
    int n, val;
    std::cin>>n; //The number of objects that is going to be created.
    Person *per[n];
 
    for(int i = 0;i < n;i++){
 
        std::cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;
 
        }
        else per[i] = new Student; // Else the current object is of type Student
 
        per[i]->getdata(); // Get the data from the user.
 
    }
 
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
 
    return 0;
 
}
Source Link
Blasco
  • 753
  • 8
  • 22
Loading