const成员
- ++引入了新的关键字const
- const数据成员
- const类型变量是不可以修改,只读模式
- 必须采用初始化参数列表方式进行初始化
- const成员函数
- 写法上, const写在函数后面
- 常成员函数是不能够修改数据成员,只读数据成员
- 常成员函数可以与普通函数同时存在
- 普通函数和常成员函数相同时,普通对象优先调用普通函数
- 普通对象可以调用常成员函数
- const对象,const修饰的对象
- 常对象只能调用常成员函数
#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
MM(string name, int age) :name(name), age(age) {}
void print()
{
cout << name << "\t我是普通函数\t" << age << endl;
}
void print()const//常函数
{
cout << name << "\t我是常成员函数\t" << age << endl;
}
protected:
string name;
const int age;//常成员
};
int main()
{
MM mm("对象",18);
mm.print();
const MM mm2("常对象",19);
mm2.print();
return 0;
}
static成员
- C++引入了新的关键字static
- static成员是不属于对象,是属于类的,意味着是所有对象共有的,调用可以不需要对象,当然你可以用对象调用
- static成员依旧受权限限定
- static数据成员
- 必须在类外初始化,不再需要static修饰,但是需要类名限定
- 类中初始化是错误的,不能采用初始化参数列表的方式初始化
- static成员函数
- static写在前面即可
- 调用非静态成员,必须要指定对象
- static对象释放是最后释放的
- static数据成员
#include<iostream>
#include<string>
#define NUM 18
using namespace std;
class MM
{
public:
MM(string name) :name(name) {}
void print()
{
cout << name<<" "<< age << endl;
}
protected:
string name;
public:
static int age;
};
int MM::age = NUM;//初始化static成员
int main()
{
cout << MM::age << endl;//可以直接使用,不需要对象
MM mm("小芳");
mm.age;
mm.print();
return 0;
}
友元函数
-
C++引入了新的关键字friend
-
友元只是提供一个场所,赋予对象具有打破类的权限定(无视权限)
-
普通友元函数
#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
MM(string name, int age, double money) :name(name), age(age), money(money) {}
void print()
{
cout << name << "\t" << age << "\t" << money << endl;
}
friend void printdata()
{
MM mm2("有缘对象",20,13.5);
cout << mm2.name << "\t" << mm2.age << "\t" << mm2.money << endl;
}
protected:
string name;
int age;
private:
double money;
};
void printdata();
int main()
{
MM mm("小芳",18,10.5);
mm.print();
printdata();//直接调用友元函数
return 0;
}
- 互为友元函数
* B类
* A类
* A类的友元函数(B类的数据成员)
* B类的友元函数(A类的数据成员)
#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
friend class GG;
void print();
protected:
string prin = "A";
};
class GG
{
public:
friend class MM;
void print()
{
MM mm;
cout << mm.prin << endl;
}
protected:
string prin = "B";
};
void MM::print()
{
GG gg;
cout << gg.prin << endl;
}
int main()
{
MM mm;
mm.print();
GG gg;
gg.print();
return 0;
}
this指针和explicit
- C++引入了新的关键字this
- this指针
- 避免形参名和数据成员同名
#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
MM(string name, int age) :name(name), age(age) {}
void printdata(string name,int age)
{
this->name = name;
this->age = age;
}
void print()
{
cout << this->name << "\t" << this->age << endl;
}
protected:
string name;
int age;
};
int main()
{
MM mm("小芳",18);
mm.print();
mm.printdata("小丽",20);
mm.print();
return 0;
}
- 充当函数返回值
#include<iostream>
#include<string>
using namespace std;
class MM
{
public:
MM(string name, int age) : name(name), age(age) {}
MM MMreturn()
{
return *this;//充当对象本身的返回值
}
void print()
{
cout << name << "\t" << age << endl;
}
protected:
string name;
int age;
};
int main()
{
MM mm("小芳",18);
mm.MMreturn().MMreturn().MMreturn().print();
return 0;
}
- 静态成员函数不能使用this指针
- C++引入了新的关键字explicit
- 作用是修饰构造函数使用,不让隐式转换构造
#include<iostream>
using namespace std;
class MM
{
public:
//MM(int age) :age(age) {}
explicit MM(int age) :age(age) {}
void print()
{
cout << age << endl;
}
protected:
int age;
};
int main()
{
/*MM mm = 10; 隐式转换
mm.print();*/
MM mm(12);
mm.print();
return 0;
}
谢谢大家的阅读,新手上路,如有不足之处请及时指出,我好纠正,万分感激~