简体   繁体   English

C ++函数参数和类

[英]C++ function parameters and classes

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 感谢任何人的帮助,这意味着很多

  1. Program starts at function main . 程序从函数main开始。
  2. It declares a local variable con of type stan . 它声明了一个类型为stan的局部变量con
  3. Method setName of object con is called with argument "Bruce Almighty" . 使用参数"Bruce Almighty"调用对象con setName方法。
  4. Method with heading void setName(string x) starts executing. 标题为void setName(string x)开始执行。 Parameter x takes the value of the corresponding argument ( "Bruce Almighty" ). 参数x接受相应参数的值( "Bruce Almighty" )。
  5. Private member name of this object is set to the value of parameter x ( "Bruce Almighty" ). 该对象的私有成员name设置为参数x"Bruce Almighty" )的值。
  6. Method setName returns to the point it was invoked. 方法setName返回到它被调用的位置。
  7. cout represents console output. cout表示控制台输出。
  8. To apply operator << , con.getName() needs to be evaluated. 要应用运算符<< ,需要评估con.getName() Method getName of object con is called with no arguments. 没有参数调用对象con方法getName
  9. Method with heading string getName() starts executing. 标题为string getName()开始执行。 No parameters involved. 不涉及任何参数。
  10. Value of private member name of this object is returned ( "Bruce Almighty" ). 返回此对象的私有成员name的值( "Bruce Almighty" )。
  11. Method getName returns to the point it was invoked with value "Bruce Almighty" . 方法getName返回到使用值"Bruce Almighty"调用的点。
  12. cout uses this value and produces it in the console. cout使用此值并在控制台中产生它。
  13. The same cout object produces endl value in the console. 相同的cout对象在控制台中产生endl值。 endl make a new line to be created in the console. endl在控制台中创建一个新行。

http://www.cplusplus.com/doc/tutorial/classes/ 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" 可以从类外部调用公共函数/变量,您给出的示例是“ setName”和“ 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. 调用setname函数,并将字符串"Bruce Almighty"传递给它。

void setName(string x)

receives the string "Bruce Almighty" that you sent, and calls it x . 接收您发送的字符串"Bruce Almighty" ,并将其称为x

name = x;

assigns the value of x , which is "Bruce Almighty" , to the variable name . x的值"Bruce Almighty"分配给变量name

con.getName()

asks for a value from the getName function. getName函数要求一个值。

string getName()

declares the getName function as one that returns a string . getName函数声明为一个返回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. 接收保存在变量name的字符串,该字符串的setName设置为"Bruce Almighty" ,并将其返回给调用方,后者将其发送到cout进行输出。

setName is somebody who helps you to deliver your string, "Bruce Almighty" to the private name field in the stan village. setName是可以帮助您将字符串“ Bruce Allmighty”传送到stan村庄的私有名称字段的人。

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,我将传递“ Bruce Almighty”参数作为您的参数x 。我不在乎您如何操作,只需将其传递到私有名称字段即可!

setName says, "Ok, I got it, you mean my x parameter is Bruce Almighty right?" setName说,“好吧,我明白了,您的意思是我的x参数是Bruce Allmighty,对吗?”

You say, "Yes right!" 您说:“是的!”

setName says. setName说。 "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!" “好,我完成了。您不需要知道我是如何做的。这就是我们所说的抽象。您只需订购,但是您不需要了解我是如何完成的。但是,以防万一,您想要要知道,我使用赋值运算符=做到了这一点,我在左侧放置了私有名称字段,在右侧放置了参数x ,因为您为我的参数x提供了Bruce Almighty,所以将其分配给了私有名称场!轰!

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 . 例如,在setName主体中,所有成员的名称都在范围内,因此,私有数据成员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. x的值( setName的参数)分配给name ,后者是私有数据成员。

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->这样的:

    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). this是一个C ++关键字,表示指向您在其上调用方法(即成员函数)的对象的指针。 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 " 因此this->name 意思是“构件称为name由指针指向的对象内部this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM