C++ 如何从另一个类调用函数?

C++ 如何从另一个类调用函数?,c++,C++,我正在尝试调用另一个类中的函数。我需要使用表面积函数或存储在其中的信息,从上一个类到另一个类。我该怎么做呢 我已经试过了HalfOpenCylinder::surfaceearea()和HalfOpenCylinder.surfaceearea(),但都没有成功 //surface area function that I want to use for other class double HalfOpenCylinder::surfaceArea(double height, double

我正在尝试调用另一个类中的函数。我需要使用表面积函数或存储在其中的信息,从上一个类到另一个类。我该怎么做呢

我已经试过了
HalfOpenCylinder::surfaceearea()
HalfOpenCylinder.surfaceearea()
,但都没有成功

//surface area function that I want to use for other class
double HalfOpenCylinder::surfaceArea(double height, double pi) {

    double surfaceArea = (2 * pi * radius * height) + (pi * pow(radius, 2));
    return surfaceArea;
}

我写了一个脚本,告诉你如何

“调用另一个类中的函数”

“将表面积函数或从上一个类中存储的信息用于另一个类”

这是一个包含许多示例的脚本。它使用surfaceearea()方法的更新版本(方法是术语,因为函数是从类中定义的)。我还将脚本产生的输出包含在脚本的最底部

<>你可以复制并将整个代码段转换成C++编译器,它应该为你工作。我在Visual Studio 2015社区中编译并测试了它。我去了新项目,并在C++类中创建了一个“Win32控制台应用程序”。p>
// ConsoleApplication10.cpp : Defines the entry point for the console application.
//
// This class example was created to answer kittykoder's question on StackOverflow.


// Both of these need to be included
#include "stdafx.h"
#include <iostream>

// We need the std namespace
using namespace std;

// Here I am defining a struct so that you can easily take all the values out of the
// HalfOpenCylinder class at once, and even make a new Cylinder object with the values by
// using the struct in one of the two HalfOpenCylinder class constructors.
struct CylinderValues
{
public:
    CylinderValues(double radius, double height, double surfaceArea) {
        this->radius = radius;
        this->height = height;
        this->surfaceArea = surfaceArea;
    }
    __readonly double radius;
    __readonly double height;
    __readonly double surfaceArea;
};

// This is the class I saw in your example.  Since it is named 
// HalfOpenCylinder, I decided to treat it like an
// instantiatable object class, both because it makes sense name wise, 
// and based on the context you provided in your question.
class HalfOpenCylinder
{
public:
    // Pi is always 3.14, so there is no reason to make it a passable parameter
    // like in your example. Thus I have made it a constant.  It's a static constant because
    // of the static function I've placed in this class to help in answering your question.
    static const float pi;

    // I have encapsulated the variables that make up this
    // class's objects behind methods so that the surface area can be
    // updated every time the radius or height values are changed.
    double GetRadius() { return radius; }
    void SetRadius(double value) { radius = value; UpdateSurfaceArea(); }
    double GetHeight() { return height; }
    void SetHeight(double value) { height = value; UpdateSurfaceArea(); }
    double GetSurfaceArea() { return surfaceArea; }

    // You can make a HalfOpenCylinder object with this constructor
    HalfOpenCylinder(double radius, double height) {
        this->radius = radius;
        this->height = height;
        UpdateSurfaceArea();
    }

    // You can use another HalfOpenCylinder object to make a new HalfOpenCylinder object using
    // this constructor.
    HalfOpenCylinder(CylinderValues values) {
        radius = values.radius;
        height = values.height;
        surfaceArea = values.surfaceArea;
    }

    // This will return the struct needed to use the constructor just above this comment.
    CylinderValues CopyValues() {
        return CylinderValues(radius, height, surfaceArea);
    }

    // Here is your surface area calculation from your question
    static double CalculateSurfaceArea(double radius, double height) {
        return (2 * pi * radius * height) + (pi * pow(radius, 2));
    }

private:
    // Here are the values you wanted to be able to access from another class.
    // You can access them using the methods above for getting and setting. The 
    // surfaceArea value is automatically recalculated if you change either the
    // radius or height variable's values.
    double radius;
    double height;
    double surfaceArea;

