最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++一個(gè)函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)

 更新時(shí)間:2023年02月23日 09:10:45   作者:Trivis Kylee  
這篇文章主要介紹了C++一個(gè)函數(shù)如何調(diào)用其他.cpp文件中的函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一個(gè)函數(shù)調(diào)用其他.cpp文件中的函數(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ù)值類型";
	gotoxy(15, 4); cout << "2:對(duì)中文字符進(jìn)行排序";
 
	gotoxy(0, 10);
	int choice = 0;
	cout << "請(qǐng)輸入您要執(zhí)行的功能:";
	cin >> choice;
	getchar();    //吸收回車
	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類型字符串的時(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;
}

在主文件cpp中調(diào)用其他文件函數(shù)的方法

直接用

和我們的數(shù)據(jù)成員必須加extern不同的是,你只需把待調(diào)用函數(shù)的聲明寫在其頭文件中,然后在主函數(shù)中直接用就可以

//test.h
#ifndef TEST_H //注意,這里千萬(wàn)不要寫成TEST.H,必須用下劃線,用點(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方法

使用extern的時(shí)候你甚至不需要在main.cpp文件中加上引用文件的聲明,直接就可以用。

#include<iostream>
using namespace std;
extern void print();
int main() {
	print();
}

但是這樣寫其實(shí)作用不大,在一些大的工程中反而不如以好用。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

始兴县| 长岛县| 饶阳县| 新河县| 阿克陶县| 宁乡县| 上高县| 商洛市| 靖江市| 叙永县| 绥滨县| 南召县| 湘乡市| 彭州市| 冷水江市| 柯坪县| 金昌市| 北辰区| 邢台县| 平乡县| 南皮县| 庄河市| 西贡区| 新化县| 和林格尔县| 河北区| 崇明县| 巧家县| 瑞昌市| 句容市| 名山县| 弥勒县| 神池县| 定安县| 壶关县| 凤山市| 建湖县| 璧山县| 八宿县| 宜兰市| 玉环县|