本文性质:B站视频笔记
课程来源:这可能是史上最快学习C++的课程,期末考前复习冲刺的宝典_哔哩哔哩_bilibili
内容为C++基础知识:语法、输出、注释、变量(及常量)、输入、运算、字符串、条件语句、Switch、循环语句、数组、指针、函数、类、多态性、文件、异常处理
将来复习方法:打开C++编译器,下面所有代码敲一遍,预计时间8-10h可以搞定。
学习时间:2023.11.07
1 、输出
(1)有无using namespace std差别,下面两种代码等效
#include<iostream>
using namespace std;//分号别漏
int main(){
cout <<"Hello world!";
return 0;
}
#include<iostream>
int main(){
std::cout <<"Hello world!";
return 0;
}
(2)默认连续输出不换行,下面两种代码等效
#include<iostream>
using namespace std;//分号别漏
int main(){
cout <<"Hello world!";
cout <<"I am learning C++";
return 0;
}
#include<iostream>
using namespace std;//分号别漏
int main(){
cout <<"Hello world!"<<"I am learning C++";
return 0;
}
(3)输出字符串钟加入\n实现换行,输出结果区分与上面(2)中代码
#include<iostream>
using namespace std;//分号别漏
int main(){
cout <<"Hello world! \n";
cout <<"I am learning C++";
return 0;
}
(4)输出两个\n即为换两行
#include<iostream>
using namespace std;//分号别漏
int main(){
cout <<"Hello world! \n\n";
cout <<"I am learning C++";
return 0;
}
(5)另一种换行方式<<endl
#include<iostream>
using namespace std;//分号别漏
int main(){
cout <<"Hello world! "<<endl;
cout <<"I am learning C++";
return 0;
}
2、注释:单行注释//;多行注释 /*开头,*/结尾
3、变量(及常量)
(1)int 整型变量
#include<iostream>
using namespace std;//分号别漏
int main(){
int mynum = 15;
cout << mynum;
return 0;
}
//输出结果 15
#include<iostream>
using namespace std;//分号别漏
int main(){
int mynum = 15;
mynum =10;
cout << mynum;
return 0;
}
//输出结果 10
(2)float浮点型;double双精度浮点型;char(单个)字符;string字符串;bool
Tips:float的指数位有8位,而double的指数位有11位,分布如下:
float:1bit(符号位) 8bits(指数位) 23bits(尾数位);
double:1bit(符号位) 11bits(指数位) 52bits(尾数位);
Tips:当(单个)字符存储在内存中时,它实际上是存储的数字代码。当计算机被指示在屏幕上打印该值时,它将显示与数字代码对应的字符。数字 65 对应大写字母 A。你认为存储在内存中的是字符 A时,实际上存储的是数字 65。故输出结果为AA。(这里涉及到ASCII表)
Tips:单个字符char用的是单引号;字符串string用的是双引号。
Tips:有string要添加对应标准库#include<string>,网上有说没加也能跑的,咱不管,咱必须加
#include<iostream>
#include<string>
using namespace std;//分号别漏
int main(){
int mynum = 15;
double mydoublenum = 5.6695;
float myfloatnum = 7.55;
double mydoublenum2 = 8E2; //是科学计数法8*10^2;
float myfloatnum2 = 4e2; // 大小E,e都没关系
char myletter1 = 65;
char myletter2 = 'A';
string mytext = "hello";
bool mybool = true;
cout<<myletter1<<myletter2;
return 0;
}
//输出结果为AA
(3)可以同时赋值多个变量;可以在赋值时直接定义运算;可以在输出时定义运算
#include<iostream>
using namespace std;
int main(){
int x=5,y=6;
int k = x + y ;
cout<<x+y+k<<endl;
cout<<"I am"<<k<<"years old";
return 0
}
(4)常量const,代码中不可变,强行修改会报错
#include<iostream>
using namespace std;
int main(){
const int x=5;
const double y=9.99;
return 0
}
(5)变量及常量命名规则
4、输入
#include<iostream>
using namespace std;
int main(){
int x;
cout << "Please type a number:";
cin >> x; //从键盘获取输入
cout << "Your number is:"<< x;
return 0;
}
#include<iostream>
using namespace std;
int main(){
int x,y;
int sum;
cout << "Please type a number:";
cin >> x; //从键盘获取输入
cout << "Please type another number:";
cin >> y;
sum = x+y;
cout << "sum is:"<< sum;
//注意这里cin,cout会手动换行,因为键盘输出后用户默认会按回车
return 0;
}
5、运算与字符串
(1)运算符
Tips:x++和++x 两者区别: 如果单独作为一条语句的话,并没有区别例如 x++;和++x;没有任何区别 如果作为一个表达式;前者表达式值为x,然后x自身+1;后者表达式值x自身+1以后的x值, 例:x=1; a=x++;这里a结果是1;x是2; x=1;a=++x; 这里a结果是2, x结果也是2
#include<iostream>
using namespace std;
int main(){
int x = 5;
int y = 2;
cout << x + y << endl;
cout << x - y << endl;
cout << x * y << endl;
cout << x / y << ednl;
cout << x % y << endl;
cout << x++ << ednl;//先赋值再自加,初始x=5,k=x++,则k=5,x=x+1=6;
cout << ++x << endl;//先自加再赋值,初始x=6,k=++x,则x=7,k=7
cout << y-- << endl;//
cout << --y << endl;//
return 0;
}
#include<iostream>
using namespace std;
int main(){
int x = 5;
x += 3; //相当于x=x+3;
cout << "x += 3 " << x <<endl;
x = 5;
x -= 3; //相当于x=x-3
cout << "x -= 3" << x <<endl;
x = 5;
x *= 3; //相当于x=x*3
cout << "x *= 3" << x <<endl;
x = 5;
x /= 3; //相当于x=x/3
cout << "x /= 3" << x <<endl;
x = 5;
x %= 3; //相当于x=x%3,取余
cout << "x %= 3" << x <<endl;
x = 5;
x &= 3; //相当于x=x&3,与
cout << "x &= 3" << x <<endl;
x = 5;
x |= 3; //相当于x=x|3,或
cout << "x |= 3" << x <<endl;
x = 5;
x ^= 3; //相当于x=x^3,异或
cout << "x ^= 3" << x <<endl;
x = 5;
x >>= 3; //相当于x=x>>3,右移三位,101变000
cout << "x >>= 3" << x <<endl;
x = 5;
x <<= 3; //相当于x=x<<3,左移三位,0000_0101变0010_1000=十进制的40
cout << "x <<= 3" << x <<endl;
return 0;
}
(2)比较运算符
#include<iostream>
using namespace std
int (){
int x = 5;
int y = 2;
cout << (x == y) <<endl;//输出为0,bool型即为false
cout << (x != y) <<endl;//输出为1,bool型即为true
cout << (x > y) <<endl;//输出为1,bool型即为true
cout << (x < y) <<endl;//输出为0,bool型即为false
cout << (x >= y) <<endl;//输出为1,bool型即为true
cout << (x <= y) <<endl;//输出为0,bool型即为false
return 0;
}
#include<iostream>
using namespace std
int (){
cout << (true && false) <<endl;//输出为0
cout << (true || false) <<endl;//输出为1
cout << (!true) <<endl;//输出为0
return 0;
}
(3)字符串,引入#include<string>数据库
#include<iostream>
#include<string>
using namespcae std
int main (){
string firstname = "Hong";
string lastname = "Belinda";
string fullname = firstname + lastname;
cout << fullname <<endl; //第一个输出HongBelinda
fullname = firstname + " " +lastname;
cout << fullname << endl; //第二个输出Hong Belinda
fullname = firstname.append(lastname);
cout << fullname << endl; //第三个输出HongBelinda
string txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
cout << "The length of the txt string is: " << txt.length() <<endl;//输出长度26
cout <<"The length of the txt string is: " << txt.size() <<endl;//和上一个语句等效
string mystring = "HELLO";
cout << mystring[0]<<endl;//输出H,记住这里从零开始,从左往右
mystring[0] = 'J';
cout << mystring <<endl;//输出Jello
}
(4)算术,引入#include<cmath>
TIPS:很多函数都有integer和float两个版本,函数名前面有无f的差别,这是一个规律,不一定,使用前可以多个心眼查一下,避免精度损失;
TIPS:很多计算公式有特定函数,是为了避免在函数某些特殊取值点的精度损失
TIPS:这里列的函数并不全,再自己另外找
#include<iostream>
#include<cmath>
using namespace std;
int main(){
cout << max(5,10) << endl;//输出10,取最大
cout << min(5,10) << endl;//输出5,取最小
cout << "fmin"
<< "return the lowest value of a floating x and y" << fmin(5,10)<<endl;
//很多函数都有integer和float两个版本
cout << sqrt(64) << endl;//输出8,开平方
cout << round(2.6) << endl;//输出3,四舍五入
cout << log(2) << endl;//输出0.693147,取对数4
cout << "fmod(x,y)"
<< "return the floating poinit reminder of x/y" << fmod(5,3) << endl;
cout << "abs(x)"
<< "return the absolute value of x " << abs(-5) <<endl;//输出5,取绝对值
cout << "fabs(x)"
<< "return the absolute value of a floating x " << fabs(5) <<endl;
cout << "fdim(x,y)"
<< "return the positive difference between x and y" << fdim(3,5)<<endl;
cout << "acos(x)"
<< “return the arccosine of x ” << acos(0.5) <<endl;//输出1.0472,即60°
cout << "asin(x)"
<< “return the arcsine of x ” << asin(0.5) <<endl;//输出0.523599,即30°
cout << "atan(x)"
<< “return the arctan of x ” << atan(5) <<endl;//输出1.3734
cout << "cbrt(x)"
<< “return the cube root of x ” << cbrt(5) <<endl;//输出1.70998,开三次方
cout << "pow(x,y)"
<< "return the value of x to the power of y" << pow(5,3) <<endl; //5的3次方
cout << "ceil(x)"
<< “return the value of x rounded up to the nearest integer” << ceil(5.6) <<endl;//输出6
cout << "floor(x)"
<< "return the value of x rounded down to its nearest integer"<<floor(5.6)<<endl;
cout << "exp(x)"
<< "return the value of Ex" << exp(5) <<endl;
cout << "expm1(x)"
<< "return ex -1 " << expm1(5) <<endl;
cout << "hypot(x,y)"
<< "return sqrt(x2 + y2) without intermediate overflow or underflow" << hypot(5,3)<<endl;
cout << "fma(x)"
<< "return x*y+z without losing precision" << fma(5,3,2)<<endl;
return 0;
}
6、条件语句
(1)if句式及简单判断句式
第一种:if ……else…… ;第二种: if……else if ……else……; 第三种:判断式选择
#include<iostream>
using namespace std;
int main ()
{
if (20>18)
{
cout << "20 is greater than 18" << endl;
}
int time = 20;
//第一种
if (time < 18)
{
cout << "Good day." << endl;
}
else
{
cout << "Good evening." << endl;
}
//第二种
if (time < 10)
{
cout << "Good morning." << endl;
}
else if (time <25)
{
cout << "Good day." << endl;
}
else
{
cout << "Good evening." << endl;
}
//第三种
string result = (time <18)? "Good day." : "Good eveing" ;
cout << result <<endl;
return 0;
}
(2)switch-case-default
#include<iostream>
using namespace std;
int main ()
{
int day = 4;
switch(day)
{
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "the number is over seven"<<endl;
}
return 0
}
7、循环语句(三种)
Tips:注意break和continue的作用
#include<iostream>
using namespace std;
int main ()
{
int i = 0;
//第一种while
while(i < 5)
{
cout << i <<"\n";
i++;
}
//第二种do while
i = 0;
do
{
cout << i <<"\n";
i++;
}while(i < 5);
//第三种for
for (int j = 0 ; j<5 ; j++)
{
cout << j << endl;
}
for (j ; j<10 ; j=j+2)
{
if(j == 8)
{
break; //跳出for循环
}
cout << j << endl;
}
for (j ; j<15 ; j=j+2)
{
if(j == 12)
{
contuie; //i=12时,不会执行下面语句,直接跳过,进入j+2下一次启动下一次循环
}
cout << j << endl;//因为continue语句无法输出12
}
}
8、数组
可以有整型数组,有字符数组,字符串数组……
#include<iostream>
#include<string>
using namespace std;
int main ()
{
string cars[4] = {"volvo","byd","ford","mazda"};//字符串数组
string bands[] = {"volvo","byd","ford","mazda","tesla"};//不定长数组
int mynum[3] = {10,20,30};//整型数组
cout << cars[0] << endl; // 输出volvo
for (int i = 0; i < 4 ; i++)
{
cout << i << ":" << cars[i] << endl;
}
return 0;
}
9、指针与引用
(1)引用
&这个符号可以理解为取地址,&meal =food;相当于meal取了food的内存地址,所以指向同一个内存块
#include<iostream>
using namespace std;
int main ()
{
string food = "Pizza";
string &meal = food;//就像给food创建了一个桌面快捷方式似的,指向同一个内存块
cout << food <<endl;//输出Pizza
cout << meal <<endl;//输出Pizza
cout << &food;//输出Pizza的存储地址
return0;
}
(2)指针
变量定义为指针,说明该变量对应内存块中存储的是一个地址数据,通过*可以直接调出地址数据对应内存块的内容,并且可以直接修改。
#include<iostream>
using namespace std;
int main ()
{
string food = "Pizza";
string *ptr = &food;
//等价于string* ptr = &food; 定义该变量为指针变量时,*的位置靠近变量名称还是数据类型都没关系
//string *ptr 定义变量为字符串指针
//&food引用出food的内存地址,并赋值给ptr
cout << food <<endl;//输出“Pizza”
cout << &food << endl;//输出food的内存地址
cout << ptr << endl;//输出food的内存地址
cout << *ptr <<endl;//输出“Pizza”,调用ptr内容对应的数据地址的内容
*ptr = "Hamburger"; //可以理解为直接对food的存储数据做出了修改
cout << *ptr << endl;//输出“Hamburger”
cout << food << endl;//输出“Hamburger”
return 0;
}
10、函数
Tips:void 就是空,不需要返回,即不需要return语句 ;int 的函数声明需要return int型结果?;
Tips:函数可以直接写在主函数前面,如果写在主函数后面,则在主函数之前需要一句简单声明。
Tips:函数输入变量为空,可调用提前设定好的默认值
#include<iostream>
using namespace std;
void myfunction()
{
cout << "success to call the function" <<endl;
}
int main ()
{
myfunctino(); //调用函数
return 0;
}
#include<iostream>
using namespace std;
void myfunction();//函数声明
int main ()
{
myfunction(); //调用函数
return 0;
}
void myfunction()//函数定义
{
cout << "success to call the function" <<endl;
}
#include<iostream>
using namespace std;
void myfunction (string fname)
{
cout << fname << "Refsnes\n";
}
int main ()
{
myfunction("Jerry");
myfunction("Belinda");
myfunction("Joan");
return 0
}
#include<iostream>
using namespace std;
void myfunction (string fname = "Tina")//给予默认值
{
cout << fname << "Refsnes\n";
}
int main ()
{
myfunction("Jerry");
myfunction("Belinda");
myfunction();//没有输入变量,则默认输出为Tina Refsnes
myfunction("Joan");
return 0
}
#include<iostream>
using namespace std;
void myfunction (string fname,int age)//给予默认值
{
cout << fname << "Refsnes" << age << "years old"<<endl;
}
int main ()
{
myfunction("Jerry",3);
myfunction("Belinda",45);
myfunction("Joan",19);
return 0
}
#include<iostream>
using namespace std;
int myfunction(int x, int y)
{
return x+y;
}
int main ()
{
int z = myfunction(3 ,2);
cout << z << endl;
return 0;
}
下面这个示例,细品。
Tips:&引用及*指针都可以通过形参修改外部实参。
#include<iostream>
using namespace std;
void swapt1 (int x, int y)//相当于把firstnum的内存块内容赋值给了x的内存块内容
{
int z = x;
x = y; //x确实变成了20,但是firstnum的内存块还是10
y = z; //该函数并没有返回x去修改firstnum内存内容
}
void swapt2 (int &x, int &y)//这里是引用符号,不是取地址符号。相当于x=firstname,指向同一个内存块,函数形参能够调用外部实参
{
int z = x;
x = y;
y = z;
}
void swapt3 (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main ()
{
int firstnum = 10;
int secondnum = 20;
cout << "before swapt1 "<< firstnum <<" " <<secondnum<<endl;//输出 10 20
swapt1(firstnum,secondnum);
cout << "after swapt2" << firstnum <<" " <<secondnum<<endl;//输出 10 20,互换失败
swapt2(firstnum,secondnum);
cout << "after swapt1" << firstnum <<" " <<secondnum<<endl;//输出 20 10,互换成功
swapt3(&firstnum,&secondnum);
cout << "after swapt3" << firstnum <<" " <<secondnum<<endl;//输出 10 20,互换成功
return 0;
}
Tips:重载。多个函数可以具有同一个函数名,但是参数和功能是不同的,通过参数和功能来区分
#include<iostream>
using namespace std;
int plus (int x, int y)
{
return x+y;
}
double plus (double x, double y)
{
return x-y;
}
int main ()
{
int num1 = plus(2,6);
double num2 = plus (2.86,3.5);
cout << "int :" << num1 <<endl;
cout << "double: " << num2 <<endl;
return 0;
}
11、类class(属性,方法,构造函数,继承,多态性)
类和对象是面向对象编程的两个方面,区分如下:
类里面的变量叫属性,类里面的函数叫方法。
(1)属性
#include<iostream>
using namespace std;
class car //类定义
{
pubulic: //访问关键字
string brand; //类中的变量叫做属性
string model;
int year;
}; //类定义最后这个分号不要漏掉
int main ()
{
car carobject1; //声明类的对象,又称为实例,实例后才真正的分配内存
carobject1.brand = "BYD";
carobject1.model = "song plus";
carobject1.year = 2020;
car carobject2;
carobject2.brand = "BMW";
carobject2.model = "xs";
carobject2.year = 1999;
cout << carobject1.brand << " " << carobject1.model << " " << carobject1.year <<endl;
cout << carobject2.brand << " " << carobject2.model << " " << carobject2.year <<endl;
return 0;
}
(2)方法
#include<iostream>
using namespace std;
class car
{
public:
int speed(int maxspeed); //类里面的函数叫方法,这里只是声明了,后面得另外定义
void mymethod() // 也可以直接把方法(函数)的定义在这里写全了
{
cout << "Hello world!";
}
};
int car::speed(int maxspeed) //方法声明,要在多加上类名::
{
return maxspeed;
}
int main ()
{
car car1;//类声明
cout << car1.speed(300) << endl;
car1.mymethod();
return 0;
}
(3)构造函数
Tips:构造函数,在类中声明或定义的函数,且函数名与类名相同。在主函数中声明类的时候,就必须把构造函数的变量补齐,并且在声明类的时候构造函数就会自动执行。
#include<iostream>
using namespace std;
class car
{
public:
string brand;
string model;
int year;
car(string x,string y, int z); //构造函数,前面没有返回值类型
};
car::car(string x,string y, int z ) //构造函数,前面没有返回值类型
{
brand = x; //类中属性可以直接被调用
model = y;
year = z ;
}
int main ()
{
car car1("BYD","Song Plus",2020); //声明类,同时执行构造函数
car car2("BMW","XS",1999);
cout << car1.brand << " " << car1.model << " " << car1.year <<endl;
cout << car2.brand << " " << car2.model << " " << car2.year <<endl;
return 0;
}
(4)private 和public
#include<iostream>
using namespace std;
class myclass
{
int b; //默认为私有变量
public: //公共范围
int x;
int y;
private: //私有范围
int a;
};
int main ()
{
myclass myobj;//声明类
myobj.x = 25; // 该语句正确
myobj.b = 5; //该语句报错,b在类的私有范围,不允许访问
myobj.a = 7; //该语句报错,a在类的私有范围,不允许访问
return 0;
}
(5)对类中私有范围内的属性进行访问
Tips:类中私有范围内的变量并不是不可以修改的,很多时候我们不希望它可以直接公开访问;我们通过在公有范围内定义方法(函数)访问私有范围的属性。(就是拐个弯,我怕我自己丢了家门钥匙,但我不希望别人找到我的家门钥匙,就存了一把家门钥匙在公共区域的鞋柜的第三层,一个道理)
#include<iostream>
using namespace std;
class payment
{
private:
int salary;
public:
void setsalary(int x)
{
salary = x;
}
int getsalary()
{
return salary;
}
};
int main ()
{
payment my;
my.setsalary(20000);
cout << "my salary is " << my.getsalary();
return 0
}
(6)类的继承
好处:类的继承我就就能从一个大的父类里面逐渐继承后新增实现细分,这样可以逐渐细化分类。
#include<iostream>
using namespace std;
class vehicle // 父类
{
public:
string brand = "Ford";
void hook(){
cout<< "Tuut,Tuut!"<<endl;
}
};
class car :: public vehicle //子类,继承了vehicle 中public的内容
{
public:
string model = "Mustang";//子类新增属性
}
int main ()
{
car carobject; // 声明子类
carobject.hook(); // 由于子类继承了父类public中的所有属性与方法,所以这里可以访问
cout << carobject.brand << " " << carobject.model << endl;
return 0;
}
(7)多继承:一个子类同时继承多个父类
#include<iostream>
using namespace std;
class father1 // 父类1
{
public:
void func1()
{
cout << "success to get father1"<<endl;
}
};
class father2 // 父类2
{
public:
void func2()
{
cout << "success to get father2"<<endl;
}
};
class son :: public father1 , public father2 //子类,同时继承父类1和父类2
{
};
int main ()
{
son son_object;
son_object.func1();
son_object.func2();
return 0;
}
(8)protected下声明只有子类可以访问的属性或方法
protected(半公开)和private(私有)在父类中定义
相同点:在主函数中声明了父类,都无法通过声明父类直接调用;
差别是:子类可以继承父类中protected的属性或方法,在主函数中声明子类,可以通过声明子类直接访问protected的属性或方法;而private无法被子类继承。
(private定义的函数只能自己内部用,protected可以子类用并通过主函数子类声明访问,public主函数子类或父类声明都能用)
#include<iostream>
using namespace std;
class employee
{
protected:
int salary;
};
class programmer::public employee
{
public:
int bonus;
void setsalary(int x){
salary = x;
}
int getsalary(){
return salary;
}
};
int main ()
{
programmer pro;
pro.setsalary(50000);
pro.bonus = 15000;
cout << pro.getsalary() <<endl ;
return 0;
}
(9)多态性(这里是指动态多态)
静态多态也称为编译时多态,是指在编译时就能确定调用哪个方法。静态多态的实现方式主要是方法重载。方法重载指的是在同一个类中定义多个方法,这些方法具有相同的名称但参数列表不同。在调用方法时,编译器会根据传入的参数类型来确定要调用哪个方法。因为方法重载是在编译时决定的,所以它也被称为静态多态。
运行时多态也称为动态多态,是指在运行时才能确定调用哪个方法。运行时多态的实现方式主要是方法重写。方法重写指的是子类重写父类的方法,实现相同的方法名称和参数列表,在运行时根据实际类型来确定要调用哪个方法。因为方法重写是在运行时决定的,所以它也被称为运行时多态。
静态多态和运行时多态都是多态的表现形式,它们分别适用于不同的场景。静态多态通常用于实现同一类对象的不同行为,而运行时多态则通常用于实现不同类对象之间的相同行为。
————————————————
版权声明:本文为CSDN博主「aywala」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Minstrel223/article/details/130718962
#include<iostream>
using namespace std;
//方法重写
class animal
{
public:
void anima_sound(){
cout << "the animal makes a sound " <<endl;
}
};
class dog :: public animal
{
public:
void animal_sound(){
cout << “the dog says : wang wang ” << endl;
}
};
class pig :: public animal
{
public:
void animal_sound(){
cout << “the pig says : heng heng ” << endl;
}
};
int main()
{
animal mynanimal;
dog mydog;
pig mypig;
myanimal.animal_sound();//输出the animal makes a sound
mydog.animal_sound();//输出the dog says : wang wang
mypic.animal_sound();//输出the pig says : heng heng
return 0;
}
12、文件的读写
Tips:在文件读写时需要引进标准库#include<fstream>
好奇怪,这两个示例代码中,没有return 0;
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream myfile("filename.txt"); // 创建并打开txt文件,命名为filename
myfile << "files can be tricky,but it is fun enough!"; //写入文件
myfile.close(); //关闭文件
}
#include<iostream>
#include<fstream>
int main ()
{
ifstream myfile("filename.txt") ; //打开已有的文件,区分于ofstream
string mytext;
while(getline(myfile,mytext)) //getline是一个库函数,按行读出文件内容
{
cout << mytext <<endl;
}
myfile.close();//文件必须记得关闭
}
13、异常处理try……catch……
执行try中程序,如果抛出异常,则代入异常执行catch中程序
#include<iostream>
using namespace std;
int main ()
{
try
{
int age = 15;
if (age > 18)
{
cout << "Access granted.you are old enough.";
}
else
{
throw(age); //抛出整型异常,通常调用网络函数还是什么的才会有throw
}
}
catch(int num) //捕获整型异常
{
cout << “Access denied.You must be at least 18 years old.”<<endl;
cout << "your age is " << num <<endl;
}
try
{
int age = 15;
if (age > 18)
{
cout << "Access granted.you are old enough.";
}
else
{
throw 505; //抛出异常
}
}
catch(...) //捕获所有异常,管他是什么类型
{
cout << “Access denied.You must be at least 18 years old.”<<endl;
}
}
完整结束!