面向对象编程思想
面向对象编程是一种以对象为中心的程序设计范型。也可以说是一套概念和想法,是一种用计算机程序来描述实际问题的思路。是一种新的思维和编程方法。
面向对象编程思想的三大特征
封装性、继承性、多态性
封装性
把对象的属性和功能结合成一个独立的系统单位。 尽可能隐蔽对象的内部细节。对外形成一道屏障)只保留有限的对外接口使之与外部发生联系。
封装在现实中的例子
人们使用录音机,只要知道键的功能即可(放音、录音、停止、倒带等),不需要知道这些功能是如何实现的,也不需要知道其内部电路或构造。
继承性
继承对于软件代码的重复利用有着重要意义,是面向对象技术能够提高软件开发效率的重要原因之一。
定义:如果类的对象A拥另一个类B的全部属性与服务,称作类A对类B的继承。
多态性
多态是指在一般类中定义的属性或行为,被特殊类继承之后,可以具有不同的数据类型或表现出不同的行为。这使得同一个属性或行为在一般类及其各个特殊类中具有不同的语义。
* 例如:数的加法->实数的加法 ->复数的加法
C++语言支持两种多态性 :编译时多态性和运行时多态性 C++中,编译时多态是通过重载实现的,而运行时多态是通过虚函数实现的。
重载的概念
重载是指用一个标识符(符号)来实现多个功能或行为的现象。 重载包括函数重载和运算符重载
函数重载 :使用同一个名字的多个函数定义。
运算符重载 :运算符号使用的重新定义。实质同函数重载
输入输出
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
char str[] = "hello";
cout << str << endl;
cout << "world";
cout << "world\n";
//结果为无符号整形(有符号转无符号)
unsigned int a = 100;
int b = -400;
cout << a+b << endl;
int num = 97;
cout << dec << num << endl;//以10进制输出
cout << hex << num << endl;//以16进制输出
cout << oct << num << endl;//以8进制输出
//setw(n) n 表示宽度,用数字表示。
//当后面紧跟着的输出字段长度小于 n 的时候,在该字段前面用空格补齐
cout << setw(5) << num << setw(5) << num << endl;
int x,y;
cin >>x >> y;
cout << x << " : " << y <<endl;
return 0;
}
C++库函数
在C++中使用这些 头文件有两种方法。
1、用C语言的传统方法
#include<stdio.h>
#include<math.h>
2、用C++的新方法
C++标准要求系统提供的头文件不包括后缀.h,例如iostream、string。 为了表示与C 语言的头文件有联系又有区别,C++所用的头文件名是在C语言的相应的头文件名之前加一字母c。 因为标准库非常的庞大,所以程序员在选择的类的名称或函数名时就很有可能和标准库中的某个名字相同。 为了避免名字冲突,98年以后的c++语言提供一个全局的命名空间namespace就把标准库中的一切都被放在名字空间std中,因此在程序中要对命名空间std作声明。
namespace命名空间
在C++中,名称(name)可以是: 符号常量、变量、宏、函数、结构、枚举、类和对象
为了避免,在大规模程序的设计中,以及在程序员使用各种各样的C++库时,这些标识符的命名发生冲突,标准C++引入了关键字namespace
namespace一般被译为:命名空间/名字空间/名称空间/名域 命名空间(namespace)是一种描述逻辑分组的机制,可以将按某些标准在逻辑上属于同一个集团的声明放在同一个命名空间中。命名空间可以是全局的,也可以位于另一个命名空间之中,但是不能位于类和代码块中。
定义: 一般自定义格式如下
namespace xxx
{
//作用域;
}
注意:命名空间不能单独在函数内部定义
命名空间要在调用位置之前声明
命名空间可以分段定义,同一命名空间可以合并;
#include <iostream>
using namespace std;
namespace xxx
{
int name;
void fun()
{
cout << "fun" << endl;
}
namespace yyy
{
int num;
}
}
int main(int argc,char *argv[])
{
// 不能位于类和代码块中
// 编译错误 error: ‘namespace’ definition is not allowed here
namespace kkk
{
int num;
}
//xxx::yyy::num = 10;
//cout << xxx::yyy::num << endl;
// 用法2:
//int name = 1111;
// 通常这种方式较为少用
// 因为一旦声明后,通常命名空间的作用域可以省略,这个时候可能会与其他作用域的内容出现混淆
// 一般我们常用的还是用法1,直接指明其命名空间(主要针对自定义命名空间)。
//name = 1000;
//cout << name << endl;
// 用法1:
//int name = 111;
//std::cout << "hello" << endl;
当我们自定义了一个命名空间时,通常我们在使用这个命名空间中的内容时需要通过命名空间来引用
:: 是作用域,用于指定其从属关系
//xxx::name = 10;
//cout << xxx::name << endl;
//xxx::fun();
//cout << name << endl;
return 0;
}
智能字符串string
C++的string类型是对C语言的字符串的极大提升,string类型是智能对象,能自动分配所需的内存空间,并且提供针对字符串的几乎所有操作。
所需要的头文件:<string>
所需声明的名字空间:using std::string;
比起C风格字符串来,特有的操作有:
- 自动分配字符串内存空间
- 双向遍历字符串迭代器
- 清空字符串内容
- 累加、追加、插入、交换等操作
1、string初始化、赋值
string str;
string str1("hello");
string str2(str1);
string str3 = str1;
cout << str << " - " << str1 << endl;
//printf("%s\n", str1); error
cout << str2 << endl;
cout << str3 << endl;
//赋值
string str4;
str4 = str2;
// str4(str1); error
cout << str4 << endl;
2、str.c_str()、string 与 char*的转换
string str("12345676");
char* ptr = (char*)str.c_str();
char* ptr = const_cast<char*>(str.c_str());
cout << ptr << endl;
3、string的输入
string str;
cin >> str;
cout << str << endl;
4、capacity
5、size、length
6、max_size
string str("123456789");
cout << str.capacity() << endl;
cout << str.size() << endl;
cout << str.length() << endl;
cout << str.max_size() << endl;
7、clear
str.clear();
cout << str.size() << endl;
8、empty
为空返回真,不为空返回非真
str = "123";
cout << str.empty() << endl;
string s("So, you like donuts, eh? Well, have all the donuts all in the world!");
cout << s << endl;
9、erase
从索引为4的位置开始删除,总共删除8个字符
s.erase(4, 8);
12、find
从下标索引30位置开始向后查找
cout << s.find("all", 30) << endl;
10、substr
从索引为10的位置返回5个字符
cout << s.substr(10, 5) << endl;
11、swap
交换两个变量的值
string s1("123123");
s1.swap(s);
cout << s << endl;
cout << s1 << endl;
13、replace
从下标为5的位置,将后续的6个字符进行替换
cout << s.replace(5, 6, "AAAAAAAAAAAAAAAAAAAAAAA") << endl;
14、字符串连接
15、字符串比较
string s1("abcdefg");
string s2("abcdefge");
cout << (s1 < s2) << endl;
cout << (s1 + s2) << endl;
// cout << (s1 - s2) << endl; //error error: no match for ‘operator-’
struct结构体
#include <stdio.h>
//#include <iostream>
//using namespace std;
#if 0
// C语言中的struct与C++中的struct区别。
// C++中的结构体和C++中的类的区别。(默认访问属性不同)
typedef class Node
{
// 2、C语言中结构体不允许有函数成员
// C++可以。
void fun() { printf("fun!\n"); }
int num;
}NODE;
int main(int argc,char *argv[])
{
// 1、这种写法在C语言中是错误的
// 在C++是正确的。
//Node n;
NODE n;
n.fun();
//n.num = 111;
//printf("%d\n", n.num);
return 0;
}
#endif
struct Node
//class Node
{
int max(int a, int b);
};
// 如果C++中的结构体声明了一个函数
// 函数定义在结构体外部时,只需要在函数名前添加作用域即可。
int Node::max(int a, int b)
{
return a > b ? a : b;
}
int main()
{
Node n;
n.max(12, 10);
return 0;
}