C艹+编程简介 函数原型与函数特征 数据声明与作用过 类、对象、构造函数与析构函数 输入输出 函数、参数传递与函数返回值 函数名重载与操作符重载 动态存储分配 友元函数与内联函数 结构、联合与类 模板类
1 ◼ 函数原型与函数特征 ◼ 数据声明与作用域 ◼ 类、对象、构造函数与析构函数 ◼ 输入/输出 ◼ 函数、参数传递与函数返回值 ◼ 函数名重载与操作符重载 ◼ 动态存储分配 ◼ 友元函数与内联函数 ◼ 结构、联合与类 ◼ 模板类
函数原型 下面的程序给出了典型的C程序结构,它是 Hel, world”程序的变型。 这个程序由三个文件组成: / File: hello.h */ char *helloo; / File: hello. c # include<sdoh>包括 sprintf()的原型* # include< stdlib. h>/包括malo(的原型*/ # include< string. h>/包括 strlen()的原型* # include" hello.h"/包括hell0)的原型*2
2 函数原型 ◼ 下面的程序给出了典型的C 程序结构,它是 “Hello, world”程序的变型。 ◼ 这个程序由三个文件组成: /* File: hello.h */ char *hello( ); /* File: hello.c */ # include <stdio.h> /*包括sprintf ( )的原型*/ # include <stdlib.h> /*包括malloc( )的原型*/ # include <string.h> /*包括strlen( )的原型*/ # include "hello.h" /*包括hello( )的原型*/
char hello(name) char name char *value;/*返回串" Hello,name.".* value =(char *)(malloc(9+strlen(name)); sprintf (value, Hello OS name return value, File: main.c r/ # include< stdio. h>/包括 printf()的原型*/ # include" hello.h"/包括hell0)的原型*/
3 char *hello(name) char *name; { char *value; /*返回串 "Hello, name.". */ value = (char *)(malloc(9+strlen(name)); sprintf (value, "Hello, %s.", name); return value; } /* File: main.c */ # include <stdio.h> /*包括printf ( )的原型*/ # include "hello.h" /*包括hello( )的原型*/
main(argc, argv) int argc, char argyl i printf(" %s", hello("world)); 头文件名字的后缀用“h”表示,程序文件 名字的后缀用“c”表示。 hel.h:包含heo函数的原型。main函数 可通过“# include”定向到该原型的定义文 件,取得对原型的访问性
4 main(argc, argv) int argc; char *argv[ ]; { printf("%s", hello("world")); } ◼ 头文件名字的后缀用“.h”表示,程序文件 名字的后缀用“.c”表示。 ◼ hello.h:包含 hello函数的原型。main函数 可通过“# include”定向到该原型的定义文 件,取得对原型的访问性
hello.c:这是hel函数的定义文件。它通过 个 string类型的形式参数接受需要打印的 串,返回一个 sstring类型的值作为打印串。 返回类型必须与在#ncud定向的“h文件 中所给出的原型的类型匹配。 man. c:这是打印“Hell,word”的主程序 它构造和返回一个欢迎词字符串,其结果通 过函数prin打印出来。 C把函数和数据定义放在后缀为“c”的代 码文件中。在各代码文件中使用后缀为“h” 的 include文件,定义对其它各模块的调用接
5 ◼ hello.c:这是hello函数的定义文件。它通过 一个string类型的形式参数接受需要打印的 串, 返回一个string类型的值作为打印串。 返回类型必须与在#include定向的“.h”文件 中所给出的原型的类型匹配。 ◼ main.c:这是打印“Hello, world”的主程序, 它构造和返回一个欢迎词字符串,其结果通 过函数printf打印出来。 ◼ C 把函数和数据定义放在后缀为“.c”的代 码文件中。在各代码文件中使用后缀为“.h” 的include文件, 定义对其它各模块的调用接 口