arguments sent to constructor

Hello, I am trying to understand a code, there is a class sending to constructor the following:
class Course
{private:

int CourseID;
char* CourseName;

public:
Course(int=0,char[]="anonymous");
~Course(void);
int GetCourseID();
char* getCourseName();
};
Course::Course(int num,char* name)
{
CourseName=new char[strlen(name)+1];
strcpy(CourseName,name);
CourseID=num;
}


I haven't seen so far this kind of parameters being sending like this
Course(int=0,char[]="anonymous");

Does anybody have an article that could explain me this?Thanks
that means that the arguments are optional as they have default values
so calling Course() would be the same as calling Course(0,"anonymous")
Is it like to have a default and an argument constructor on only one declaration?
Another question on the same code: Instead of:


Course::Course(int num,char* name)
{CourseName=new char[strlen(name)+1];
strcpy(CourseName,name);}
,

Why can't I put like this:

CourseName=new char[strlen(name)+1];
Course name=name;
Because you are working with pointers:
name's value is a location in memory as well as CourseName
by using CourseName=name you would make CourseName pointing to the same location in memory, by using
1
2
CourseName=new char[strlen(name)+1];
strcpy(CourseName,name);
You allocate a new part of memory and set the values for that copying from the memory pointed by name

http://www.cplusplus.com/doc/tutorial/ntcs/
http://www.cplusplus.com/doc/tutorial/pointers/
Last edited on
Thanks, you helped me a lot
Topic archived. No new replies allowed.