Structures

Can someone explain the following code. I need to include a structure in my program that would add a way to represent the worker's name as first name, last name, and "other" names (which could be a middle initial or several names)and salutaion (e.g. Mr. or Ms.)so that the name can be displayed or written to a file. I was given the following code as a guide but do not understand it.

struct FullName {
FullName( string salutaion,
string first,
string last,
string other )
: salutaion(salutaion),
last(last),
first(first),
other(other)
{}

string salutaion,
first,
last,
other;
};
ostream& operator<<( ostream& os, const FullName& n )
{ return os << n.salutaion << n.first << n.last << n.other ; }
closed account (zb0S216C)
flank wrote:
1
2
3
4
FullName( string salutaion,
string first,
string last,	
string other ) 

This is called a constructor, specifically, the constructor interface. Constructors are functions that are automatically invoked (called) when an object is created. In your case, when an object of type FullName is made, the constructor is invoked immediately. Constructors effectively build the object so that it's ready for use.

flank wrote:
1
2
3
4
: salutaion(salutaion),
last(last),
first(first),
other(other)

This is part of the constructor and it's the "member initialiser list". As the name suggests, it's used to give all data members of the class an initial value before any modifications to your class are made. The braces that follow is the body of the constructor. By the time you reach the code within the braces, all data members will have been fully constructed and initialised.

flank wrote:
1
2
3
4
string salutaion,
first,
last,
other;

These are you data members. Each FullName object will have a copy of these.

flank wrote:
1
2
ostream& operator<<( ostream& os, const FullName& n )
{ return os << n.salutaion << n.first << n.last << n.other ; }

This is an overload of the << operator. It informs the compiler on how to handle a FullName object when the << operator is invoked with a FullName object as an operand.

Wazzak
Last edited on
When the constructor FullName is called, is it the same as calling the Structure FullName? Should the structure and the constructor have the same name (i.e. be called FullName?

Also does the constructor FullName take in variables salutaion last, first and other or does it return these variables? Can it return more than one variable?
closed account (zb0S216C)
flank wrote:
"When the constructor FullName is called, is it the same as calling the Structure FullName?"

I'm not too sure what you mean here. The only way to invoke the constructor is by making an object:

 
FullName MyNewObject("Salutation", "A", "W", "H"); // Calls the constructor. 

flank wrote:
"Should the structure and the constructor have the same name"

Yes. If they don't, the compiler will assume it's a function without a return-type (error).

flank wrote:
"Also does the constructor FullName take in variables salutaion last, first and other or does it return these variables? Can it return more than one variable?"

A constructor cannot return anything; not even void. The objects within the parentheses are called "parameters". The constructor can use the non-constant parameters to store output to compensate. And, no, a constructor cannot return more than 1 object; no function can.

Wazzak
I began to write the following code, but due to my lack of knowledge, I do not know how to proceed:

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
struct FullName {
string salutation;
string firstname
string lastname; 
string othername;
}; // How do I incorporate 

FullName( string salutaion,
              string first,
              string last,			  
              string other ) 
        : salutaion(salutaion),
          last(last),
          first(first),
          other(other)
    {}
          
    string salutaion,
           first,
           last,
           other;


//Function to obtain employee name
FullName employee1
cout<< ”Enter salutation:”;
cin>> employee1.salutation;
cout<< ”Enter firstname:”;
cin>> employee1.firstname;
cout<< ”Enter lastname:”;
cin>> employee1.lastname;
cout<< ”Enter othername(s):”;
cin>> employee1.othername; //can a variable accept two values for example if the employees other names is Maury John, can employee1.othername accept the value Maury John?

//Function to display employee name
//How do I use the overload operator to display the employee name?

 
Hi Flank, get a C++ tutorial ( a lot from web) to learn it in a more easier way than this.


b2ee
Thanks to all who contributed. I was able to refine my code based on your guidance.
Topic archived. No new replies allowed.