0

Why are we giving void as a parameter for the getdetails function, i.e, getdetails(void) and not (char name, int rollNO) as a parameter? Why is this being done only while using array of class objects. Why?

class student
{
    private:
    char name[30];
    int rollNo;
    public:
    void getDetails(void); // Member function to get student's details
    void putDetails(void); // Member function to print student's details
};

void student::getDetails(void) // Member function definition, outside of the class
{
    cout << "Enter name: ";
    cin >> name;
    cout << "Enter roll number: ";
    cin >> rollNo;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tanmai
  • 3
  • 2
  • 2
    Because whoever wrote that probably spent a long time writing old C code, where this is common, but in C++ this doesn't mean anything. Further evidence of that is `char name[30];`, instead of `std::string`. – Sam Varshavchik Aug 18 '20 at 16:58
  • `some_function (void)` is just another way to write `some_function()` – Miguel Aug 18 '20 at 16:59
  • @Miguel -- that's not quite right. You're right that as a function **declaration** or **definition** they're the same. That is, `int f() {}` and `int f(void) {}` mean the same thing. But when **calling** the function they're not the same. `f(void)` isn't legal. – Pete Becker Aug 18 '20 at 18:43
  • @PeteBecker You are right, I meant in declarations. I should've specified. – Miguel Aug 18 '20 at 18:53
  • @Miguel -- I figured that was what you meant. Just wanted to underscore it. – Pete Becker Aug 18 '20 at 18:56

0 Answers0