zoukankan      html  css  js  c++  java
  • 【c++习题】【17/4/16】动态分配内存

    #include<iostream>
    #include<cstring>
    #define N 100
    using namespace std;
    
    class String{
    public:
        String(const string&);
        void display() { cout<<Head<<endl; }
        void re();
        ~String() { delete[] Head; }
    private:
        char *Head;
    };
    
    String::String(const string &str)
    {
        Head = new char[100];
      for(int i = 0; i != str.size(); ++i){
            Head[i] = str[i];
        }
    }
    
    void String::re()
    {
        for(int i = 0, j = strlen(Head) - 1; i < j; ++i, --j) {
            swap(Head[i], Head[j]);
        }
    }
    
    int main()
    {
        string str = "hi~";
        String s(str);
        s.display();
        s.re();
        s.display();
        return 0;
    }

    一、动态分配内存。

    1、像上面那样:a-动态分配,通常在构造器里完成。

    char *Head;
    Head = new char[100];

    b-删除。

     ~String() { delete[] Head; }

    越界访问的结果是未知的。

    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        int *a;
        a = new int[n];
        printf("size of a=%d
    ", n);
        for (int i = 0; i != n; ++i) {
            a[i] = i;
            // 动态分配内存后表现得和数组一模一样~
            cout << a[i] << endl;
        }
        // 表忘记删除
        delete [] a;
        printf("delete a!");
        return 0;
    }

    2、还有个技巧就是用来重新初始化,

    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    int main()
    {
        char *word;
        word = new char[100];
        char c;
        int i = 0;
        cout << "Create word array and enter the words." << endl;
        while (cin >> c) {
            word[i++] = c;
        }
        cout << "word:" << word << '
    ';
        delete [] word;
        cout << "Delete word:" << endl;
        cout << "word:" << word << '
    ';
        return 0;
    }

    char *word在c++中表现得和常量字符串是一样的。

    Create word array and enter the words.
    ?@#!@#!@#!@#
    ^Z
    word:?@#!@#!@#!@#
    Delete word:
    word:

    3、下面是二维数组的动态创建。参考博客 -> here

     

    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    
    void create(int **&p, int row, int col)
    {
        p = new int*[row]; // p是一个指向、指向int的指针的、指针
        for (int i = 0; i != row; ++i) 
            p[i] = new int[col];
    }
    
    void init(int **&p, int row, int col)
    {
        int k = 0;
        for (int i = 0; i != row; ++i) 
            for (int j = 0; j != col; ++j) 
                p[i][j] = k++;
    }
    
    void delt(int **&p, int row)
    {
        for (int i = 0; i != row; ++i)
            delete [] p[i];
        delete [] p;
    }
    
    void print(int **&p, int row, int col)
    {
        for (int i = 0; i != row; ++i) {
            for (int j = 0; j != col; ++j) 
                printf("%d ", p[i][j]);
            cout << endl;
        }
    }
    
    int main()
    {
        int row, col;
        int **p; // int* *p;
        cin >> row >> col;
        create(p, row, col);
        init(p, row, col);
        print(p, row, col);
        delt(p, row);
        return 0;
    }
    
    /*
    ps:
    
        尝试使用range for: 失败
        for (int x : p) {
            cout << p << " ";
        }
        
    */

    二、把非模板类改成模板类。

    。。Java里的对象只要需要就一直存在,而c++中的local对象的生命周期仅限花括号内,要接收函数内return的对象的那个对象所属的类必须实现了对应的拷贝方法。。。

     参考这里 -> click here!

    三、c++继承。(仿Java)

    #include <iostream>
    #include <cstdio>
    #include <string>
    using namespace std;
    class Tool {
    private:
        int nl, np, nw;
        string fac;
    public:
        Tool(int nl, int np, int nw, string fac): nl(nl), np(np), nw(nw), fac(fac) {
            printf("Tool Constructor...
    ");
        }
        void display() {
            printf("tool[nl=%d,np=%d,nw=%d,fac=%s]
    ", nl, np, nw, fac);
        }
    };
    
    class Motor : virtual public Tool {
    private:
        int nm;
    public:
        Motor(int nl, int np, int nw, int nm, string fac):  Tool(nl, np, nw, fac), nm(nm) {
            printf("Motor Constructor...
    ");
        }
        void display() {
            Tool::display();
            printf("motor[nm=%d]
    ", nm);
        }
    };
    
    class Bicycle : virtual public Tool {
    private:
        int comp;
    public:
        Bicycle(int nl, int np, int nw, int comp, string fac):  Tool(nl, np, nw, fac), comp(comp) {
            printf("Bicycle Constructor...
    ");
        }
        void display() {
            Tool::display();
            printf("Bicycle[comp=%d]
    ", comp);
        }
    };
    
    class motoBicycle : public Motor, public Bicycle {
    private:
        int price;
    public:
        motoBicycle(int nl, int np, int nw, int price, string fac):  
        Tool(nl, np, nw, fac), Motor(nl, np, nw, 0,fac), Bicycle(nl, np, nw, 0,fac),
        price(price) {
            printf("motoBicycle Constructor...
    ");
        }
        void display() {
            Tool::display();
            printf("motoBicycle[price=%d]
    ", price);
        }    
    };
    
    class Car : public Motor {
    private:
        int pov;
    public:
        Car(int nl, int np, int nw, int pov, string fac):
        Tool(nl, np, nw, fac),
        Motor(nl, np, nw, 0, fac), pov(pov) {
            printf("Car Constructor...
    ");
        }
        void display() {
            Motor::display();
            printf("Car[pov=%d]
    ", pov);
        }
    };
    
    class Truck : public Motor {
    private:
        int pov;
    public:
        Truck(int nl, int np, int nw, int pov, string fac):
        Tool(nl, np, nw, fac),
        Motor(nl, np, nw, 0, fac), pov(pov) {
            printf("Truck Constructor...
    ");
        }
        void display() {
            Motor::display();
            printf("Truck[pov=%d]
    ", pov);
        }
    };
    
    class Bus : public Motor {
    private:
        int pov;
    public:
        Bus(int nl, int np, int nw, int pov, string fac):
        Tool(nl, np, nw, fac),
        Motor(nl, np, nw, 0, fac), pov(pov) {
            printf("Bus Constructor...
    ");
        }
        void display() {
            Motor::display();
            printf("Bus[pov=%d]
    ", pov);
        }
    };
    
    int main()
    {
        Tool t(4, 4, 250, "myTool");
        t.display();
        Motor m(2, 2, 150, 100, "myMotor");
        m.display();
        Bicycle b(2, 2, 100, 2, "myBicycle");
        b.display();
        motoBicycle mb(2, 2, 100, 1700, "myMotoBicycle");
        mb.display();
        Car c(1, 2, 3, 4, "myCar");
        c.display();
        Truck tr(1, 2, 3, 4, "myCar");
        tr.display();
        Bus bu(1, 2, 3, 4, "myCar");
        bu.display();
        return 0;
    }
    运行结果:
    Tool Constructor...
    tool[nl=4,np=4,nw=250,fac=L�]
    Tool Constructor...
    Motor Constructor...
    tool[nl=2,np=2,nw=150,fac=(�]
    motor[nm=100]
    Tool Constructor...
    Bicycle Constructor...
    tool[nl=2,np=2,nw=100,fac=�a]
    Bicycle[comp=2]
    Tool Constructor...
    Motor Constructor...
    Bicycle Constructor...
    motoBicycle Constructor...
    tool[nl=2,np=2,nw=100,fac=旋a]
    motoBicycle[price=1700]
    Tool Constructor...
    Motor Constructor...
    Car Constructor...
    tool[nl=1,np=2,nw=3,fac=橗a]
    motor[nm=0]
    Car[pov=4]
    Tool Constructor...
    Motor Constructor...
    Truck Constructor...
    tool[nl=1,np=2,nw=3,fac=h齛]
    motor[nm=0]
    Truck[pov=4]
    Tool Constructor...
    Motor Constructor...
    Bus Constructor...
    tool[nl=1,np=2,nw=3,fac=8齛]
    motor[nm=0]
    Bus[pov=4]

    通过virtual解决二义性

    q:子类访问父类的属性?

  • 相关阅读:
    9-python 的ProxyHandler处理器(代理设置)
    2018.2.7 css 的一些方法盒子模型
    2018.2.6 JS-判断用户浏览器
    2018.2.5 PHP如何写好一个程序用框架
    2018. 2.4 Java中集合嵌套集合的练习
    2018.2.3 Centos 的vim好看的主题配置及JDK的安装配置
    2018.2.2 java中的Date如何获取 年月日时分秒
    2018.2.2 JavaScript中的封装
    2018.1.30 PHP编程之验证码
    2018.1.29 计算机二级错题汇总(二)
  • 原文地址:https://www.cnblogs.com/xkxf/p/6719544.html
Copyright © 2011-2022 走看看