1.C++基本语法
- #include <iostream>
- using namepase std;
- int main(){
- cout<<"Hello Word!"endl;
- reutrn 0;
- }
2.强制类型转换
- double answer;
- answer = static_cast<double>(9)/2;
3.简单的文件操作
- #include <iostream>
- #include <fstream>
- int mian(){
- use namepace std;
- ifstream in_stream;
- ofstream out_stream;
- in_stream.open("infile.dat");
- out_stream.open("outfile.dat");
- int first,second,third;
- in_stream >> first >> second >> third;
- out_stream << first + second + third << endl;
- in_stream.close();
- out_stream.close();
- return 0;
- }
4.C++终止程序代码
- exit(1);
5.检查文件是否打开成功
- ifstream in_stream;
- if(in_stream.fail()){
- cout << "Opening failed";
- exit(1);
- }
6.追加到文件
- ofstream fout;
- fout.open("data.txt",);
7.eof函数
- if(!fin.eof())
- cout << "not done yet;";
- else
- cout << "end of file;";
8.结构体定义
- struct CDAccount{
- double balance;
- double interest_rate;
- };
- int main(){
- CDAccount account;
- int interest;
- account.balance = account.balance + interest;
- }
9.定义类和成员函数
- class DayOfYear{
- public:
- void output();
- };
- void DayOfYear::output(){
- 函数实现
- }
10.构造函数
- 是类的成员函数,每个类都有成员函数。
- 初始化类的对象,比如:分配空间,赋初值。
- 特点:构造函数必须与类名相同、没有数据类型、没有返回值。
- 构造函数可重载,故一个类可有N个构造函数。
- 类给出构造函数时,系统不再产生构造函数。
- 构造函数在声明时自动调用。
11.无参数构造函数
- BankAccount account;//正确。
- BankAccount account()//错误的。
12.内存空间的释放
- BankAccount *myAcct;
- myAcct = new BankAccount();
- delete myAcct;//释放内存空间
- 备注:delete只能释放自己分配的内存空间,new表示自己分配的内存空间。
- //错误例子
- int x = 5;
- delete x;
13.友元函数
- class DayOfYear(){
- public:
- friend bool equal(DayOfYear date1,DayOfYear date2);
- private:
- int month;
- int day;
- };
- bool equal(DayOfYear date1,DayOfYear date2){
- return (date1.month == date2.month && date1.day == date2.day);
- }
转载于:https://blog.51cto.com/okowo/1176636