    // This method is here so that HalfOpenCylinder objects can use the
    // Surface area calculation.  I could have copied and pasted the calculation
    // code here to avoid calling the static method, but then I would be writing code
    // more than need be.  This way, you can update one and the other will be correct.
    void UpdateSurfaceArea() {
        surfaceArea = CalculateSurfaceArea(radius, height);
    }
};

// This is honestly just here because the compiler yelled at me for defining a static
// constant inside a non-static class.  Could'a gotten away with it in C#.  Thank you compiler.
const float HalfOpenCylinder::pi = 3.141592;

// This is called a function since it is outside of any class (although,
// that is one of the few differences between functions and methods.
// Methods being, functions defined inside classes)
void ThisIsAFunction() {
    cout << "This is the text from the function named: ThisIsAFunction";
}

// This class is just here to show you how to call functions and methods from inside classes
class CallFunctionAndMethodTester
{
public:

    void MethodInsideTheClass() {
        cout << "The below is printed from a function called in a class: \n";
        // Here, I am calling a function from inside a class
        ThisIsAFunction();
        cout << "\n\nThe below is printed from a static method called in a class: \n";
        // Here, I am calling a static method from inside a class
        cout << HalfOpenCylinder::CalculateSurfaceArea(14.5, 50.5);

        // Here, I am making an object instance from inside a class
        HalfOpenCylinder bobTheCylinder(1.5, 5.4);
        cout << "\n\nThe below is printed from an object's method called in a class: \n";
        // Here, I am calling an object's method from inside a class
        cout << bobTheCylinder.GetRadius();
    }

};

// Ok.  We made it.  THIS main function is where we will use and
// test the classes we have made above.
int main() {

    // Make a new cylinder object.  No pointer, so it will be destroyed when the computer
    // reads past main (which is the end of this program anyways).
    cout << "Cylinder 1 Values: \n";
    HalfOpenCylinder cylinder1(5.0, 10.0); 
    cout << cylinder1.GetRadius();
    cout << "\n"; // <--just makin' a newline here
    cout << cylinder1.GetHeight();
    cout << "\n";
    cout << cylinder1.GetSurfaceArea();
    cout << "\n\n"; // <--just makin' two newlines here

    // Change the object's height.  The surface area updates automatically.
    cout << "Cylinder 1 new surface area once Height is changed: \n";
    cylinder1.SetHeight(20.5);
    cout << cylinder1.GetSurfaceArea();
    cout << "\n\n";

    // Make a second Cylinder using the first cylinder's values.
    cout << "Cylinder 2 Values: \n";
    HalfOpenCylinder cylinder2(cylinder1.CopyValues());
    cout << cylinder2.GetRadius();
    cout << "\n";
    cout << cylinder2.GetHeight();
    cout << "\n";
    cout << cylinder2.GetSurfaceArea();
    cout << "\n\n";

    // Here I'm using the static CalculateSurfaceArea function to use the surface area
    // method without having to make a new HalfOpenCylinder object.
    cout << HalfOpenCylinder::CalculateSurfaceArea(5.0, 10.0);
    cout << "\n\n";

    // Here I am making an object of type CallFunctionAndMethodTester so that I can call
    // the method inside it that is using my example of how to call functions and methods
    // from within classes.
    CallFunctionAndMethodTester tester;
    cout << "Everything printed to the console after this line is printed using functions and methods that are called from inside classes. \n\n";
    tester.MethodInsideTheClass();

    int meh;
    cin >> meh;
    return 0;
}

