C++ 初步
c++新特性:
1. 初始化方法:
复制初始化 int x=1024;
直接初始化 int x (1024);
2.变量随用随定义
3.增加bool数据类型
输入输出:
cout<<x;
cin>>x>>y;
cout<<oct<<x<<endl; //oct为8进制,dec为十进制,hex为16进制
cout<<boolalpha<<y<<endl; //输出布尔型数据
命名空间:
namespace A
{
int x=0;
void f1();
void f2();
}
A::x A::f1()
引用:变量的别名
int a=3;
int &b=a; 引用必须初始化
b=10;
结构体类型的引用:
typedef struct
{
int x;
int y;
}Coor;
Coor c1;
Coor &c=c1;
c.x;
指针类型的引用:
类型 *&指针引用名=指针;
int a=10;
int *p=&a;
int *&q=p;
*q=20; //a=20;
引用做函数参数:
void fun(int &a,int &b)
{
int c=0;
c=a;
a=b;
b=c;
}
int x=10,y=20;
fun(x,y);
const:
函数参数默认值:函数括号内最右端的变量必须有默认值 ,无实参用默认值,否则覆盖。
函数重载:在相同作用域下,有同名函数,参数个数和类型不同。
内联函数: 编译时将函数体内代码和实参代替函数调用语句。效率高,有条件
关键字:inline
内存管理:
申请:new int *p=new int; 释放:delete p;
块内存: int *arr=new int[10]; delete []arr;
注意事项:c语言:*malloc(),free()。
if(NULL==p){ // 申请失败 } delete p; p=NULL;
类:
对象实例化:
1.从栈实例化对象:class TV{ .... } TV tv; TV tv[20];
2.从堆实例化: TV *p =new TV(); delete p;
对象成员的访问:
1. 栈: tv.type=0; tv.func();
2.堆: p->tyoe=0; p->func(); delete p; p=NULL;
数据类型:
1.string:
#include<string>
初始化:string s1; s1为空串
string s2("ABC");
string s3(s2); 将s3初始化为s2的一个副本
string s4(n,'c'); 将s4初始化为字符‘c’的n个副本
常用操作: s.empty(), s.size() , s[n], = , + , == , !=
数据封装:
#include<stdlib.h> #include<iostream> #include<string> using namespace std; class Student { public: void setName(string _name) { m_strName=_name; } string getName() { return m_strName; } void setGender(string _gender) { m_strGender=_gender; } string getGender() { return m_strGender; } int getScore() { return m_iScore; } void initScore() { m_iScore=0; } void study(int _score) { m_iScore+=_score; } private: string m_strName; string m_strGender; int m_iScore; }; int main(void) { Student stu; stu.initScore(); stu.setName("hhhh"); stu.setGender("F"); stu.study(5); stu.study(3); cout<<stu.getName()<<" "<<stu.getGender()<<" "<<stu.getScore()<<endl; return 0; }
内联函数:inline
类内定义:类内定义的函数会优先转换为内联函数
类外定义:1.同文件类外定义
#include<stdlib.h> #include<iostream> #include<string> using namespace std; class Teacher { public: void setName(string _name); string getName(); void setGender(string _gender); string getGender(); void setAge(int _age); int getAge(); void teach(); private: string m_strName; string m_strGender; int m_iAge; }; void Teacher::setName(string _name){ m_strName=_name; } string Teacher::getName() { return m_strName; } void Teacher::setGender(string _gender){ m_strGender=_gender; } string Teacher::getGender(){ return m_strGender; } void Teacher::setAge(int _age){ m_iAge=_age; } int Teacher::getAge(){ return m_iAge; } void Teacher::teach(){ cout<<"teaching!...."<<endl; } int main(void) { Teacher t; t.setName("Kongzi"); t.setGender("M"); t.setAge(70); cout<<t.getName()<<" "<<t.getGender()<<" "<<t.getAge()<<endl; t.teach(); return 0; }
2.分文件类外定义
//teacher.h /* * teacher.h * * Created on: 2017年7月12日 * Author: rmbp */ #include <string> using namespace std; class Teacher { public: void setName(string _name); string getName(); void setGender(string _gender); string getGender(); void setAge(int _age); int getAge(); void teach(); private: string m_strName; string m_strGender; int m_iAge; }; //teacher.cpp /* * teacher.cpp * * Created on: 2017年7月12日 * Author: rmbp */ #include "teacher.h" #include<iostream> using namespace std; void Teacher::setName(string _name){ m_strName=_name; } string Teacher::getName() { return m_strName; } void Teacher::setGender(string _gender){ m_strGender=_gender; } string Teacher::getGender(){ return m_strGender; } void Teacher::setAge(int _age){ m_iAge=_age; } int Teacher::getAge(){ return m_iAge; } void Teacher::teach(){ cout<<"teaching!...."<<endl; } //hello.cpp #include<stdlib.h> #include<iostream> #include<string> #include "teacher.h" using namespace std; int main(void) { Teacher t; t.setName("Kongzi"); t.setGender("M"); t.setAge(70); cout<<t.getName()<<" "<<t.getGender()<<" "<<t.getAge()<<endl; t.teach(); return 0; }
内存分区:
栈区:系统控制
堆区:程序员控制
全局区:存储全局变量和静态变量
常量区:字符串,常量
代码区:存储逻辑代码的的二进制
构造函数:在对象实例化时被自动调用,与类名同名,没有返回值,可以重载,只用到一个构造函数,用户没有创建时系统自动创建。
//teacher.h #include <string> using namespace std; class Teacher { public: Teacher(); Teacher(string name,int age); //可以初始化参数值 void setName(string _name); string getName(); void setAge(int _age); int getAge(); private: string m_strName; string m_strGender; int m_iAge; }; //teacher.cpp #include "teacher.h" #include<iostream> using namespace std; Teacher::Teacher() { m_strNme="JIM"; m_iAge=7; cout<<"Teacher()"<<endl; } Teacher::Teacher(string name,int age){ m_strNme=name; m_iAge=age; cout<<"Teacher(string name,int age)"<<endl; } void Teacher::setName(string _name){ m_strName=_name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int _age){ m_iAge=_age; } int Teacher::getAge(){ return m_iAge; } //hello.cpp #include<stdlib.h> #include<iostream> #include<string> #include "teacher.h" using namespace std; int main(void) { Teacher t1; Teacher t2("Merry",16); cout<<t1.getName()<<" "<<t1.getAge()<<endl; cout<<t2.getName()<<" "<<t2.getAge()<<endl; return 0; }
默认构造函数:在对象实例化时不需要传递参数
构造函数初始化列表:先于构造函数执行,只能用于构造函数,可以同时初始化多个数据
class Student { public: Student():m_strName("JIM"),m_iAge(10){} private: string m_strName; int m_iAge; };
必要性:
class Circle { public: Circle():m_dPi(3.14){} //不能用构造函数,const private: const double m_dPi; }
//teacher.h #include <string> using namespace std; class Teacher { public: Teacher(string name="JIM",int age=1,int m=100); void setName(string _name); string getName(); void setAge(int _age); int getAge(); int getMax(); private: string m_strName; int m_iAge; const int m_iMax; }; //teacher.cpp #include "teacher.h" #include<iostream> using namespace std; Teacher::Teacher(string name,int age,int m):m_strName(name),m_iAge(age),m_iMax(m) { cout<<"Teacher(string name,int age)"<<endl; } void Teacher::setName(string _name){ m_strName=_name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int _age){ m_iAge=_age; } int Teacher::getAge(){ return m_iAge; } int Teacher::getMax(){ return m_iMax; } //hello.cpp #include<stdlib.h> #include<iostream> #include<string> #include "teacher.h" using namespace std; int main(void) { Teacher t1("merry",12); cout<<t1.getName()<<" "<<t1.getAge()<<" "<<t1.getMax()<<endl; return 0; }
拷贝构造函数: 类名(const 类名& 变量名)
1.当没有自定义拷贝构造函数则系统系统自动生成一个默认的拷贝构造函数。
2.当采用直接初始化或复制初始化实例化对象时系统自动调用拷贝构造函数。
无参构造函数,有参构造函数(参数带默认值)--》默认构造函数
系统自动生成普通构造函数和拷贝构造函数。不能重载。
//teacher.h Teacher(const Teacher& tea); //.... //teacher.cpp teacher::Teacher(const Teacher &tea){ cout<<"Teacher(const Teacher &tea)"<<endl; } //hello.cpp int main(void) { Teacher t1; Teacher t2=t1; Teacher t3(t1); //.... }
//hello.cpp void test(Teacher t) { } int main(void) { Teacher t1; //构造函数 test(t1); //拷贝构造函数 //.... }
析构函数:~类名(),不允许加任何参数,系统自动产生,在对象销毁时自动调用。没有返回值,没有参数不能重载
//teacher.h /* * teacher.h * * Created on: 2017年7月12日 * Author: rmbp */ #include <string> using namespace std; class Teacher { public: Teacher(string name="JIM",int age=1); //构造 Teacher(const Teacher &tea); //拷贝构造 ~Teacher(); //析构函数 void setName(string _name); string getName(); void setAge(int _age); int getAge(); private: string m_strName; int m_iAge; }; //teacher.cpp #include "teacher.h" #include<iostream> using namespace std; Teacher::Teacher(string name,int age):m_strName(name),m_iAge(age) { cout<<"Teacher(string name,int age)"<<endl; } Teacher::Teacher(const Teacher &tea) { cout<<"Teacher(const Teacher &tea)"<<endl; } Teacher:~Teacher() { cout<<"~Teacher()"<<endl; } void Teacher::setName(string _name){ m_strName=_name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int _age){ m_iAge=_age; } int Teacher::getAge(){ return m_iAge; } //hello.cpp #include<stdlib.h> #include<iostream> #include<string> #include "teacher.h" using namespace std; int main(void) { Teacher t1; //Teacher *p=new Teacher(); //delete p; Teacher t2(t1); return 0; }