Hi jaituk ^_^
It's ugly C++ which is exactly ugly C code hehehe.
Your function Student() has the same name of your class Student so it is a constructor.
The first parameter it takes is a C string, that is a pointer on char.
The second parameter is a number.
And both parameters are optional... because there are use of the "=" after each parameter definition in the prototype of Student().
So when you create an instance of your class Student, a "default one" with no arguments at all... by defaut, the construction Student() receives the defaut parameters which are the string "nobody" and the number 0.0. So to say, the "compiler" do this calll for you Student("nobody", 0.0).
I'm not sure about what you don't understand here. So if it is the syntax, then as any function, a constructor can take a paremeter "optionallly" : that is if you don't give the parameter, the compiler calls the function with the defaut value given at the definition of the function.
If what you don't understand is the char* name = "nobody", then "nobody" is just a litteral (a constant string here) that is used temporarily as default value of the parameter n. By the way, OMG for the naming ! "n" pfffff. Why not just "name" ^_^
The strcpy copies the parameter name "n" you give to the function to your variable "name" which is a member variable of your class Student. Because name is a constant size array of char, it is "atomatically" allocated when you create an instance of the class Student and that array is 20 characters long.
So there is a huge bug here ! because if you give a parameter "n" longer than 19 characters, the strcpy() function will copy your C-string to a constant array of only 20 characters in memory. So creating an instance Student("Jean Duval de la Broche Pelle-Melle") will not be a good idea ^_^.
This code is very dangerous, buggy, incorrect...
For your last comentary, I don't understand the "doesn't work" part. What doesn't work then ?
You mean, you don't store the name of your Student objet ? like
auto me = Student("bibi");
... and the "name" is not "bibi" ?
Well for an other way to do it, yes there is ^_^
In C++, there is a real class string available. It is called "string" in the namespace std.
To use it, you must #include <string>, be carefull => not <string.h> !
Then you have a type called std::string.
So for example, you define your class Student with a string named "name" as member variable. And then almost all the rest is the same. Basically :
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <string>
class Student
{
public:
std::string name;
double average;
public:
Student(std::string aName = "nobody", double aAverage = 0.0)
{
name = aName;
average = aAverage;
}
| |
...
I put the variable in "public:" section for the moment because I'm dumping them afterwards.
Then create an instance or two of the class Student :
1 2 3
|
auto me = Student("bibi");
auto you = Student("chirashi");
auto who = Student();
| |
And when you show the value of me.name, you.name and who.name... the answers are what we (I hope) expected ^_^~ that is : "bibi", "chirashi" and "nobody"
You can find the complete source code here
https://www.punksheep.com/partage/C++/testStudentClass.zip
And here the full dump :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
//[brief] test of a class with specific constructor and full optional parameters and string default value
//[forum] http://www.cplusplus.com/forum/general/235069
//
//[author] punks Marc
//[contact email] punksheep@kantamal.net
//[web site] http://www.punksheep.com
//
//[note] delete the line <<#include "stdafx.h">> if it doesn't compile.
#include "stdafx.h"
#include <iostream>
#include <string>
class Student
{
public:
std::string name;
double average;
public:
Student(std::string aName = "nobody", double aAverage = 0.0)
{
name = aName;
average = aAverage;
}
void prt();
void get();
};
int main()
{
using namespace std;
//--
auto me = Student("bibi");
auto you = Student("chirashi");
auto who = Student();
cout << me.name << endl;
cout << you.name << endl;
cout << who.name << endl;
return 0;
}
| |
Hope that helps :3