/* Here is the output of this code when the program runs:

Cylinder 1 Values:
5
10
392.699

Cylinder 1 new surface area once Height is changed:
722.566

Cylinder 2 Values:
5
20.5
722.566

392.699

Everything printed to the console after this line is printed using functions and methods that are called from inside classes.

The below is printed from a function called in a class:
This is the text from the function named: ThisIsAFunction

The below is printed from a static method called in a class:
5261.38

The below is printed from an object's method called in a class:
1.5

*/
//ConsoleApplication10.cpp:定义控制台应用程序的入口点。
//
//创建这个类示例是为了回答kittykoder关于StackOverflow的问题。
//这两者都需要包括在内
#包括“stdafx.h”
#包括
//我们需要std名称空间
使用名称空间std;
//这里我定义了一个结构,以便您可以轻松地从
//一次半开圆柱体类,甚至可以使用
//在两个HalfOpenCylinder类构造函数之一中使用结构。
结构Cylinder值
{
公众:
圆柱体值(双半径、双高度、双表面A){
这个->半径=半径;
这个->高度=高度;
此->表面earea=表面earea;
}
__只读双半径;
__只读双倍高度;
__只读双面ea;
};
//这就是我在你的例子中看到的类。因为它叫
//半开圆筒,我决定把它当作一个
//可实例化的对象类,因为它在名称方面有意义,
//根据你在问题中提供的背景。
半开式气缸
{
公众:
//Pi始终为3.14,因此没有理由将其作为可通过的参数
//就像你的例子一样,所以我把它设为一个常数,它是一个静态常数,因为
//我在这个类中放置的静态函数的一部分,用来帮助回答您的问题。
静态常数浮点数;
//我已经封装了组成这个函数的变量
//在方法后面类的对象,以便可以
//每次更改半径或高度值时更新。
double GetRadius(){return radius;}
void SetRadius(双值){radius=value;UpdateSurfaceArea();}
double GetHeight(){返回高度;}
void SetHeight(双值){height=value;UpdateSurfaceArea();}
double GetSurfaceArea(){return surfaceArea;}
//可以使用此构造函数创建半开放圆柱体对象
半开圆柱体(双半径、双高度){
这个->半径=半径;
这个->高度=高度;
UpdateSurfaceArea();
}
//可以使用另一个半开放圆柱体对象来创建新的半开放圆柱体对象
//这个构造函数。
半开圆柱体(圆柱体值){
半径=值。半径;
高度=数值。高度;
surfaceearea=值。surfaceearea;
}
//这将返回使用此注释上方的构造函数所需的结构。
CylinderValues CopyValues(){
返回圆柱体值(半径、高度、表面ea);
}
//这是根据您的问题计算的表面积
静态双计算表面面积(双半径、双高度){
返回(2*pi*半径*高度)+(pi*pow(半径,2));
}
私人:
//以下是您希望能够从另一个类访问的值。
//您可以使用上述获取和设置的方法访问它们
//如果更改以下任一项,则会自动重新计算surfaceArea值:
//半径或高度变量的值。
双半径;
双高;
双表面积;
//此方法用于使半开放圆柱体对象可以使用
//表面积计算。我可以复制并粘贴计算
//在这里编写代码以避免调用静态方法,但这样我就可以编写代码了
//超出需要。这样,您可以更新其中一个,而另一个将是正确的。
void UpdateSurfaceArea(){
表面面积a=计算表面面积(半径、高度);
}
};
//老实说,这只是因为编译器因为我定义了一个静态
//非静态类中的常量。在C#中能逃脱吗。谢谢你。
恒浮子半开圆柱::pi=3.141592;
//这称为函数,因为它不属于任何类(尽管,
//这是函数和方法之间为数不多的差异之一。
//方法(在类中定义的函数)
void thisisa函数(){

cout要从另一个类调用函数,您需要首先创建该类的对象(实例)。 使用该对象可以调用特定函数

例如:

#包括
使用名称空间std;
班级学生
{//定义类
公众:
int-id;
void add(){
int x=1;
int y=2;
intz=x+y;

cout
半开式气缸h;h.表面轴承A(…)
-您需要一个类的实例,并且函数必须是公共的,除非这些类通过继承而相关。或者,
friend
s…老实说,如果您想在没有该类对象的情况下调用它,那么函数不应该是该类的一部分。为什么不发布想要调用t的代码呢
 #include <iostream> 
 using namespace std;  
    class Student 
    {         // defining class 
       public:  
          int id;

          void add(){
            int x=1;
            int y=2;
            int z=x+y;
            cout<<z<<endl;
            }


    };  
    int main() {  
        Student s1;      // creating object of  class

        s1.id=20;
        s1.add()                // calling function of that class


        return 0;  
    }