_ | 覦覈襦 | 豕蠏手 | 殊螳 | 譯殊碁
FrontPage › Cpp蠍一

Contents

1
2 2谿 覦一伎 誤一 蠏
3 File /豢
4 襷り覲
5 Function overloading
6 inline function
7 覃覈襴
8 覦一願骸 main襷り覲


1 #

  • 覲: a, 讌覲: a 蟆曙 一一 ::襯 覃 覲 蠏 .
  • 襦るジ 殊 螳 企 覲 static朱 誤
  • るジ 殊 覲襯 (#include "file1.c", file.c 覲 a襯 file2.c ) extern 朱

2 2谿 覦一伎 誤一 蠏 #

#include <iostream>
using namespace std;

int main()
{
	int arr[2][3] = {{1,2,3},{4,5,6}};

	for(int i=0; i < sizeof(arr)/sizeof(*(arr+1)); i++)
	{
		for(int j=0; j < sizeof(*(arr+1))/sizeof(int);j++)
		{
			cout << **(arr+i)+j << endl;
		}
	}
	/*
	int* pArr = &arr[0][0];

	for(int i = 0; i < sizeof(arr)/sizeof(int);i++)
	{
		cout << *(pArr+i);
	}
	*/
	return 0;
}

//語襦 蠍郁鍵
#include <iostream>
using namespace std;

void show(int (*p)[3], int n)
{
	for(int i=0; i < n/sizeof(*p); i++)
	{
		for(int j=0;j < sizeof(*p)/sizeof(int); j++)
			cout << *(*(p+i))+j << endl;
	}
}

int main()
{
	int arr[2][3] = {{1,2,3},{4,5,6}};
	show(arr, sizeof(arr));
	
	return 0;
}

3 File /豢 #

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	//
	ofstream outFile("c:\\my.txt", ios::out);

	if(!outFile)
	{
		cerr << "cannot open my.txt" << endl;
		return 1;
	}

	int n = 50;
	float f = 20.3f;

	outFile << "n:" << n << endl;
	outFile << "f:" << f << endl;

	outFile.close();

	//豢
	ifstream inFile;
	char s[80];

	inFile.open("c:\\my.txt", ios::in);

	if(!inFile)
	{
		cerr << "cannot open my.txt" << endl;
		return 1;
	}

	while (!inFile.eof())
	{
		inFile >> s;
		cout << s << endl;
	}

	inFile.close();
	return 0;
}

4 襷り覲 #

  • Call by value
  • Call by Reference

5 Function overloading #

企 螳螻, 蠏碁豌螳 るジ 螳 譟伎螳
  • int max(int, int)
  • int max(int, int, int)
  • int max(float, int)

6 inline function #

inline int sum(a int, b int) { return a+b; }

企蟆 inline れ襯 覃 i = sum(10, 20); 覓語レ i = 10 + 20; 朱 豌企. 襦蠏碁 襯 語螻 語襯 覲旧 るる襯 る.

7 覃覈襴 #

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    // 覃覈襴  
    int *jp = new int[10];
    
    // 螳豌企ゼ 燕  朱 0 覦 
    if(jp == 0) cerr << "memory not allocated!" << endl;
    
    cout << "jp: " << jp << endl;
    cout << "jp[0]: " << &jp[0] << endl;
    
    if (jp == &jp[0])
       cout << "譯殊螳 螳" << endl;
    else
       cout << "譯殊螳 るゴ" << endl; 
    
    delete []jp; //覦一  覦 []襯 覿譴 . 
    
    system("PAUSE");
    return EXIT_SUCCESS;
}

8 覦一願骸 main襷り覲 #

#include <stdio.h>
#include <stdlib.h> //atoi  
#include <string.h> //memset  

int main(int argc,char* argv[])
{
	//語螳 伎 
	if(argc <2)
	{
		printf("Usage : 1_4_2 [integer]\n");
		return 0;
	}

	//譯殊伎 語 2覲企   .

	int n = atoi(argv[1]);
	if(n<2)
	{
		printf("Error : n must be greater than 1\n");
		return 0;
	}
	
	int *parray
	int i, j;

	//覃覈襴 
	parray = new int [n+1];
	if(parray == 0)
	{
		printf("Error : memory allocation failed\n");
		return 0;
	}
	//覃覈襴襯 0朱 豐蠍壱
	memset(parray,0,sizeof(int)*(n+1));
	for(i=2;i<=n;i++)
	{
		if(parray[i] == 1)
			continue;
		j=i; //i襯 蠍一朱
		while((j+=i)<=n)
		{
			parray[j] = 1; //i 覦一れ 覈 螳 手 襷
		}
	}

	// 豢
	for(i=2;i<=n;i++)
	{
		if(parray[i] == 0) printf("%d ",i);
	}
	printf("\n");

	//豪 覃覈襴 豌企ゼ 伎
	delete [] parray;
	return 0;


}
蠍 蠍郁鍵..
企: : るジ讓曙 襦螻豺 企Ν 譯殊語. 襦螻豺
EditText : Print : Mobile : FindPage : DeletePage : LikePages : Powered by MoniWiki : Last modified 2018-04-13 23:12:52

螳讌襦 襯 觸願鍵 覲企 觜襦 覲願鍵 覲狩讌襷 訖襴襯 企Μ蠍 磯 螳 .