您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“C++一個(gè)函數(shù)怎么調(diào)用其他.cpp文件中的函數(shù)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“C++一個(gè)函數(shù)怎么調(diào)用其他.cpp文件中的函數(shù)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
使用VC或VS創(chuàng)建C++項(xiàng)目的時(shí)候,會(huì)自動(dòng)產(chǎn)生許多文件夾,其中有一個(gè)文件夾->源文件:
在該文件下可以自定義許多.cpp文件,但是需要注意的是這里面的各個(gè)文件只能有一個(gè)文件中含有main()函數(shù),
而且各個(gè)文件中不能使用相同的函數(shù)名進(jìn)行定義;
那么要那么多文件放在項(xiàng)目中有什么用呢?
當(dāng)然這里C++是提供一個(gè)文件調(diào)用其他文件中函數(shù)的功能的,
這就可以讓我們自定義一個(gè)只包含main()函數(shù)的文件,通過(guò)在該函數(shù)中調(diào)用其他文件中的函數(shù)就可以將各個(gè)文件鏈接起來(lái),
而且更重要的一點(diǎn)就是,通過(guò)調(diào)用其他,cpp文件中的函數(shù)的時(shí)候,如果調(diào)用的某函數(shù)又調(diào)用了它自己文件中的一個(gè)函數(shù),
那么只用調(diào)用“父級(jí)函數(shù)”就可以實(shí)現(xiàn)間接調(diào)用~~~
首先是資源管理窗口:
功能主函數(shù).cpp
// C++上機(jī)作業(yè).cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。 //'0-9': 48-57 #include "stdafx.h" using namespace std; extern void gotoxy(short x, short y); extern void sort_by_name(); extern int Strtoint(); int main() { system("title 功能主函數(shù)"); gotoxy(23, 2); cout << "功能列表"; gotoxy(15, 3); cout << "1:字符串轉(zhuǎn)換為數(shù)值類(lèi)型"; gotoxy(15, 4); cout << "2:對(duì)中文字符進(jìn)行排序"; gotoxy(0, 10); int choice = 0; cout << "請(qǐng)輸入您要執(zhí)行的功能:"; cin >> choice; getchar(); //吸收回車(chē) switch (choice) { case 1: Strtoint(); break; case 2: sort_by_name(); break; default: cout << "選擇失敗,感謝使用,再見(jiàn)!" << endl << endl; } return 0; }
stdafx.h(stdandard application framework extensions)
// stdafx.h : 標(biāo)準(zhǔn)系統(tǒng)包含文件的包含文件, // 或是經(jīng)常使用但不常更改的 // 特定于項(xiàng)目的包含文件 // #pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #include <iostream> #include <Windows.h> #include <string> //注意這里的string與cstring中的使用差別,在定義與使用cout輸出string類(lèi)型字符串的時(shí)候,最好使用string庫(kù),否則可能會(huì)出現(xiàn)亂碼以及錯(cuò)誤等一系列錯(cuò)誤 // TODO: 在此處引用程序需要的其他頭文件
gotoxy().cpp
#include "stdafx.h" using namespace std; void gotoxy(short x, short y) { COORD pos = { x,y }; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hOut, pos); }
對(duì)中文字符的排序.cpp
//對(duì)中文字符串進(jìn)行排序時(shí),默認(rèn)是按照第一個(gè)字符的第一個(gè)拼音的順序進(jìn)行排序 #include "stdafx.h" using namespace std; void sort_by_name() { string s[4] = { "一號(hào)","二號(hào)","三號(hào)","四號(hào)" }, t; for (int i = 0; i<4; i++) { for (int j = i; j<4; j++) { if (s[i]>s[j]) { t = s[i]; s[i] = s[j]; s[j] = t; } } } for (int i = 0; i < 4; i++) { cout << s[i] << endl; } cout << "功能運(yùn)行結(jié)束!" << endl << endl; }
字符串轉(zhuǎn)換為數(shù)值型.cpp
#include "stdafx.h" using namespace std; int Strtoint_0(const char str[]) //字符串?dāng)?shù)字轉(zhuǎn)換為整形 { int i = 0, j = 0; long long number1 = 0; //定義一個(gè)長(zhǎng)整形變量,用來(lái)存儲(chǔ)轉(zhuǎn)換后得到的值 int number[50] = { 0 }; //定義一個(gè)數(shù)組,用來(lái)存儲(chǔ)轉(zhuǎn)換后得到的值 int symbol = 1; //符號(hào)常量,0為負(fù),1為正(默認(rèn)為正) while (str[i] != '\0') //測(cè)試輸出判斷是否正確 { while (str[i] == ' ') { i++; } if ((str[i] == '+' || str[i] == '-')) { i++; if (str[i] == '-') { symbol = 0; } } else if (str[i]<'9' && str[i]>'0') { number[j++] = str[i] - 48; //存儲(chǔ)數(shù)據(jù),j++ // cout << number[j - 1] << endl; i++; } if (str[i]>'9' || str[i]<'0') //停止輸出規(guī)則判斷語(yǔ)句 { break; } } cout << "數(shù)的位數(shù)為:" << j << endl; //j到這里就已經(jīng)得到數(shù)組的最大索引值+1了 int x = 1; for (int k = j - 1; k >= 0; k--, x = x * 10) { number1 += number[k] * x; } if (symbol == 0) { number1 = number1*(-1); } cout << "轉(zhuǎn)換后的數(shù)為:" << number1 << endl << endl; return 1; } int Strtoint() //調(diào)用字符轉(zhuǎn)換函數(shù),確保變量不在主函數(shù)中定義 { char arr[50] = { 0 }; int i = 0; char c; cout << "Please input the string :" << endl; while ((c = getchar()) != '\n') { arr[i++] = c; //注意這里下面的i就開(kāi)始++了 } /* while ((c = cin.get()) != '\n') //另一種控制輸入的方法 { arr[i++] = c; cout << arr[i - 1]; } */ Strtoint_0(arr); return 0; }
和我們的數(shù)據(jù)成員必須加extern不同的是,你只需把待調(diào)用函數(shù)的聲明寫(xiě)在其頭文件中,然后在主函數(shù)中直接用就可以
//test.h #ifndef TEST_H //注意,這里千萬(wàn)不要寫(xiě)成TEST.H,必須用下劃線(xiàn),用點(diǎn)不行 #define TEST_H void print(); #endif //test.cpp #include<iostream> #include"test.h" using namespace std; void print() { cout << "test函數(shù)被調(diào)用" << endl; } //main.cpp #include<iostream> #include"test.h" using namespace std; int main() { print(); }
使用extern的時(shí)候你甚至不需要在main.cpp文件中加上引用文件的聲明,直接就可以用。
#include<iostream> using namespace std; extern void print(); int main() { print(); }
但是這樣寫(xiě)其實(shí)作用不大,在一些大的工程中反而不如以好用。
讀到這里,這篇“C++一個(gè)函數(shù)怎么調(diào)用其他.cpp文件中的函數(shù)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。