1 / 30

第 2 章 C++ 基础

第 2 章 C++ 基础. 2.1 C++ 的产生和特点.   2.1.1 C++ 的产生 C++ 是美国贝尔实验室于 1980 年开发出来的一种过程性与面向对象性结合的程序设计语言。最初他把这种新的语言叫做“含类的 C”, 到 1983 年才取名为 C++ 。. 2.1.2 C++ 的特点 (1) C++ 保持与 C 兼容。 (2) 用 C++ 编写的程序可读性更好 , 代码结构更为合理 , 可直接地在程序中映射问题空间的结构。 (3) 生成代码的质量高 , 运行效率仅比汇编语言代码段慢 10% 到 20% 。

tamma
Download Presentation

第 2 章 C++ 基础

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第2章 C++基础

  2. 2.1 C++的产生和特点   2.1.1 C++的产生 C++是美国贝尔实验室于1980年开发出来的一种过程性与面向对象性结合的程序设计语言。最初他把这种新的语言叫做“含类的C”,到1983年才取名为C++。

  3. 2.1.2 C++的特点 (1) C++保持与C兼容。 (2) 用C++编写的程序可读性更好,代码结构更为合理,可直接地在程序中映射问题空间的结构。 (3) 生成代码的质量高,运行效率仅比汇编语言代码段慢10%到20%。 (4) 从开发时间、费用到形成的软件的可重用性、可扩充性、可维护性和可靠性等方面有了很大的提高,使得大中型的程序开发项目变得容易的多。 (5) 支持面向对象的机制,可方便地构造出模拟现实问题的实体和操作。

  4. 2.2 C++程序的结构特性 2.2.1 一个简单的C++示例程序

  5. 例2.1 两数相加 // sum.cpp #include<iostream.h> int add(int a, int b); // 函数原型说明 int main() // 主函数 { int x, y, sum; // 定义三个整型变量 cout << Enter two numbers: <<\n; // 界面:提示用户输入两个数 cin>>x; // 从键盘输入变量x的值 cin>>y; // 从键盘输入变量y的值 sum=add(x, y); // 调用函数add,将得到的值赋给变量sum cout<<The sum is:<<sum<<\n; // 输出两个数的和sum的值 return 0 } int add(int a,int b) // 定义add函数,函数值为整型 { int c; // 定义一个整型变量 c=a+b; // 计算两个数的和 return c; // 将c的值返回,通过add带回调用处 }

  6. 2.2.2 C++程序的结构特性 类的声明部分 面向对象程序 类的使用部分

  7. 例2.2 典型的C++程序结构 #include<iostream.h> // 类的声明部分 class A{ int x,y,z; //类A的数据成员声明 … fun(){ … } //类A的成员函数声明 … }; // 类的使用部分 int main() { A a; // 创建一个类A的对象a … a.fun(); // 给对象a发消息,调用成员函数fun() return 0; }

  8. 2.2.3 C++程序的编辑、编译和运行 C源程序文件扩展名为.C,而C++源程序文件扩展名为.CPP。 在DOS下,C++程序的编辑、编译及运行方法和过程与C语言基本一样,如Turbo C++或Borland C++都有带C和C++两种编辑器,当源程序文件扩展名为.C时,启动C编译器,当源程序文件扩展名为.CPP时启动C++编译器。 在Windos下,我们常用Visual C++开发环境,来编辑、编译和运行C++程序。

  9. 2.3 C++在非面向对象方面的扩充 2.3.1 注释与续行 以下两条语句是等价的: x=y+z; /* This is a comment */ x=y+z; //This is a comment

  10. 2.3.2 输入输出流 例2.3 一个完整的C++程序。 #incluade <iostream.h> int main() { char name[20]; cout <<"Hello,your name:"; cin >>name; cout<<name; return 0; }

  11. 2.3.3 灵活的变量说明 float fun(int x,int y) // 对形参直接进行说明 { for (int i=0;i<10;i++) // 对循环变量i进行说明 { int sum=0; // 循环体内也可对变量sum进行说明 sum=sum+i; cout<<"sum="<<sum; } int z=0; // 使用变量z时才说明它 z=x+y; }

  12. 2.3.4 结构、联合和枚举名 例如: enum boole{FALSE,TRUE}; struct string{ char * str; int length; }; union number{ int i; float f; }; C++在中, 定义变量时,可以说明为: boole done; string str; number x;

  13. 2.3.5 函数原型 例2.5 函数原型的说明。 #include <iostream.h> void write(char *s); // 函数原型的说明 void main() { write("Hello,world!");} void write(char *s) { cont<<s; }

  14. 2.3.6 const修饰符 例2.6 #define的不安全性 #include<iostream.h> main() { int a=1; #define T1 a+a #define T2 T1-T1 cout <<"T2 is "<<T2<<endl; return 0; }

  15. 例2.7 用const取代#define。 #include<iostream.h> int main() { int a=1; const T1=a+a; const T2=T1-T1; cout <<"T2 is"<<T2<<endl; return 0; } 输出:T2 is 0

  16. (1) 指向常量的指针是指:一个指向常量的指针变量。例如: const char* pc="abcd"; // 声明指向常量的指针 (2) 常指针是指:把指针本身,而不是它指向的对象声明为常量。例如: char* const pc="abcd"; // 常指针 (3) 指向常量的常指针是指:这个指针本身不能改变,它所指向的值也不能改变。要声明一个指向常量的常指针,二者都要声明为const。例如: const char* const pc="abcd";//指向常量的常指针

  17. 2.3.7 void型指针 例2.8 void型指针的使用。 #include<iostream.h> main() { void* pc; int i=456; char c=a; pc=&i; cout<<*(int* )pc<<endl; pc=&c; cout<<*(char*)pc<<endl; return 0; }

  18. 2.3.8 内联函数 例2.9 内联函数的使用 #include <iostream.h> inline double circle(double r) // 内联函数 { return 3.1416*r*r; } int main() { for (int i=1;i<=3;i++) cout<<"r="<<i<<" area= "<<circle(i)<<endl; return 0; }

  19. 2.3.9 带有缺省参数值的函数 例如有一函数原型说明为: int init(int x=5,int y=10); 则x与y的缺省值分别为5与10。 以下的函数调用都是允许的: init(100,80); // x=100,y=80 init(25); // x=25,y=10 init(); // x=5,y=10

  20. 2.3.10 函数重载 在C++中, 只要函数参数的类型不同,或者参数的个数不同,或者二者兼而有之,两个或者两个以上的函数可以使用相同的函数名。

  21. 例2.13 参数类型不同的重载函数 #include<iostream.h> int cube(int i) { return i*i*i; } float cube(float f) { return f*f*f; } double cube(double d) { return d*d*d;} int main() { int i=12; float f=3.4; double d=5.67; cout<<i<<*<<i<<*<<i<<=<<cube(i)<<endl; cout<<f<<*<<f<<*<<f<<=<<cube(f)<<endl; cout<<d<<*<<d<<*<<d<<=<<cube(d)<<endl; return 0; }

  22. 2.3.11 作用域标识符∷ 例2.16 使用作用域标识符的情况。 #include<iostream.h> int avar; main() { int avar; avar=25; // 局部变量avar ::avar=10; // 全局变量avar cout<<"local avar ="<<avar<<endl; cout<<"global avar ="<<::avar<<endl; return 0; } 程序运行结果如下: local avar=25 global avar=10

  23. 2.3.12 无名联合 union { int i; float f; } 在此无名联合中,声明了变量i和f具有相同的存储地址。

  24. 2.3.13 强制类型转换 在C中如果要把一个整数(int)转换为浮点数(float), 要求使用如下的格式: int i=10; float x=(float)i; C++支持这样的格式,但上面的语句可改写成: int i=10; float x=float(i);

  25. 2.3.14 new和delete 运算符new用于内存分配的最基本的语法形式为: 指针变量 = new 类型名; 运算符delete用于释放new分配的存储空间。其基本的语法形式为: delete 指针变量;

  26. 例2.17 操作符new和delete的使用。 #include <iostream.h> main() { int *p; // 声明一个整型指针变量p p=new int; // 动态分配一个存放int型数据的内存 空间,并将首地址赋给p *p=10; cout<<*p; delete p; // 释放指针变量p指向的内存空间 return 0; }

  27. 2.3.15 引用 1. 引用的概念 引用通常被认为是某个变量的别名,声明一个引用的格式如下: 数据类型 & 引用名 = 已定义的变量名; 例如: int i=5; int& j=i; 这里,j是一个整数类型的引用,用整型变量i对它进行初始化,这时j就可看作是变量i的别名 。

  28. 例2.22 引用的使用。 #include <iostream.h> void main() { int i; int &j=i; i=30; cout<<"i="<<i<<"j="<<j<<"\n"; j=80; cout<<"i="<<i<<"j="<<j<<"\n"; cout<<"Address of i"<<&i<<"\n"; cout <<"Address of j"<<&j<<"\n"; }

  29. 2. 引用作函数参数 例2.25 采用“引用参数”传递函数参数 #include <iostream.h> void swap(int& m,int& n) { int temp; temp=m; m=n; n=temp; } main() { int a=5,b=10; cout<<"a="<<a<<" b="<<b<<endl; swap(a,b); cout<<"a="<<a<<" b="<<b<<endl; return 0; }

  30. 3. 用引用返回值 例2.27 用引用返回函数的值。 #include<iostream.h> int A[10]; int& array(int i); void main() { int i,number; A[0]=0; A[1]=1; cin>>number; for (i=2;i<number;i++) { array(i)=array(i-2)+array(i-1); cout<<"array("<<i<<")="<<array(i)<<endl; } } int& array(int i) { return A[i]; }

More Related