C++ 引入命名空间
#include<iostream>
//引入整个命名空间
//using namespace std;
//引入部分命名空间
//using std::cout;
//using std::endl;
int main(){
//用到时引入
std::cout << "hhh" << std::endl;
return 0;
}
C++ bool类型
int main(){
bool a = true;
a = false;
cout << a + 1 << endl;
//结果输出 1
//c++ 中bool变量 true为 1 false为 0
return 0;
}
const关键字
int main(){
//const 使变量变为只读 read-only
int a = 10;
const int *p = &a;//使p指向的地址内容为只读
int const *q = &a;// 与上面相同
int * const r = &a;// 使r的指向为只读
*r = 20;
cout << "a = " << a << endl;
return 0;
}
c++ 字符串
#include<iostream>
#include<string>
#include<cstring> //在c++中使用c的库
using std::cout;
using std::endl;
using std::cin;
using std::string;
int main(){
//c中使用字符串
char str[10] = "world";
char *a = str;
strcat(a, "123"); //c中拼接字符串
cout << a << endl;
//c++中使用字符串
string s = "hello";
//将c风格的字符串赋值给c++中的字符串对象
s = a;
s += "456"; //c++中拼接字符串
const char *p = s.c_str(); //将c++中的字符串对象转换成char指针
cout << s << endl;
cout << p << endl;
return 0;
}
c++ 引用
//使用引用,进行函数传值
void fun(int &ref){
ref = 300;
}
int main(){
int a = 10;
int b = 90;
int &ref = a;//声明引用,代表 ref为 a的一个别名 , ref 和 a 同时代表了申请的那段空间
//引用关系不能更改
// int &ref = b;
ref = 30;
fun(ref);
cout << a << endl;
return 0;
}
c++ new 和 delete
struct Student{
char *name;
int id;
int age;
};
int main(){
struct Student s;
//c中申请堆区内存
struct Student *p = (struct Student *)malloc(sizeof(struct Student));
s.name = "hello";
p -> id = 1001;
cout<< s.name <<endl;
free(p);
p = NULL;
//c++中申请堆区内存
struct Student *q = new Student();
q -> name = "world";
cout<< q -> name <<endl;
delete q;
return 0;
}
c++ 异常机制
int mydiv(int a, int b){
if (b == 0){
throw b;
} else {
return a / b;
}
}
int main(){
int a = 10, b = 5;
b = 2;
try{
int res = mydiv(a, b);
cout<< "res = " << res <<endl;
} catch(int b){
cout<< "b = " << b <<endl;
} catch(...){
cout<< "other error happened" <<endl;
}
return 0;
}
c++ vector容器
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<char> vec_char(0);//定义一个空的vector容器 “(0)”也可以不写
vec_char.push_back('a');//往容器的最后添加数据
vec_char.push_back('b');
vec_char.push_back('b');
cout << (vec_char.empty() ? "empty" : "not empty") << endl;
for (int i = 0; i < vec_char.size(); i++){
cout << vec_char.at(i) << endl;// 也可以使用 vec_char[i] ,使用at方法可以检测下标越界
}
return 0;
}
c++ list容器
#include<iostream>
#include<list>
using namespace std;
int main(){
list<int> lt_int;
lt_int.push_back(10);//在尾部插入数据
lt_int.push_back(11);
lt_int.push_front(20);//在头部插入数据
lt_int.push_front(19);
list<int>::iterator int_iterator; //声明一个迭代器,指明类型
//使用迭代器插入元素
int pos = 2;
int_iterator = lt_int.begin();
while(pos-- > 0){
int_iterator++;
}
lt_int.insert(int_iterator, 30);
lt_int.sort();//元素从小到大排序
lt_int.reverse();//逆置
// int_iterator = lt_int.begin();//返回list中第一个元素的iterator,end()返回最后一个元素后一个位置
//类似于指针的使用
for (int_iterator = lt_int.begin(); int_iterator != lt_int.end(); int_iterator++){
cout<< *int_iterator <<endl;
}
return 0;
}
c++ map容器
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
map<string, string> map_string;//声明一个空的map容器,key为string,value为string
map_string["a"] = "cpp";
map_string["b"] = "c";
cout<< "key: a | value: " << map_string["a"] <<endl;
return 0;
}
c++ 声明一个类
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
string name;
string getHobby(){
return this -> hobby;
}
void setHobby(string hobby){
this -> hobby = hobby;
}
int getAge(){
return this -> age;
}
void setAge(int age){
this -> age = age;
}
void displayInfo();//声明成员函数
protected:
int age;
private:
string hobby;
};
//在类的外部实现类的成员函数
void Person::displayInfo(){
cout<< this -> name << " : " << this -> age << " : " << this -> hobby <<endl;
}
int main(){
Person a; //在栈中声明一个Person类的引用变量
a.name = "Tom";
a.setAge(20);
a.setHobby("Football");
a.displayInfo();
Person *p = new Person();//在堆区创建Person类的一个对象 ,并返回该对象在堆区的首地址
p -> name = "Jack";
p -> setAge(15);
p -> setHobby("Basketball");
p -> displayInfo();
delete(p);
p = NULL;
return 0;
}
c++ 构造函数和析构函数
#include<iostream>
#include<string>
using namespace std;
class Person{
public:
string name;
Person(){
cout<< "无参构造函数被调用" <<endl;
}
Person(string name, int age, string hobby){
cout<< "有参构造函数被调用" <<endl;
this -> name = name;
this -> age = age;
this -> hobby = hobby;
}
~Person(){
cout<< "析构函数被调用" <<endl;
}
string getHobby(){
return this -> hobby;
}
void setHobby(string hobby){
this -> hobby = hobby;
}
int getAge(){
return this -> age;
}
void setAge(int age){
this -> age = age;
}
void displayInfo(){
cout<< this -> name << " : " << this -> age << " : " << this -> hobby <<endl;
}
protected:
int age;
private:
string hobby;
};
int main(){
{
Person p1;// 在栈区创建的对象,变量作用范围结束的时候调用析构函数
}
Person *p2 = new Person("Jack", 15, "Ball");
p2 -> displayInfo();
delete p2;// 在堆区创建的对象,delete的时候调用析构函数
p2 = NULL;
return 0;
}
c++ 继承
#include<iostream>
using namespace std;
class Animal{
public:
void setName(string name){
this->name = name;
}
void setAge(int age){
this->age = age;
}
void setType(string type){
this->type = type;
}
void display(){
cout<< name << " : " << age << " : " << type <<endl;
}
protected:
string name;
private:
int age;
string type;
};
class Fish: private Animal{
public:
void fun(){
display();
cout<< name <<endl;
}
};
class GoldFish: public Fish{
public:
void fun2(){
// display();
}
};
int main(){
// 基类中private修饰的成员始终不能被派生类继承
// 基类中public修饰的成员始终可以被派生类继承,不同的继承方式,会改变成员的访问权限
// protected方式会使派生类中public -> protected、protected -> protected
// private方式会使派生类中public -> private、protected -> private
return 0;
}
c++ 静态成员
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
static string id;
int a;
static void display(){
cout<< id <<endl;//静态成员函数可以访问静态成员变量,不可访问非静态成员
}
};
string Student::id;//初始化的时候分配内存
int main(){
Student::id = "1001";
Student::display();
return 0;
}
c++ 虚函数与纯虚函数
#include<iostream>
using namespace std;
class Person{
public:
void eat(){
cout<< "Person eat" <<endl;
}
//声明为虚函数,使用父类的指针或者引用可以访问到子类中对该函数的重写
virtual void run(){
cout<< "Person run" <<endl;
}
//声明为纯虚函数
virtual void sleep() = 0;
};
class Student: public Person{
public:
//重写父类的函数
void eat(){
cout<< "Student eat" <<endl;
}
void run(){
cout<< "Student run" <<endl;
}
//子类必须重写父类的纯虚函数
void sleep(){
cout<< "Student sleep" <<endl;
}
};
int main(){
Student s;
Person *p = &s;
p -> eat();//在父类中没有声明为虚函数,则只能访问到父类自己的函数
s.eat();
p -> run();
p -> sleep();
return 0;
}
c++ 多继承与虚继承以及命名空间的声明
#include<iostream>
using namespace std;
//命名空间的声明方式
namespace Name{
class A{
public:
int a;
};
//虚继承使子类在实现多继承时,在父类出现冲突时,只会得到父类中冲突成员的一份拷贝
class B1: virtual public A{
public:
int b1;
};
class B2: virtual public A{
public:
int b2;
};
class C: public B1, public B2{
public:
void fun(){
int s = a;
}
};
}
int main(){
Name::A a;
a.a = 10;
return 0;
}