I'm a fairly new C ++ programmer, and I would like to hear arguments for and against naming parameters in the class declaration.
Here is an example:
Student.h
#ifndef STUDENT_H_ #define STUDENT_H_ #include <string> using namespace std; class Student { private: string name; unsigned int age; float height, GPA; public: Student(string, unsigned int, float, float); void setAge(unsigned int); }; #endif /*STUDENT_H_*/
against.
#ifndef STUDENT_H_ #define STUDENT_H_ #include <string> class Student { private: string name; unsigned int age; float height, GPA; public: Student(string name, unsigned int age, float height, float GPA); void setAge(unsigned int age); }; #endif /*STUDENT_H_*/
Student.cpp
#include "Student.h" Student::Student( string name, unsigned int age, float height, float GPA) : name(name), age(age), height(height), GPA(GPA) {} void Student::setAge(unsigned int age) { this -> age = age; }
I canโt decide. On the one hand, I believe that it is redundant to name variables both in the declaration (.h) and in the definition (.cpp). Especially since you need to worry about updating the names in both places to match each other. On the other hand, without names it is often difficult to determine which variables match the parameters simply by looking at the declaration.
So what do you think?
source share