C++学习记录
前言:这是记录C++学习过程中遇到的问题,防止重复犯这些错误。
2017.09.02
/***********************************************************************************************************************************************************************************************************/
今天使用codeblocks来写一个Cat类,但是编译的时候遇到“error: 'string' does not name a type“的错误提示。
解决方法:
增加一行代码:using namespace std;
但是这样的做法不好,可能导致空间污染。
因此最好的做法是在string前面加上std::前缀
2017.09.09
/***********************************************************************************************************************************************************************************************************/
include <?>和include "?"有什么区别?
首先看一下我自定义的一个头文件Cat.h,尝试两种include的方法。
include<Cat.h>
编译时出错:
编译器找不到头文件
但是使用include"Cat.h"时编译顺利通过。
百度了有关这方面的区别:
include<?>编译器会到标准库中寻找头文件。
include"?"编译器会先到当前文件目录下寻找头文件。
所以自定义的头文件如果没有加入标准库或者编译器选项中添加路径,就会出现无法找到头文件的错误。
2017/09/12
/***********************************************************************************************************************************************************************************************************/
关于“引用”的思考:
C++语言中存在引用机制,就像是同一个人有多个名称一样,中文名,英文名,日文名等。。。
用法:
int a;
int &aa=a; /*定义引用aa,它是变量a的引用*/
在声明一个引用的时候同时需要初始化。
其实我感觉引用是对指针的一种封装。
实例:
利用引用交换两个变量的值
#include <iostream> using namespace std; void swap(int &x,int &y);
int main() { int x,y; cout<<"Please input two numbers: "; cin>>x>>y; cout<<"The numbers you input are: "<<x<<" and "<<y<<endl; cout <<"Now swap the val of the two numbers"<<endl; swap(x,y); cout<<"After swapped..."<<endl; cout<<"x= "<<x; cout<<"\n"; cout<<"y= "<<y; return 0; }
void swap(int &x,int &y) { int tmp=0; tmp=x; x=y; y=tmp; }
2017/09/15
/***********************************************************************************************************************************************************************************************************/
今天学习了运算符重载的概念
学习内容:
1、对 + 号进行运算符重载;
2、<< 流操作符的重载;
代码如下:
main.h
1 #include<iostream> 2 3 class Complex 4 { 5 public: 6 Complex(); 7 Complex(double r,double i); 8 Complex operator + (Complex &c); 9 private: 10 double real; 11 double imag; 12 13 friend std::ostream& operator << (std::ostream& os,Complex c); 14 }; 15 16 Complex::Complex() 17 { 18 this->real=0; 19 this->imag=0; 20 } 21 Complex::Complex(double r,double i) 22 { 23 this->real=r; 24 this->imag=i; 25 } 26 27 Complex Complex::operator +(Complex &c) 28 { 29 Complex rtl; 30 rtl.real=this->real+c.real; 31 rtl.imag=this->imag+c.imag; 32 return rtl; 33 } 34 35 std::ostream& operator << (std::ostream &os,Complex c); 36 37 std::ostream& operator <<(std::ostream &os,Complex c) 38 { 39 os<<"( "<<c.real<<" , "<<c.imag<<" )"; 40 return os; 41 }
main.cpp:
#include<iostream> #include"main.h" int main() { Complex c1(3,4), c2(5,-10), c3; c3=c1+c2; std::cout<<"c1 = "<<c1<<std::endl; std::cout<<"c2 = "<<c2<<std::endl; std::cout<<"c1 + c2 = "<<c3<<std::endl; return 0; }
其中需要注意:
声明重载<<操作符的时候需要将其声明为复数类的友元函数。
friend std::ostream& operator << (std::ostream& os,Complex c);
并且需要注意&的使用。
声明重载 + 操作符的时候需要注意:
不能忘记写&,作用是传址。
Complex operator + (Complex &c);
2017/09/16
/***********************************************************************************************************************************************************************************************************/
C++中的内存分配:
堆(heap)和栈(stat)的概念:
堆和栈都是人为划分的区域,其中程序员可以对堆(heap)进行操作,而栈(stat)不可控,由OS自动分配并释放。
申请动态内存:
1、使用new分配一定大小的内存(必须和delete成套使用,否则会导致内存溢出问题)。
Type *p = new Type;
delete p;
必须记得手动释放内存!!!
实例:
#include<iostream> class Print { public: Print() { std::cout<<"这里是构造函数"<<std::endl; } ~Print() { std::cout<<"这里是析构函数"<<std::endl; } void printfunc() { std::cout<<"在这输出文字"<<std::endl; } }; void func() { Print *p = new Print; p->printfunc(); #if 1 delete p; #endif } int main() { func(); return 0; }
在有delete p;这条语句时,执行结果如下:
如果么有delete p;则会出现以下情况:
结果是不会执行析构函数,如果大量内存被动态申请却不释放,则会使得内存溢出。