c中将数组传递给子函数_在C ++中将对象传递给Non-Member函数
c中將數(shù)組傳遞給子函數(shù)
Here, we have to define a Non-Member Function, in which we have to pass an Object to the class in C++ programming language.
在這里,我們必須定義一個(gè)非成員函數(shù),其中必須將一個(gè)Object傳遞給C ++編程語言的類。
What we are doing in this example?
在此示例中我們正在做什么?
We declared a class named Number that has a private data member named num.
我們聲明了一個(gè)名為Number的類,該類具有一個(gè)名為num的私有數(shù)據(jù)成員。
We define a Non Member function named myFunction(), that will take two parameters 1) object to class Number and 2) an integer variable number.
我們定義了一個(gè)名為myFunction()的非成員函數(shù),該函數(shù)將使用兩個(gè)參數(shù):1)對象為Number類,以及2)一個(gè)整數(shù)變量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.
使用示例,我們必須使用非成員函數(shù)為類的數(shù)據(jù)成員提供一個(gè)數(shù)字(來自main()函數(shù))。
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中將數(shù)組傳遞給子函數(shù)
總結(jié)
以上是生活随笔為你收集整理的c中将数组传递给子函数_在C ++中将对象传递给Non-Member函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 马头琴多少钱啊?
- 下一篇: 密码学加密算法分类_密码学中的国际数据加