找到
11
篇与
编程语言
相关的结果
-
运算符重载 运算符重载 运算符重载的概念 运算符重载是指允许用户自定义类型的对象使用内置的运算符,或者允许用户自定义的运算符作用于内置类型的对象。运算符重载是通过函数重载来实现的。 不能重载的运算符: .和.*运算符 ::运算符 ?:运算符 sizeof运算符 typeid运算符 运算符重载的基本语法 运算符重载的基本语法如下: 返回类型 operator 运算符(参数列表) { //函数体 }加法运算符重载 //运算符重载-1 #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } example operator+ (example x) { cout<<"重载操作"<<endl; example temp(a+x.a,b+x.b); return temp; } }; int main() { example e1(1,2),e2(3,4); e1=e1+e1+e2+e2; example e3 = e1+e2; cout<<"e3的值"; cout<<e3.a<<","<<e3.b<<endl; cout<<"e1的值"; cout<<e1.a<<","<<e1.b<<endl; return 0; }运行结果: 初始化赋值操作 初始化赋值操作 重载操作 初始化赋值操作 重载操作 初始化赋值操作 重载操作 初始化赋值操作 重载操作 初始化赋值操作 e3的值11,16 e1的值8,12不但可以成员函数实现 + 号运算符重载,也可以通过全局函数实现 + 号运算符重载。 //运算符重载-2 #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } }; example operator+ (example x,example y) { cout<<"重载操作"<<endl; example temp(x.a+y.a,x.b+y.b); return temp; } int main() { example e1(1,2),e2(3,4); e1=e1+e1+e2+e2; example e3 = e1+e2; cout<<"e3的值"; cout<<e3.a<<","<<e3.b<<endl; cout<<"e1的值"; cout<<e1.a<<","<<e1.b<<endl; return 0; }初始化赋值操作 初始化赋值操作 重载操作 初始化赋值操作 重载操作 初始化赋值操作 重载操作 初始化赋值操作 重载操作 初始化赋值操作 e3的值11,16 e1的值8,12左移(<<)运算符重载 //运算符重载-3 #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } //如果需要访问私有成员,需要将重载运算符函数声明为友元函数 friend ostream& operator<< (ostream &out,example x); }; ostream& operator<< (ostream &out,example x) { cout<<"重载<<运算符"<<endl; out<<x.a<<","<<x.b<<endl; return out; } int main() { example e1(1,2),e2(3,4); cout<<e1<<e2; return 0; }输出结果: 初始化赋值操作 初始化赋值操作 重载<<运算符 1,2 重载<<运算符 3,4自增运算符重载 //运算符重载-4(前置++) #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } example operator++ () { cout<<"重载++运算符"<<endl; a++; b++; return *this; } }; ostream& operator<<(ostream& out, example ex1) { out << ex1.a<<ex1.b; return out; } int main() { example e1(1,2),e2(3,4); ++e1; ++e2; cout<<e1<<","<<e2; return 0; }输出结果: 初始化赋值操作 初始化赋值操作 重载++运算符 重载++运算符 23,45//运算符重载-5(后置++) #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } example operator++ (int) { cout<<"重载++运算符"<<endl; example temp(a,b); a++; b++; return temp; } }; ostream& operator<<(ostream& out, example ex1) { out << ex1.a<<ex1.b; return out; } int main() { example e1(1,2),e2(3,4); e1++; e2++; cout<<e1<<e2<<endl; cout<<e1++<<endl; cout<<e1; return 0; }输出结果: 初始化赋值操作 初始化赋值操作 重载++运算符 初始化赋值操作 重载++运算符 初始化赋值操作 2345 重载++运算符 初始化赋值操作 23 34所以说:前置递增返回引用,后置递增返回值。 赋值运算符重载 //运算符重载-6 #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } example operator= (example x) { cout<<"重载赋值运算符"<<endl; a=x.a; b=x.b; return *this; } }; ostream& operator<<(ostream& out, example ex1) { out << ex1.a<<ex1.b; return out; } int main() { example e1(1,2),e2(3,4); e1=e2; cout<<e1<<e2<<endl; return 0; }输出结果: 初始化赋值操作 初始化赋值操作 重载赋值运算符 3434关系运算符重载 //运算符重载-7 #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } bool operator== (example x) { cout<<"重载==运算符"<<endl; if(a==x.a&&b==x.b) return true; else return false; } bool operator!= (example x) { cout<<"重载!=运算符"<<endl; if(a!=x.a||b!=x.b) return true; else return false; } bool operator> (example x) { cout<<"重载>运算符"<<endl; if(a>x.a&&b>x.b) return true; else return false; } bool operator< (example x) { cout<<"重载<运算符"<<endl; if(a<x.a&&b<x.b) return true; else return false; } }; ostream& operator<<(ostream& out, example ex1) { out << ex1.a<<ex1.b; return out; } int main() { example e1(1,2),e2(3,4); if(e1==e2) cout<<"e1==e2"<<endl; else cout<<"e1!=e2"<<endl; if(e1!=e2) cout<<"e1!=e2"<<endl; else cout<<"e1==e2"<<endl; if(e1>e2) cout<<"e1>e2"<<endl; else cout<<"e1<=e2"<<endl; if(e1<e2) cout<<"e1<e2"<<endl; else cout<<"e1>=e2"<<endl; return 0; }输出结果: 初始化赋值操作 初始化赋值操作 重载==运算符 e1!=e2 重载!=运算符 e1!=e2 重载>运算符 e1<=e2 重载<运算符 e1<e2函数调用运算符重载 //运算符重载-8 #include<iostream> using namespace std; class example { public: int a; int b; example(int x,int y):a(x),b(y) { cout<<"初始化赋值操作"<<endl; } example operator() (example x) { cout<<"重载()运算符"<<endl; a=x.a; b=x.b; return *this; } }; ostream& operator<<(ostream& out, example ex1) { out << ex1.a<<ex1.b; return out; } int main() { example e1(1,2),e2(3,4); e1(e2); cout<<e1<<","<<e2<<endl; return 0; }输出结果: 初始化赋值操作 初始化赋值操作 重载()运算符 34,34其他运算符略 重载运算符的注意事项 重载运算符不能改变运算符的优先级和结合性 重载运算符不能创建新的运算符 重载运算符不能改变运算符的操作数个数 重载运算符不能改变运算符的操作数类型 重载运算符不能改变运算符的操作数的含义 重载运算符的应用 重载运算符可以使得类的对象像内置类型一样使用,如:cin、cout、+、-、、/、==、!=、>、<、++、--、=、()、[]、&、|、~、!、<<、>>、+=、-=、=、/=、<<=、>>=、&=、^=、|=、,、->、->*、new、delete、new[]、delete[]等;
-
引用 引用 语法结构数据类型 &别名=原名 注意事项: 引用必须在定义的时候必须要初始化。 引用在初始化后不允许更改引用。 引用做函数参数,进行引用传递:其和按地址传递是一样的。因为引用传递可以看作给变量取别名的过程。 引用也是可以作为返回值存在的,但是返回局部变量的引用会因为局部变量存在于战区内导致被系统释放掉。但是函数是可以作左值出现的 例如: //返回静态变量引用 int& test02() { static int a = 20; return a; } //如果函数做左值,那么必须返回引用 int& ref2 = test02(); cout << "ref2 = " << ref2 << endl; cout << "ref2 = " << ref2 << endl; test02() = 1000; cout << "ref2 = " << ref2 << endl; cout << "ref2 = " << ref2 << endl;本质:引用的本质在c++内部实现是一个指针常量. 常量引用 作用:常量引用主要用来修饰形参,防止误操作 在函数形参列表中,可以加==const修饰形参==,防止形参改变实参 示例: //引用使用的场景,通常用来修饰形参 void showValue(const int& v) { //v += 10; cout << v << endl; } int main() { //int& ref = 10; 引用本身需要一个合法的内存空间,因此这行错误 //加入const就可以了,编译器优化代码,int temp = 10; const int& ref = temp; const int& ref = 10; //ref = 100; //加入const后不可以修改变 cout << ref << endl; //函数中利用常量引用防止误操作修改实参 int a = 10; showValue(a); system("pause"); return 0; }
-
文件 文件 C++可以将进行文件操作。C++使用文件的时候需要包含#include<fstream>这个库。 文件类型分为两种,一种是二进制文件,一种是文本文件。他们分别对应两大种存储方式。一个我们可以通过类似于记事本方式打开,一个一般情况之下我们看不太懂。 文本文件 包含头文件 #include<fstream> 创建流对象 ofstream ofs; 打开文件 ofs.open("文件路径",打开方式); 写数据 ofs << "写入的数据"; 关闭文件 ofs.close(); 文件打开方式: 打开方式解释ios::in为读文件而打开文件ios::out为写文件而打开文件ios::ate初始位置:文件尾ios::app追加方式写文件ios::trunc如果文件存在先删除,再创建ios::binary二进制方式注意: 文件打开方式可以配合使用,利用|操作符 例如:用二进制方式写文件 ios::binary | ios:: out for example: //文件-1(文本文件写入) #include<iostream> #include<fstream> using namespace std; int main() { ofstream ofs1; ofs1.open("1.txt",ios::out); ofs1<<"我是你的爸爸"<<endl; ofs1<<"66666"<<endl; ofs1.close(); }读文件其实类似,只需要把输出流fstream改为输入流ifstream就可以了,头文件不变。看代码 //文件-2(文本文件读取) #include<iostream> #include<fstream> using namespace std; int main() { ifstream ifs1; ifs1.open("1.txt",ios::in); if (!ifs1.is_open()) { cout << "文件打开失败" << endl; return 0; } //第一种方式 //char buf[1024] = { 0 }; //while (ifs >> buf) //{ // cout << buf << endl; //} //第二种 //char buf[1024] = { 0 }; //while (ifs1.getline(buf,sizeof(buf))) //{ // cout << buf << endl; //} //第三种 //string buf; //while (getline(ifs1, buf)) //{ // cout << buf << endl; //} char c; while ((c = ifs1.get()) != EOF)//EOF:end of file { cout << c; } ifs1.close(); }二进制文件 二进制文件的读取和写入,可以用以下两个函数原型: 写入:ostream& write(const char * buffer,int len);参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数 读取:istream& read(char *buffer,int len);参数解释:字符指针buffer指向内存中一段存储空间。len是读写的字节数 同时加上|操作符,ios::binary以二进制打开就可以了 for example
-
内存分区 内存分区 C++内存分区模型 C++的程序在执行过程中,大体会划分为四个区域,也就是:代码区,全局区,堆区,栈区。 在程序进行了编译之后C++的编译器会在编译之后,生成一个.exe的可执行文件,这个可执行文件在被执行之前,会有两个区域。 代码区: 存放 CPU 执行的机器指令 代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可 代码区是只读的,使其只读的原因是防止程序意外地修改了它的指令 全局区: 全局变量和静态变量存放在此. 全局区还包含了常量区, 字符串常量和其他常量也存放在此. ==该区域的数据在程序结束后由操作系统释放==. 可以利用下面这行代码理解一下: //全局变量 int g_a = 10; int g_b = 10; //全局常量 const int c_g_a = 10; const int c_g_b = 10; int main() { //局部变量 int a = 10; int b = 10; //打印地址 cout << "局部变量a地址为: " << (int)&a << endl; cout << "局部变量b地址为: " << (int)&b << endl; cout << "全局变量g_a地址为: " << (int)&g_a << endl; cout << "全局变量g_b地址为: " << (int)&g_b << endl; //静态变量 static int s_a = 10; static int s_b = 10; cout << "静态变量s_a地址为: " << (int)&s_a << endl; cout << "静态变量s_b地址为: " << (int)&s_b << endl; cout << "字符串常量地址为: " << (int)&"hello world" << endl; cout << "字符串常量地址为: " << (int)&"hello world1" << endl; cout << "全局常量c_g_a地址为: " << (int)&c_g_a << endl; cout << "全局常量c_g_b地址为: " << (int)&c_g_b << endl; const int c_l_a = 10; const int c_l_b = 10; cout << "局部常量c_l_a地址为: " << (int)&c_l_a << endl; cout << "局部常量c_l_b地址为: " << (int)&c_l_b << endl; system("pause"); return 0; }局部变量是不会存放在全局区中,全局区中又会包含常量,全局常量和字符串常量。无论如何局部的常量或者变量都不会存放在全局区中。 当然在程序执行后,又会有两个区域。 栈区: 由编译器自动分配释放, 存放函数的参数值,局部变量等 注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放 堆区: 由程序员分配释放,若程序员不释放,程序结束时由操作系统回收 在C++中主要利用new在堆区开辟内存 new操作符 基本语法:new 数据类型 new返回的是该数据对性的类型的指针。堆区的数据由代码的编写者管理开辟,管理释放,释放需要用到delete。 释放数组的时候要加一个[] 举例: new int a[10];//堆区创建一个数组 new int a(10);//堆区创建一个整型值为10 delete p;//释放 delete[] p;//释放数组
-
模板 模板 模板是一类通用的模型,无论什么情况我们都有可以调用他,提高复用性。 C++中的模板分为函数模板和类模板两大类型。 函数模板 作用:建立一个通用的函数,其函数返回值类型和形参不具体指向特定的数据类型,用一个虚拟的类型来表示 语法: template<typename T> 函数声明很容易template表示这是一个模板,typename后面的T是一个数据类型,可以用class代替。T就是一个变量 在使用过程中可以系统可以自己推导出来数据类型且必须一致才可以使用,同时也可以自己指定数据类型。 //模板-1 #include<iostream> using namespace std; //利用模板提供通用的交换函数 template<class T> void mySwap(T& a, T& b) { cout<<"mySwap的调用"<<endl; T temp = a; a = b; b = temp; } // 1、自动类型推导,必须推导出一致的数据类型T,才可以使用 void test01() { int a = 10; int b = 20; char c = 'c'; c++; mySwap(a, b); // 正确,可以推导出一致的T //mySwap(a, c); // 错误,推导不出一致的T类型 } // 2、模板必须要确定出T的数据类型,才可以使用 template<class T> void func() { cout << "func 调用" << endl; } void test02() { //func(); //错误,模板不能独立使用,必须确定出T的类型 func<int>(); //利用显示指定类型的方式,给T一个类型,才可以使用该模板 } int main() { test01(); test02(); return 0; }结果: mySwap的调用 func 调用不过函数模板并不是万能的,还是有它的局限性的。C++还可以对模板重载和对特定的数据类型进行具体化。 譬如说: //具体化,显示具体化的原型和定意思以template<>开头,并通过名称来指出类型 //具体化优先于常规模板 template<> bool myCompare(Person &p1, Person &p2)上面就是一个简单的例子,他对bool类型进行了一个特殊的定义。 类模板 语法同函数模板,类模板没有自动类型推导的使用方式也就是说,你必须使用指定类型 例如: Peson<string,int>p("刘纪彤",1000);值得一提的是类模板中的模板参数列表可以有默认参数。 template<class NameType, class AgeType = int> 像这样AgeType就默认是int,后面可以直接用默认的int类型。大体和函数那里互相类似。 类模板中的成员函数并不是一开始就创建的,在调用时才去创建 //模板-2 #include<iostream> using namespace std; class Person1 { public: void showPerson1() { cout << "Person1 show" << endl; } }; class Person2 { public: void showPerson2() { cout << "Person2 show" << endl; } }; template<class T> class MyClass { public: T obj; //类模板中的成员函数,并不是一开始就创建的,而是在模板调用时再生成 void fun1() { obj.showPerson1(); } void fun2() { obj.showPerson2(); } }; void test01() { MyClass<Person1> m; m.fun1(); //m.fun2();//error: 'class Person1' has no member named 'showPerson2'; } int main() { test01(); return 0; }结果: Person1 show传参数的时候可以有三种情况一共有三种传入方式: 指定传入的类型 --- 直接显示对象的数据类型 参数模板化 --- 将对象中的参数变为模板进行传递 整个类模板化 --- 将这个对象类型 模板化进行传递 //1、指定传入的类型 void printPerson1(Person<string, int> &p) //2、参数模板化 template <class T1, class T2> void printPerson2(Person<T1, T2>&p) //3、整个类模板化 template<class T> void printPerson3(T & p)模板的继承: 当类模板碰到继承时,需要注意一下几点: 当子类继承的父类是一个类模板时,子类在声明的时候,要指定出父类中T的类型 如果不指定,编译器无法给子类分配内存 如果想灵活指定出父类中T的类型,子类也需变为类模板 示例 class Son :public Base<int> //必须指定一个类型 //类模板继承类模板 ,可以用T2指定父类中的T类型 template<class T1, class T2> class Son2 :public Base<T2>类外实现就可以用通过::实现,示例代码如下: template<class T1, class T2> void Person<T1, T2>::showPerson() {} template<class T1, class T2> Person<T1, T2>::Person(T1 name, T2 age) {}类模板实现友元函数: 全局函数类内实现 - 直接在类内声明友元即可 全局函数类外实现 - 需要提前让编译器知道全局函数的存在