C++一個函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)
一個函數(shù)調(diào)用其他.cpp文件中的函數(shù)
使用VC或VS創(chuàng)建C++項目的時候,會自動產(chǎn)生許多文件夾,其中有一個文件夾->源文件:
在該文件下可以自定義許多.cpp文件,但是需要注意的是這里面的各個文件只能有一個文件中含有main()函數(shù),
而且各個文件中不能使用相同的函數(shù)名進行定義;
那么要那么多文件放在項目中有什么用呢?
當然這里C++是提供一個文件調(diào)用其他文件中函數(shù)的功能的,
這就可以讓我們自定義一個只包含main()函數(shù)的文件,通過在該函數(shù)中調(diào)用其他文件中的函數(shù)就可以將各個文件鏈接起來,
而且更重要的一點就是,通過調(diào)用其他,cpp文件中的函數(shù)的時候,如果調(diào)用的某函數(shù)又調(diào)用了它自己文件中的一個函數(shù),
那么只用調(diào)用“父級函數(shù)”就可以實現(xiàn)間接調(diào)用~~~
看示例
首先是資源管理窗口:

功能主函數(shù).cpp
// C++上機作業(yè).cpp : 定義控制臺應用程序的入口點。
//'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ù)值類型";
gotoxy(15, 4); cout << "2:對中文字符進行排序";
gotoxy(0, 10);
int choice = 0;
cout << "請輸入您要執(zhí)行的功能:";
cin >> choice;
getchar(); //吸收回車
switch (choice)
{
case 1:
Strtoint();
break;
case 2:
sort_by_name();
break;
default:
cout << "選擇失敗,感謝使用,再見!" << endl << endl;
}
return 0;
}stdafx.h(stdandard application framework extensions)
// stdafx.h : 標準系統(tǒng)包含文件的包含文件, // 或是經(jīng)常使用但不常更改的 // 特定于項目的包含文件 // #pragma once #include "targetver.h" #include <stdio.h> #include <tchar.h> #include <iostream> #include <Windows.h> #include <string> //注意這里的string與cstring中的使用差別,在定義與使用cout輸出string類型字符串的時候,最好使用string庫,否則可能會出現(xiàn)亂碼以及錯誤等一系列錯誤 // 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);
}對中文字符的排序.cpp
//對中文字符串進行排序時,默認是按照第一個字符的第一個拼音的順序進行排序
#include "stdafx.h"
using namespace std;
void sort_by_name()
{
string s[4] = { "一號","二號","三號","四號" }, 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 << "功能運行結(jié)束!" << endl << endl;
}字符串轉(zhuǎn)換為數(shù)值型.cpp
#include "stdafx.h"
using namespace std;
int Strtoint_0(const char str[]) //字符串數(shù)字轉(zhuǎn)換為整形
{
int i = 0, j = 0;
long long number1 = 0; //定義一個長整形變量,用來存儲轉(zhuǎn)換后得到的值
int number[50] = { 0 }; //定義一個數(shù)組,用來存儲轉(zhuǎn)換后得到的值
int symbol = 1; //符號常量,0為負,1為正(默認為正)
while (str[i] != '\0') //測試輸出判斷是否正確
{
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; //存儲數(shù)據(jù),j++
// cout << number[j - 1] << endl;
i++;
}
if (str[i]>'9' || str[i]<'0') //停止輸出規(guī)則判斷語句
{
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就開始++了
}
/*
while ((c = cin.get()) != '\n') //另一種控制輸入的方法
{
arr[i++] = c;
cout << arr[i - 1];
}
*/
Strtoint_0(arr);
return 0;
}在主文件cpp中調(diào)用其他文件函數(shù)的方法
直接用
和我們的數(shù)據(jù)成員必須加extern不同的是,你只需把待調(diào)用函數(shù)的聲明寫在其頭文件中,然后在主函數(shù)中直接用就可以
//test.h
#ifndef TEST_H //注意,這里千萬不要寫成TEST.H,必須用下劃線,用點不行
#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方法
使用extern的時候你甚至不需要在main.cpp文件中加上引用文件的聲明,直接就可以用。
#include<iostream>
using namespace std;
extern void print();
int main() {
print();
}但是這樣寫其實作用不大,在一些大的工程中反而不如以好用。
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Visual Studio2022+QT6創(chuàng)建桌面應用實現(xiàn)
本文主要介紹了Visual Studio2022+QT6創(chuàng)建桌面應用實現(xiàn),文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-02-02
對for循環(huán)中表達式和循環(huán)體的執(zhí)行順序詳解
今天小編就為大家分享一篇對for循環(huán)中表達式和循環(huán)體的執(zhí)行順序詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

