• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

【c++习题】【17/4/16】动态分配内存

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
#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", 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 << '\n';
    delete [] word;
    cout << "Delete word:" << endl;
    cout << "word:" << word << '\n';
    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...\n");
    }
    void display() {
        printf("tool[nl=%d,np=%d,nw=%d,fac=%s]\n", 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...\n");
    }
    void display() {
        Tool::display();
        printf("motor[nm=%d]\n", 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...\n");
    }
    void display() {
        Tool::display();
        printf("Bicycle[comp=%d]\n", 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...\n");
    }
    void display() {
        Tool::display();
        printf("motoBicycle[price=%d]\n", 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...\n");
    }
    void display() {
        Motor::display();
        printf("Car[pov=%d]\n", 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...\n");
    }
    void display() {
        Motor::display();
        printf("Truck[pov=%d]\n", 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...\n");
    }
    void display() {
        Motor::display();
        printf("Bus[pov=%d]\n", 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:子类访问父类的属性?


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c++精简版fps限制发布时间:2022-07-13
下一篇:
用C#创建Windows服务(WindowsServices)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap