2
Bozo(const char * fname, const char * lname); // constructor prototype

In this case, you would use it to initialize new objects as follows:

Bozo bozetta = Bozo("Bozetta", "Biggens"); // primary form
Bozo fufu("Fufu", "O’Dweeb"); // short form
Bozo *pc = new Bozo("Popo", "Le Peu"); // dynamic object

I just have a few questions regarding this. First is that, why is const needed before char? or why is it there?. Also, why is it declaring as a pointer?

Second, is there any difference between the "primary form" and the "short form?"

Third question is that in Java, I used string variables for the formal parameters, but in C++ it's in char? I thought char can only contain a single alphabet, and it's not a char array. Could I do this with string instead?

Aaron
  • 959
  • 2
  • 9
  • 14
  • possible duplicate of [Is there a difference in C++ between copy initialization and direct initialization?](http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initializati) – Ben Voigt Sep 12 '13 at 03:47
  • Besides all that, the code you showed is ill-formed. Curly quotes mean nothing to the compiler. – Ben Voigt Sep 12 '13 at 03:49
  • 1
    Yes, C++ has an actual string type. Given that it's `std::string`, you can find it with minimal searching. – chris Sep 12 '13 at 03:50
  • So do you prefer asking three separate questions for this? – Aaron Sep 12 '13 at 03:51
  • "const" is a contract that you won't modify the contents of the variable. "const char*" means "points to an array of chars", which is a c-style string, typically 0+ char's followed by one with a value of 0 as a terminator, the const part meaning "promise not to modify them". Consider `strcpy(char* dest, const char* src)` the src string is copied without being modified to the destination, since we ARE going to make modifications to the destination array, it's not const. – kfsone Sep 12 '13 at 03:55
  • 1
    @aaron: Yes, that way you can pick a separate "best answer" for each question. – Ben Voigt Sep 12 '13 at 03:59

2 Answers2

2

Yes Aaron 1 Ans: const is needed before char because here we are wanting not the value of the string to be change mistakenly.Const will keep remain the original value because we pass to constructor Pointer ( by ref ). 1 Ans: Bozo bozetta = bozo(“Bozetta”, “Biggens”); // primary form Delcare Bozo bozetta = bozo ( "Bozetta", "Biggens" ) ; Ya there is a lot difference between short and primary is that we declaring an object of type Bozo and and assigning it new object values. 3 Ans: Yes you are correct that char contains only 1 character at a time but here you declared char * ( char pointer ) which is mean char array. Char pointer is consider String in C++.

Muhammad Usman
  • 77
  • 1
  • 2
  • 9
2

First is that, why is const needed before char? or why is it there?. Also, why is it declaring as a pointer?

I'll start by answering your last question first. This is declared as a pointer so the constructor can accept an array of characters. The const is there to indicate that the characters being pointed to are not modified inside this bozo constructor. This is important because your example usage can pass in C-style string literals which typically reside in read-only memory portion of your program. Any attempts to modify such a string literal is undefined behavior.

Second, is there any difference between the "primary form" and the "short form?"

The first form is creating a temporary Bozo object on the right-hand side and that object is used to initialize bozetta on the left-hand side. Now you might think that an extra copy happens with this form but that isn't the case. The C++ standard is worded in such a way so that it permits the compiler to skip this unnecessary extra copy during construction by eliding. So in practice, you'll find that on all modern C++ compilers you try this on, bozetta will be constructed using the prototype you showed above -- no extra copying happens.

The second form explicitly constructs a Bozo object using a Bozo constructor that can accept two string literals as parameters.

These two forms have the same behavior assuming that constructor isn't declared explicit.

I thought char can only contain a single alphabet, and it's not a char array.

That is correct, a char can only hold one byte worth of information. C and C++ doesn't have an actual native string type, not like the type you're use to like in other languages. So a "string" is really just a bunch of chars laid out in a contingent block of memory. Your Bozo constructor above can work with this block by taking a pointer to where that block starts as input.

Could I do this with string instead?

Yes you can and that is actually the preferred way you should do this by using std::string. The constructor declaration would look like this:

Bozo(std::string fname, std::string lname);
// or to avoid potential copying of long strings
Bozo(const std::string &fname, const std::string &lname);
greatwolf
  • 20,287
  • 13
  • 71
  • 105