如何从另一类调用功能

How to call a function from another class?

本文关键字:一类 调用 功能      更新时间:2023-10-16

我试图在另一类中调用一个函数。我需要使用表面积功能或从上一类中存储的其他类中存储的信息。我该怎么做?

我已经尝试了HalfOpenCylinder::surfaceArea()HalfOpenCylinder.surfaceArea(),也没有工作。

//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;
}

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

eg:

 #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;  
    } 

我写了一个脚本,向您展示了如何

"在另一类中调用函数"

"使用表面积功能或从上一类中存储的信息的信息"。

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

您可以将整个代码段复制到C 编译器中,并且应该对您有用。我在Visual Studio 2015社区中进行了编译并测试了它。我去了新项目,并在C 类别中创建了一个" Win32控制台应用程序"。

// 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 << "nnThe 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 << "nnThe 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 << "nn"; // <--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 << "nn";
    // 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 << "nn";
    // 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 << "nn";
    // 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. nn";
    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
*/