c中将数组传递给子函数_在C ++中将对象传递给Non-Member函数
c中將數組傳遞給子函數
Here, we have to define a Non-Member Function, in which we have to pass an Object to the class in C++ programming language.
在這里,我們必須定義一個非成員函數,其中必須將一個Object傳遞給C ++編程語言的類。
What we are doing in this example?
在此示例中我們正在做什么?
We declared a class named Number that has a private data member named num.
我們聲明了一個名為Number的類,該類具有一個名為num的私有數據成員。
We define a Non Member function named myFunction(), that will take two parameters 1) object to class Number and 2) an integer variable number.
我們定義了一個名為myFunction()的非成員函數,該函數將使用兩個參數:1)對象為Number類,以及2)一個整數變量number 。
Using the example, We have to supply a number (from the main() function) to the class's data member using a Non-Member Function.
使用示例,我們必須使用非成員函數為類的數據成員提供一個數字(來自main()函數)。
Program:
程序:
#include <iostream> using namespace std;class Number {private:int num;public:void setNum(int n){num = n;}int getNum(void){return num;} };//a non member function void myFunction(class Number N, int number) {//calling setter function and asigning the number N.setNum(number) ;//calling getter function and printing the value cout<<"The value is: " << N.getNum() << endl; }//Main function int main() {//local variable of the main int num;//object to Number class Number objN;num = 10;//supplying this 'num' to the class by passing //the name to the class in a non memberfunctionmyFunction (objN, num);return 0; }Output
輸出量
The value is: 10翻譯自: https://www.includehelp.com/cpp-programs/passing-an-object-to-a-non-member-function-in-cpp.aspx
c中將數組傳遞給子函數
總結
以上是生活随笔為你收集整理的c中将数组传递给子函数_在C ++中将对象传递给Non-Member函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 马头琴多少钱啊?
- 下一篇: 密码学加密算法分类_密码学中的国际数据加