文章目录
-
-
- 一、const关键字
- 二、static关键字
- 三、拷贝构造函数
- 四、运算符重载
- 五、输入输出流的重载
- 六、异常处理
- 七、IO流
一、const关键字
被const关键字修饰的代表是常量,只读,不可以修改,同时要求const对象必须初始化
#include "iostream"
using namespace std;
class Base{
void fucntion1() const;
};
class Person{
int age;
const string name;
Person(int age,string name):name(name){
this->age=age;
}
};
int main(){
const double min=9.9;
const int *p1;
int const *p2;
int i=10;
int * const p3=&i;
const int * const p4=&i;
return 0;
}
二、static关键字
#include "iostream"
using namespace std;
class Person{
private:
int age;
static int score;
public:
static void findPerson(Person perosn);
Person(int age){
this->age=age;
}
int getVisitors(){
static int count = 0;
return count++;
}
};
int Person::score=10;
void Person::findPerson(Person perosn) {
cout << "age:" << perosn.age << endl;
}
int main(){
Person person1(18);
cout << person1.getVisitors() <<endl;
Person person2(18);
cout << person2.getVisitors() <<endl;
Person::findPerson(person1);
return 0;
}
三、拷贝构造函数
在创建对象时,是使用之前创建的对象来初始化新创建的对象
如果类带有指针,并且动态内存分配,则必须有一个拷贝构造函数
如果在类中没有定义拷贝构造函数,编译器会自行定义一个
也可以自己写一个拷贝构造函数
#include
#include "iostream"
using namespace std;
class Student{
private:
int age;
char *name;
public:
void display(){
cout << "age:" << age << " name:" << name << endl;
}
Student(char *n,int a){
cout << "函数执行了" <<endl;
age=a;
name=n;
}
~Student(){
cout << "析构函数执行了" << endl;
if (name!= nullptr){
delete [] name;
name=NULL;
}
}
Student(const Student&s){
cout << "自定义拷贝构造函数执行" << endl;
age=s.age;
int len=sizeof(s.name);
name=new char[len+1];
strcpy(name,s.name);
name[len]='\0';
}
};
int main(){
Student s1("zwj1",20);
s1.display();
Student s2(s1);
s2.display();
return 0;
}
四、运算符重载
#include
#include "iostream"
using namespace std;
class Complex{
private:
int a;
int b;
public:
Complex(int a,int b){
this->a=a;
this->b=b;
}
void display(){
cout << "a:" <<a << " b:" << b << endl;
}
Complex& operator+=(const Complex another){
this->a+=another.a;
this->b+=another.b;
return *this;
}
int operator++(){
return this->a+100;
}
};
int main(){
Complex c1(2,3);
Complex c2(5,6);
(c1+=c2).display();
Complex c3(11,22);
cout << c3.operator++() << endl;
return 0;
}
五、输入输出流的重载
class Complex{
private:
int a;
int b;
public:
Complex(int a,int b){
this->a=a;
this->b=b;
}
void display(){
cout << "a:" <<a << " b:" << b << endl;
}
Complex& operator+=(const Complex another){
this->a+=another.a;
this->b+=another.b;
return *this;
}
int operator++(){
return this->a+100;
}
friend ostream & operator<<(ostream &os,Complex &c);
friend istream & operator>>(istream &is,Complex &c);
};
ostream & operator<<(ostream &os,Complex &c){
cout << "自定义<<:" << endl;
c.display();
return os;
}
istream & operator>>(istream &is,Complex &c){
cout << "请输入" <<endl;
cin >> c.a >> c.b ;
return is;
}
int main(){
Complex c1(2,3);
Complex c2(5,6);
(c1+=c2).display();
Complex c3(11,22);
cout << c3.operator++() << endl;
cin>>c1;
cout<<c1;
return 0;
}
六、异常处理
#include
#include "iostream"
using namespace std;
class MyException{
private:
string message;
public:
MyException(string message){
this->message=message;
}
string getstring(){
return message;
}
};
void FunDiv(int a,int b){
if (b==0){
throw "除数不能为0";
} else if(b==1){
throw MyException("nonono");
}
cout << "is OK" << endl;
int c=a/b;
}
int main(){
try {
FunDiv(1,1);
}catch (char const* a){
cout << a <<endl;
}catch (int a){
cout << a <<endl;
}catch (double a){
cout << a <<endl;
} catch (MyException myException) {
cout << myException.getstring() <<endl;
}
return 0;
}
七、IO流
#include
#include "iostream"
#include "fstream"
using namespace std;
int main(){
ifstream fin;
fin.open("/name.txt",ios::in);
fin.seekg(2,ios_base::beg);
char buffer[256];
while (fin.getline(buffer,256)) {
cout << buffer << endl;
}
ofstream fout;
fout.open("/name.txt",ios::out);
fout << "hello1111111111111afafaffasfsa1111111"<<endl;
fin.close();
fout.close();
return 0;
}