I was just wondering if you could explain to me how functions with parameters work. I've been following tutorials etc but they do not seem to explain how the code works to get the output. Heres an example:
#include <iostream>
using std::endl;
using std::cout;
using std::cin;
using std::string;
class stan
{
public:
void setName(string x)
{
name = x;
}
string getName()
{
return name;
}
private:
string name;
};
int main()
{
stan con;
con.setName("Bruce Almighty");
cout << con.getName() << endl;
}
I do not get how we get from the public string name to the private string name. What I'm saying must sound really confusing but I don't know how else to put it, I would just like to be able to understand how this code works. Thanks to anyone that helps, it means alot
main
. con
of type stan
. setName
of object con
is called with argument "Bruce Almighty"
. void setName(string x)
starts executing. Parameter x
takes the value of the corresponding argument ( "Bruce Almighty"
). name
of this object is set to the value of parameter x
( "Bruce Almighty"
). setName
returns to the point it was invoked. cout
represents console output. <<
, con.getName()
needs to be evaluated. Method getName
of object con
is called with no arguments. string getName()
starts executing. No parameters involved. name
of this object is returned ( "Bruce Almighty"
). getName
returns to the point it was invoked with value "Bruce Almighty"
. cout
uses this value and produces it in the console. cout
object produces endl
value in the console. endl
make a new line to be created in the console. http://www.cplusplus.com/doc/tutorial/classes/
- private members of a class are accessible only from within other members of the same class or from their friends.
- protected members are accessible from members of their same class and from their friends, but also from members of their derived classes.
- Finally, public members are accessible from anywhere where the object is visible.
Basically, a private member is something that ONLY a class function can access. So "string name" can only be read/write from class functions. Public functions/variables can be called from outside the class, the example you gave is "setName" and "getName"
So in order to write or read the private string, your code uses the public functions you created.
Basic steps:
con.setName("Bruce Almighty");
calls the setname
function and passes the string "Bruce Almighty"
to it.
void setName(string x)
receives the string "Bruce Almighty"
that you sent, and calls it x
.
name = x;
assigns the value of x
, which is "Bruce Almighty"
, to the variable name
.
con.getName()
asks for a value from the getName
function.
string getName()
declares the getName
function as one that returns a string
.
return name;
takes the string held in the variable name
, which setName
set to "Bruce Almighty"
and returns it to the caller, which sends it to cout
for outputting.
setName is somebody who helps you to deliver your string, "Bruce Almighty" to the private name field in the stan village.
You are saying, "Hey setName, I am going to pass "Bruce Almighty" argument as your parameter x . I don't care how you do it, but simply deliver it to the private name field!
setName says, "Ok, I got it, you mean my x parameter is Bruce Almighty right?"
You say, "Yes right!"
setName says. "Ok, I am done. You don't need to know how I did it. This is what we call abstraction. You simply order, but you don't need to understand how I did it. However, just in case you want to know, I did it by using the assignment operator = . I placed the private name field on the left side, and my parameter x on the right side. Because you provided Bruce Almighty to my parameter x , it is assigned to the private name field. Boom!"
Inside a class definition you can access data members using just their name. Eg inside setName
body the names of all members are in scope, thus also the private data member name
. In this way the statement:
name = x;
assigns the value of x
(the argument of setName
) to name
, which is the private data member.
You must be careful when naming members, though, as name clashes may arise if a local variable is named the same as a member. You can avoid them either prefixing your members with this->
like:
this->name = x;
or using a naming convention such as giving to any data member's name a prefix:
m_name = x; // "m_" stands for "member"
...
private:
string m_name;
this
is a C++ keyword representing a pointer to the object on which you invoke your methods (ie member functions). It can be used to refer to that object inside a class definition. Thus this->name
means "the member called name
inside the object pointed by the pointer this
"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.