您好,欢迎来到抵帆知识网。
搜索
您的当前位置:首页【c++习题】【17416】动态分配内存

【c++习题】【17416】动态分配内存

来源:抵帆知识网
【c++习题】【17416】动态分配内存

#include#include#define N 100

using namespace std;

class String{public:

String(const string&);

void display() { cout<~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 #include #include

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 #include #include

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、下⾯是⼆维数组的动态创建。参考博客 ->

#include #include #include

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的对象的那个对象所属的类必须实现了对应的拷贝⽅法。。。 参考这⾥ ->

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

#include #include #include

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:⼦类访问⽗类的属性?

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- dfix.cn 版权所有 湘ICP备2024080961号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务