Posts 侯捷面向对象上
Post
Cancel

侯捷面向对象上

文件与类的声明

1.友元函数

const详解

/**
 * 笔记:
 *      关于const对用法:
 *          1. int method const(){
 *               xx
 *              }
 *              const  成员函数, 不可以修改成员变量。
 *
 *           2. 友元函数与友元类
 *              1. 友元函数,可以读取类的private数据。
 *              2. 友元类
 *
 */


class Point {
private:
    int x;
    int y;
public:
    Point(int x, int y) : x(x), y(y) {}
    friend void dist(Point &A,Point &B);
};

void dist(Point &A,Point &B){
    cout<< "x1 - x2 is: "<< A.x - B.x << endl;
    cout << "y1 - y2 is "<< A.y -B.y << endl;
}

// 友元函数
void test01(){
    Point A(12,15);
    Point B(4,5);
    dist(A,B);
}

// 友元类
class A{
private:
    int a;
public:
    friend class B;
};

class B{
private:
    A a;
public:
    B(const A &a) : a(a) {}

    void setA(int i){
        a.a = i;
    }
};



int main() {
//    1.友元函数
    test01();
//    2. 友元类问题

    return 0;
}

2. 类模版

3. 不带指针的构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
 * 不带指针的类 不用写析构函数
 */
class complex{
public:
    // 函数名冒号后直接初始化,效率会更高一点
    complex(double re, double im) : re(re), im(im) {}

    complex() :re(0),im(0){}

private:
    double re,im;
};

void test01(){
    complex c();
    complex c2(19,12);
}

int main() {


    return 0;
}

4. 参数传递与返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class complex{
private:
    double re,im;
public:
    complex(double re, double im) : re(re), im(im) {}

    complex& operator +(const complex &a){
        complex c(0,0);
        c.im = this->im + a.getIm();
        c.re = this->re + a.getRe();
        return  c;
    }

    // 加const表示,该函数不可修改private变量 不可修改成员变量
    double getRe() const {

        return re ;
    }
    double getIm() const {
        return im;
    }
};




//演示不加const类的问题
class com{
private:
    int a;
    int b;
public:
    com(int a, int b) : a(a), b(b) {}


    int getA() const  {

        return a;
    }

    int getB()  const {
        return b;
    }
};


int main() {
    complex c1(12,13);
    complex c2(10,10);
    complex c(0,0);
    c = c1 + c2;
    cout << c.getRe() << ":" << c.getIm() << endl;


    return 0;
}

5. 操作符重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class complex{
private:
    double re,im;
public:
    complex& operator += (complex& a){
        complex c;
        c.re = this->re + a.re;
        c.im = this->im + a.im;
        return c;
    }
    complex(double re = 0 , double im = 0 ) : re(re), im(im) {}
};


int main() {
    complex c1();
    complex c2(12,14);
    
    return 0;
}


7. 三大函数 拷贝 赋值 析构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace  std;


class String {
public:
    char * m_data;
public:
    String(const char* cstr=0){
        if (cstr)
        {
            m_data = new char[strlen(cstr) + 1];
            strcpy(m_data, cstr);
        }
        else
        {
            m_data = new char[1];
            *m_data = '\0';  //这里也许可以这样写:*m_data='';(网友观点)
        }
    }
    String(const String& str = 0){
        m_data = new char[strlen(str.m_data) + 1];
        strcpy(m_data,str.m_data);
    }
    String& operator=(const String& str){
        if(this == &str){
            return *this;
        }
        delete[] m_data;
        m_data = new char[strlen(str.m_data) + 1];
        strcpy(m_data, str.m_data);
        return *this;
    }


};

//  操作符重载
ostream& operator <<(ostream& os,const String& str){
    os << str.m_data ;
    return os;
}


int main() {
    String s("hello");
    String S2("jay");
    cout << s << endl;
    return 0;
}

9.String 类的实现过程

This post is licensed under CC BY 4.0 by the author.

Contents

Trending Tags