- #include
- #include
- using namespace std;
- class Stu{
- private:
- char name[50];
- char sex[10];
- int age;
- public:
- Stu(){}
- Stu(char *n,char *s,int a):age(a)
- {
- strcpy(name,n);
- strcpy(sex,s);
- }
- void test01()
- {
- //以下两句代码效果相同
- //证明本类函数中调用本类成员默认使用this关键字
- cout << this->name << endl;
- cout << name << endl;
- }
- static void test02()
- {
- //报错,因为静态函数中没有this
- //cout << name << endl;
- }
- };
- int main(int argc, char *argv[])
- {
- Stu s("张三","男",18);
- s.test01();
- s.test02();
- return 0;
- }
- #include
- #include
- using namespace std;
- class Stu{
- private:
- char name[50];
- char sex[10];
- int age;
- public:
- Stu(){}
- Stu(char *name,char *sex,int age)
- {
- //当局部变量与成员变量重名,使用this区分
- strcpy(this->name,name);
- strcpy(this->sex,sex);
- this->age = age;
- }
- void print_info()
- {
- //调用本类成员变量
- cout << this->name << endl;
- cout << this->sex << endl;
- cout << this->age << endl;
- }
- void test()
- {
- //调用本类成员函数
- this->print_info();
- }
- };
- int main(int argc, char *argv[])
- {
- Stu s("张三","男",18);
- s.print_info();
- s.test();
- return 0;
- }
- #include
- #include
- using namespace std;
- class Stu{
- private:
- char name[50];
- char sex[10];
- int age;
- public:
- Stu(){}
- Stu(char *name,char *sex,int age)
- {
- strcpy(this->name,name);
- strcpy(this->sex,sex);
- this->age = age;
- }
- void print_info()
- {
- cout << this->name << endl;
- cout << this->sex << endl;
- cout << this->age << endl;
- }
- Stu& eat(char *foodName)
- {
- cout << name << "吃" << foodName << endl;
- return *this;
- }
- };
- int main(int argc, char *argv[])
- {
- Stu s("张三","男",18);
- s.eat("凉皮").eat("肉夹馍").eat("甑糕");
- return 0;
- }
特点
实例
- class Stu{
- private:
- char name[50];
- char sex[10];
- int age;
- mutable int score;
- public:
- Stu(){}
- Stu(char *name,char *sex,int age)
- {
- strcpy(this->name,name);
- strcpy(this->sex,sex);
- this->age = age;
- }
- void print_info()
- {
- cout << this->name << endl;
- cout << this->sex << endl;
- cout << this->age << endl;
- }
- Stu& eat(char *foodName)
- {
- cout << name << "吃" << foodName << endl;
- return *this;
- }
- void test() const
- {
- //age = 10;//错误
- score = 99;//正确
- }
- };
特点
步骤
示例
- #include
- #include
- using namespace std;
- class Stu{
- friend void test(Stu &stu);
- private:
- char name[50];
- char sex[10];
- int age;
- public:
- Stu(){}
- Stu(char *name,char *sex,int age)
- {
- strcpy(this->name,name);
- strcpy(this->sex,sex);
- this->age = age;
- }
- void print_info()
- {
- cout << this->name << endl;
- cout << this->sex << endl;
- cout << this->age << endl;
- }
- private:
- void eat(char *foodName)
- {
- cout << name << "吃" << foodName << endl;
- }
- };
- void test(Stu& stu)
- {
- //调用友元类的私有属性
- cout << stu.name << endl;
- cout << stu.sex << endl;
- cout << stu.age << endl;
- //调用友元类的私有函数
- stu.eat("大嘴巴子");
- }
- int main(int argc, char *argv[])
- {
- Stu s("张三","男",18);
- test(s);
- return 0;
- }
特点
注意
步骤
示例
- #include
- #include
- using namespace std;
- //定义B类,但是没有实现
- class B;
- class A{
- public:
- void test(B& b);
- };
- class B{
- friend void A::test(B& b);
- private:
- int a;
- public:
- B(int a)
- {
- this->a = a;
- }
- private:
- void print_B()
- {
- cout << "a = " << a << endl;
- }
- };
- void A::test(B& b)
- {
- cout << b.a << endl;
- b.print_B();
- }
- int main(int argc, char *argv[])
- {
- A a;
- B b(10);
- a.test(b);
- return 0;
- }
特点
步骤
示例
- #include
- #include
- using namespace std;
- //定义B类,但是没有实现
- class B;
- class A{
- public:
- void test01(B& b);
- void test02(B& b);
- };
- class B{
- friend class A;
- private:
- int a;
- public:
- B(int a)
- {
- this->a = a;
- }
- private:
- void print_B()
- {
- cout << "a = " << a << endl;
- }
- };
- void A::test01(B& b)
- {
- cout << "test01" << endl;
- cout << b.a << endl;
- b.print_B();
- }
- void A::test02(B& b)
- {
- cout << "test02" << endl;
- cout << b.a << endl;
- b.print_B();
- }
- int main(int argc, char *argv[])
- {
- A a;
- B b(10);
- a.test01(b);
- cout << "--------------------" << endl;
- a.test02(b);
- return 0;
- }
说明
代码
- #include
- #include
- using namespace std;
- class TV;
- class YK{
- public:
- void up(TV& tv);
- void down(TV& tv);
- };
- class TV{
- friend class YK;
- private:
- int yl;
- public:
- TV(){}
- TV(int yl)
- {
- this->yl = yl;
- }
- };
- void YK::up(TV &tv)
- {
- tv.yl++;
- cout << "当前音量:" << tv.yl << endl;
- }
- void YK::down(TV &tv)
- {
- tv.yl--;
- cout << "当前音量:" << tv.yl << endl;
- }
- int main(int argc, char *argv[])
- {
- TV tv(10);
- YK yk;
- yk.up(tv);
- yk.up(tv);
- yk.up(tv);
- yk.down(tv);
- yk.down(tv);
- yk.down(tv);
- return 0;
- }
- #include
- #include
- using namespace std;
- int main(int argc, char *argv[])
- {
- string str01 = "hello";
- string str02 = str01;//字符串赋值
- cout << str01 << endl;//字符串输出
- cout << str02 << endl;
- str02 = "world";
- cout << str01 << endl;
- cout << str02 << endl;
- string str03 = str01 + str02;//字符串拼接
- cout << str03 << endl;
- string str04;
- cin >> str04;//字符串输入
- cout << str04 << endl;
- string str05 = "Hi C++";
- string str06 = "Hi C++";
- string str07 = "Hi C";
- cout << (str05 == str06) << endl;//判断字符串内容是否相同
- cout << (str05 == str07) << endl;
- cout << &str05 << endl;//打印str05地址
- cout << &str06 << endl;//打印str06地址
- return 0;
- }
作用
关键字
operator
语法
- #include
- #include
- using namespace std;
- class Data{
- public:
- int x,y,z;
- Data(){}
- Data(int a,int b,int c):x(a),y(b),z(c){}
- };
- //第一个参数为运算符左边的变量
- //第二个参数为运算符右边的变量
- istream& operator >>(istream& in,Data& d)
- {
- in >> d.x >> d.y >> d.z;
- return in;
- }
- ostream& operator <<(ostream& out,Data& d)
- {
- out << "x = " << d.x << "\ty = " << d.y << "\tz = " << d.z << endl;
- return out;
- }
- int main(int argc, char *argv[])
- {
- Data d;
- cin >> d;
- cout << d << endl;
- return 0;
- }
效果
分析
示例:全局函数重载
- #include
- using namespace std;
- class Data{
- public:
- int x,y,z;
- Data(){}
- Data(int a,int b,int c):x(a),y(b),z(c){}
- };
- //第一个参数为运算符左边的变量
- //第二个参数为运算符右边的变量
- Data* operator +(Data& d1,Data& d2)
- {
- Data *d = new Data();
- d->x = d1.x + d2.x;
- d->y = d1.y + d2.y;
- d->z = d1.z + d2.z;
- return d;
- }
- int main(int argc, char *argv[])
- {
- Data d1(1,2,3);
- Data d2(1,2,3);
- Data* d3 = d1 + d2;
- cout << d3->x << d3->y << d3->z << endl;
- return 0;
- }
- #include
- using namespace std;
- class Data{
- public:
- int x,y,z;
- Data(){}
- Data(int a,int b,int c):x(a),y(b),z(c){}
- //调用该函数的对象为运算符左边的变量
- //参数为运算符右边的变量
- Data* operator +(Data& d2)
- {
- Data *d = new Data();
- d->x = this->x + d2.x;
- d->y = this->y + d2.y;
- d->z = this->z + d2.z;
- return d;
- }
- };
- int main(int argc, char *argv[])
- {
- Data d1(1,2,3);
- Data d2(1,2,3);
- Data* d3 = d1 + d2;
- cout << d3->x << d3->y << d3->z << endl;
- return 0;
- }
- #include
- using namespace std;
- class Data{
- public:
- int x,y,z;
- Data(){}
- Data(int a,int b,int c):x(a),y(b),z(c){}
- //调用该函数的对象为运算符左边的变量
- //参数为运算符右边的变量
- bool operator ==(Data& d2)
- {
- if(this->x == d2.x && this->y == d2.y && this->z == d2.z)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- };
- int main(int argc, char *argv[])
- {
- Data d1(1,2,3);
- Data d2(1,2,3);
- Data d3(2,2,3);
- cout << (d1 == d2) << endl;
- cout << (d1 == d3) << endl;
- return 0;
- }
注意
示例
- #include
- using namespace std;
- class Data{
- public:
- int x,y,z;
- Data(){}
- Data(int a,int b,int c):x(a),y(b),z(c){}
- Data& operator ++()//++前置
- {
- ++x;
- ++y;
- ++z;
- return *this;
- }
- Data operator ++(int)//++后置
- {
- Data old = *this;//记录旧值
- ++x;
- ++y;
- ++z;
- return old;//返回旧值
- }
- };
- ostream& operator <<(ostream& out,Data& d)
- {
- out << d.x << d.y << d.z << endl;
- return out;
- }
- int main(int argc, char *argv[])
- {
- Data d1(1,2,3);
- ++d1;
- cout << d1;
- Data d2(1,2,3);
- Data d3 = d2++;
- cout << d3;
- cout << d2;
- return 0;
- }
要求
推演
- #include
- using namespace std;
- class Data{
- private:
- int x,y,z;
- public:
- Data(){
- cout << "无参构造函数" << endl;
- }
- Data(int a,int b,int c):x(a),y(b),z(c){
- cout << "有参构造函数" << endl;
- }
- ~Data()
- {
- cout << "析构函数" << endl;
- }
- };
- int main(int argc, char *argv[])
- {
- Data *p = new Data();
- return 0;
- }
- #include
- using namespace std;
- class Data{
- private:
- int x,y,z;
- public:
- Data(){
- cout << "无参构造函数" << endl;
- }
- Data(int a,int b,int c):x(a),y(b),z(c){
- cout << "有参构造函数" << endl;
- }
- ~Data()
- {
- cout << "析构函数" << endl;
- }
- };
- class FreeData{
- private:
- Data* p;
- public:
- FreeData(){
- p = NULL;
- }
- FreeData(Data* data){
- p = data;
- }
- ~FreeData(){
- if(p != NULL)
- {
- delete p;
- p = NULL;
- }
- }
- };
- int main(int argc, char *argv[])
- {
- FreeData fd(new Data(1,2,3));
- return 0;
- }
- #include
- using namespace std;
- class Data{
- private:
- int x,y,z;
- public:
- Data(){
- cout << "无参构造函数" << endl;
- }
- Data(int a,int b,int c):x(a),y(b),z(c){
- cout << "有参构造函数" << endl;
- }
- ~Data()
- {
- cout << "析构函数" << endl;
- }
- int getX()
- {
- return x;
- }
- };
- class FreeData{
- private:
- Data* p;
- public:
- FreeData(){
- p = NULL;
- }
- FreeData(Data* data){
- p = data;
- }
- ~FreeData(){
- if(p != NULL)
- {
- delete p;
- p = NULL;
- }
- }
- Data& operator *()
- {
- return *p;
- }
- Data* operator ->()
- {
- return p;
- }
- };
- int main(int argc, char *argv[])
- {
- FreeData fd(new Data(1,2,3));
- cout << (*fd).getX() << endl;
- cout << fd->getX() << endl;
- return 0;
- }
作用
示例
- #include
- using namespace std;
- class Data{
- friend ostream& operator <<(ostream& out,Data& d);
- private:
- int x,y,z;
- public:
- Data(){
- cout << "无参构造函数" << endl;
- }
- Data(int a,int b,int c):x(a),y(b),z(c){
- cout << "有参构造函数" << endl;
- }
- ~Data()
- {
- cout << "析构函数" << endl;
- }
- void operator ()(int a,int b,int c){
- this->x += a;
- this->y += b;
- this->z += c;
- }
- };
- ostream& operator <<(ostream& out,Data& d)
- {
- out << d.x << "\t" << d.y << "\t" << d.z << endl;
- return out;
- }
- int main(int argc, char *argv[])
- {
- Data d(1,2,3);
- d(2,5,8);
- cout << d;
- return 0;
- }
- #include
- using namespace std;
- class Data{
- friend ostream& operator <<(ostream& out,Data& d);
- private:
- int x,y,z;
- public:
- Data(){
- cout << "无参构造函数" << endl;
- }
- Data(int a,int b,int c):x(a),y(b),z(c){
- cout << "有参构造函数" << endl;
- }
- Data(const Data& d)
- {
- cout << "执行拷贝构造" << endl;
- this->x = d.x;
- this->y = d.y;
- this->z = d.z;
- }
- ~Data()
- {
- cout << "析构函数" << endl;
- }
- void operator =(Data& d){
- cout << "执行重载=运算符的函数" << endl;
- this->x = d.x;
- this->y = d.y;
- this->z = d.z;
- }
- };
- ostream& operator <<(ostream& out,Data& d)
- {
- out << d.x << "\t" << d.y << "\t" << d.z << endl;
- return out;
- }
- int main(int argc, char *argv[])
- {
- Data d1(1,2,3);
- Data d2(3,6,9);
- d1 = d2;//d1已完成初始化,执行重载的=号运算符
- Data d3 = d2;//d3未完成初始化,执行拷贝构造
- return 0;
- }
注意: