1.输出
c 使用printf 向终端输出信息
c++ 提供标准输出流
#include<iostream>
using namespace std;
int main(){
cout << "hello c++" << endl;
return 0;
}
2.函数符号兼容
如果需要在c++中调用实现的库中的方法
extern "C" // 指示编译器这部分代码使用C的方式编译而不是C++
例如: void func(int x,int y);
c 编译器将函数名编译为 func ,则c++ 编译器 编译成funcii 之类的名称 原因在于c 没有 函数重载,而c++有
gcc main.c -o mainc.o
gcc main.cpp -o maincpp.o
nm -A mainc.o
nm -A maincpp.o
因此 对于C 库函数可以
#ifndef __cplusplus
extern "C"{
#endif
void func(int x,int y)
#ifdef __cplusplus
}
#endif
3.引用
引用是c++ 定义的一种新的数据类型
void change(int &i){
i = 10;
}
int i = 1;
change(i);
printf("%d\n",i);
4.命名空间
namespace 相当于 package
namespace A{
void a(){}
}
A::a()
namespace A{
namespace B{
void a() {};
}
}
5.字符串
c字符串
char str1[] = {'h','e','l','l','o','\n'};
char str2[] = "Hello";
函数 | 概述 |
---|
strcpy(s1,s2) | f复制字符串s2到字符串s1 |
strcat(s1,s2) | 连接字符串s2到字符串s1的末尾 |
strlen(s1) | 返回字符串s1的长度 |
strcmp(s1,s2) | 如果s1和s2相同,返回0; s1 < s2 返回小于0,s1 >s2 返回大于0 |
strchr(s1,ch) | 返回指向s1 中ch第一次出现的位置指针 |
strstr(s1,s2) | 返回指向字符串s1中字符串s2的第一次出现的指针位置 |
c++string 类
#include<string>
using namespace std;
string str1 = "hello";
string str2 = "world";
string str3("hello");
string str4(str3);
string str5 = str1 + str2;
str1.append(str2);
const char *s1 = str1.c_str();
str1.size();
str1.empty()
6.类
class Student{
int i;
public:
Student(int i,int j,int k):i(i),j(j),k(k){};
~Student(){};
private:
int j;
protected:
int k;
};
7.常量函数
函数后写上 const 表示不会 也不允许修改类中的成员
class Student{
int i;
public:
Student(){}
~Sudent(){}
void setName(char * _name) const{
name = _name;
}
private:
int j;
char *name;
protected:
int k;
};
8.友元
1.类的友元函数是定义在类外部,但是有权访问 所有私有(private)成员 和保护(protected) 成员
2.友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类,这种情况下整个类及其所有成员都是友元.
class Student{
int i;
public:
Student(){}
~Student(){}
void setName(char * _name){
name = _name;
}
friend void printName(Student *student);
private:
int j;
char *name;
protected:
int k;
};
void printName(Student *student){
cout << student ->name << endl;
}
Student *student = new Student;
student ->setName("Tom");
printName(student);
class Student{
public:
Studen(){}
~Student(){}
friend class Teacher;
private:
int j;
char *name;
};
class Teacher{
public:
void call(Student *student){
}
};
9.静态成员
1. 可以用static 来声明类成员为静态
2. 使用静态成员属性或者函数 需要使用 域运算符::
#ifdef INSTANCE_H
#define INSTANCE_H
class Instance{
public:
static Instance *getInstance();
private:
static Instance *instance;
};
#endif
#include "Instance.h"
Instance *Instance::instance = 0;
Instance* Instance::getInstance(){
if(!instance){
instance = new Instance;
}
return instance;
}
10.函数重载
void print(int i){
cout << "int:" << i << endl;
}
void print(double f){
cout << "double:" << f << endl;
}
11.操作符重载
1.c++ 允许重定义大部分的内置运算符
2.函数名是由关键字 operator 和 其后要重载的运算符符号构成的
3.重载运算可被定义为普通的非成员函数或者被定义为类成员函数
class Test1{
public:
Test1(){}
Test1 operator+(const Test& t1){
Test1 t;
t.i = this ->i + t1.i;
return t;
}
Test1(const Test1& t){
this ->i = t.i;
cout << "copy" << endl;
}
int i;
};
Test1 t1;
Test1 t2;
t1.i = 100;
t2.i = 200;
Test1 t3 = t1 + t2;
cout << t3.i << endl;
class Test2{
public:
int i;
};
Test2 operator+(const Test2& t21,const Test2& t22){
Test2 t;
t.i = t21.i + t22.i;
return t;
}
Test2 t21;
Test2 t22;
t21.i = 100;
t22.i = 200;
Test2 t23 = t21 + t22;
cout << t23.i << endl;
12.继承
方式 | 说明 |
---|
public | 基类的public、protected成员也是派生类相应的成员,基类的private成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问 |
protected | 基类的公有和保护成员将成为派生类的保护成员 |
private | 基类的公有和保护成员将成为派生类的私有成员 |
class Parent{
public:
void test(){
cout << "parent" << endl;
}
};
class Child: Parnt{
public:
void test(){
Parent::test();
cout << "child" << endl;
}
}
多继承:
class<派生类名>:<继承方式1><>