

Per->getdata() // Get the data from the user.įor(int i=0 iputdata() // Print the required output for each object. If val is 1 current object is of type ProfessorĮlse per = new Student // Else the current object is of type Student Std::cout >n //The number of objects that is going to be created. This is just a simple exercise from HackerRank #include Could you give me some advice about the best practices and style? It's considered bad practice for various reasons and will only save you 5 characters every once in a while.This is just simple code to practice the use of virtual functions from a practice challenge in HackerRank. But I would reduce on that and use only #include

Per->getdata() // Get the data from the user.įor(int i=0 iputdata() // Print the required output for each object.ĭo you really need all of that? Especially mixing and seems questionable.

Name, age and the marks of the student in \$6\$ subjects.įor(int i =0 i >n //The number of objects that is going to be created. If the object is of the Student class, you will have to input the Professor class, you will have to input the name, age and publications You can create more data members if you want.

Solve this problem using virtual functions, constructors and static Theįunction putdata should print the name, age, sum of the marks andįor each object being created of the Professor or the StudentĬlass, sequential id's should be assigned to them starting from \$1\$. User: the name, age, and the marks of the student in subjects. The function getdata should get the input from the It has two member functions: getdataĪnd putdata. The class Professor should have two data members: publications andĬur_. The classes Professor and Student should inherit from the class Person. Person should have data members name and age. I know this is a simple example but how can this be improved? Syntax wise or shortened code wise?Ĭreate three classes Person, Professor and Student. I'm a newbie in C++ (6 hours) and I have implemented this problem in C++.
