C++基础_05

来源:互联网 发布:siam journal 知乎 编辑:程序博客网 时间:2024/05/17 08:41
const 修饰类的成员函数
#include <iostream>using namespace std;class Test{public :    void getNum() const {        /*            void getNum() const <==> void getNum(const Test * const this);            1)const修饰成员函数实际上修饰的是(this)指针            2)const修饰成员函数与const出现的位置无关,以下三种写法一致:                const void getNum();                void const getNum();                void getNum() const;            3)const Test * const this ==> this指针的指向和所指向的内存均无法被修改            4)static void getNum() const//Error(被static修饰的函数并没有this指针,无法再被const修饰)        *///        this->num = 100;//Error  //        this = 0x100;//Error        cout<<"Num = "<<this->num<<endl;    }private :    int num;};
/*class的返回值*/#include <iostream>using namespace std;class Test{public :    Test (int a = 0,int b = 0){        this->a = a;        this->b = b;        cout<<"Test(int ,int )"<<endl;    }    ~Test(){        cout<<"~Test()"<<endl;    }public:    //返回一个副本    Test getTest_1(const Test & t){        *this = Test(this->a + t.a , this->b + t.b);        return *this; //返回一个匿名对象    }    //返回引用    Test & getTest_2(const Test & t){        *this = Test(this->a + t.a , this->b + t.b);        return *this; //返回一个匿名对象    }    //返回一个指向类的指针    Test * getTest_3(const Test & t){        *this = Test(this->a + t.a , this->b + t.b);        return this;//返回一个匿名对象    }public:    void show() const{        cout<<"A = "<<a<<" B = "<<b<<endl;    }private:    int a;    int b;};int main(int argc, char *argv[]){//    Test t1(1,3);//    Test t2(2,4);//    t1.show();//    t2.show();//    t1 = t1.getTest_1(t2);////    t1 = t1.getTest_2(t2);////    t1.show();//    Test * t3 = new Test;//    t3 = t1.getTest_3(t2);//    t3->show();    return 0;}

友元函数

/*友元函数*/#include <iostream>using namespace std;/*友元函数:可以在类的外部访问一个类的私有属性    1)使用friend关键字    2)声明时必须加入该类的类型    3)友元函数是全局函数*/class Test{public:    Test(int num){        this->num = num;    }    friend void setTest_Num(Test * t,int num);//友元函数public:    void showNum(){        cout<<"Num = "<<num<<endl;    }private:    int num;};void setTest_Num(Test * t,int num){        t->num = num;}int main(int argc, char *argv[]){    Test t(100);    t.showNum();    setTest_Num(&t,50);    t.showNum();    return 0;}

友元类

/*友元类的使用*/#include <iostream>using namespace std;class A{public:    //友元类    friend class B;public:    void show(){        cout<<"A::NUM = "<<this->num<<endl;    }private:    int num;};class B{public:    B(int num){        Test_A.num = num;    }public:    void show(){        Test_A.show();    }private :    A Test_A;};int main(int argc, char *argv[]){    B Test_B(100);    Test_B.show();    return 0;}
/*  成员函数和友元函数实现运算符重载*/#include <iostream>using namespace std;class Test{public:    Test (int a,int b):a(a),b(b){    }    ~Test(){    }public:    //成员函数重载    Test operator-(Test & t){//==>  Test operator- (this,Test & t);        Test temp (this->a - t.a,this->b - t.b);        return temp;    }    //重载前缀++    Test & operator++(){        this->a++;        this->b++;        return *this;    }    //重载后缀++    Test operator++(int){        Test temp = *this;        this->a++;        this->b++;        return temp;    }    //有缘函数重载    friend Test operator+(Test & t1, Test & t2){        Test temp(t1.a + t2.a,t1.b + t2.b);        return temp;    }    //重载前缀--    friend Test & operator--(Test & t){        t.a--;        t.b--;        return t;    }    //重载后缀--    friend Test operator--(Test & t,int){        Test temp = t;        t.a--;        t.b--;        return temp;    }public:    void show(){        cout<<"("<<this->a<<" + "<< this->b <<"i)"<<endl;    }private:    int a;    int b;};int main(int argc, char *argv[]){    Test t1(5,10);    Test t2(11,22);//    Test t3 = t1 - t2;// ==> t1.operator-(t2);    Test t3 = t1 + t2;    t3.show();    ++t3;    t3.show();    --t3;    t3.show();    t3++;    t3.show();    t3--;    t3.show();    return 0;}
/* * 重载<< and >>*/#include <iostream>using namespace std;class Test{public:    Test(int a,int b):a(a),b(b){    }    ~Test(){    }public:    friend ostream & operator<<(ostream & out,Test & t){        out<<"[@"<<&t<<"]("<<t.a<<"+"<<t.b<<"i)"<<endl;        return out;    }private:    int a;        int b;};int main(int argc, char *argv[]){    Test t(2,5);    cout<<t;    return 0;}
/*重载 "=" 操作符*/#include <iostream>#include <string.h>#include <stdlib.h>#include <ctype.h>using namespace std;class Test{public:    //构造函数    Test (char * str){        cout<<"Test(char *)"<<endl;        this->Length = strlen(str);        this->str = new char[this->Length + 1];        strcpy(this->str,str);    }    //析构函数    ~Test(){        cout<<"~Test()"<<endl;        delete [] str;        this->Length = 0;    }    //拷贝构造函数    Test(const Test & t){        cout<<"Test(const Test & t)"<<endl;        this->Length = strlen(t.str);        this->str = new char[this->Length + 1];        strcpy(this->str,t.str);    }    //赋值操作函数    Test & operator=(Test & t){        cout<<"Test & operator=(Test & t)"<<endl;        delete this;        this->Length = t.Length;        this->str = new char[this->Length + 1];        strcpy(this->str,t.str);        return *this;    }public:    void show(){        cout<<"string = "<<this->str<<endl;    }private:    char * str;    int Length;};int main(int argc, char *argv[]){    Test t1("Hello"); //构造函数    t1.show();    Test t2("world");    t2.show();    Test t3(t2); //拷贝构造函数    t3.show();    t3 = t2 = t1; //赋值函数    t3.show();    return 0;}
0 0
原创粉丝点击