#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[];
for(int i = ; i != str.size(); ++i){
Head[i] = str[i];
}
} void String::re()
{
for(int i = , j = strlen(Head) - ; i < j; ++i, --j) {
swap(Head[i], Head[j]);
}
} int main()
{
string str = "hi~";
String s(str);
s.display();
s.re();
s.display();
return ;
}

一、动态分配内存。

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

char *Head;
Head = new char[];

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 = ; i != n; ++i) {
a[i] = i;
// 动态分配内存后表现得和数组一模一样~
cout << a[i] << endl;
}
// 表忘记删除
delete [] a;
printf("delete a!");
return ;
}

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

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
char *word;
word = new char[];
char c;
int i = ;
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 ;
}

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 = ; i != row; ++i)
p[i] = new int[col];
} void init(int **&p, int row, int col)
{
int k = ;
for (int i = ; i != row; ++i)
for (int j = ; j != col; ++j)
p[i][j] = k++;
} void delt(int **&p, int row)
{
for (int i = ; i != row; ++i)
delete [] p[i];
delete [] p;
} void print(int **&p, int row, int col)
{
for (int i = ; i != row; ++i) {
for (int j = ; 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 ;
} /*
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, ,fac), Bicycle(nl, np, nw, ,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, , 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, , 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, , fac), pov(pov) {
printf("Bus Constructor...\n");
}
void display() {
Motor::display();
printf("Bus[pov=%d]\n", pov);
}
}; int main()
{
Tool t(, , , "myTool");
t.display();
Motor m(, , , , "myMotor");
m.display();
Bicycle b(, , , , "myBicycle");
b.display();
motoBicycle mb(, , , , "myMotoBicycle");
mb.display();
Car c(, , , , "myCar");
c.display();
Truck tr(, , , , "myCar");
tr.display();
Bus bu(, , , , "myCar");
bu.display();
return ;
}
运行结果:
Tool Constructor...
tool[nl=,np=,nw=,fac=L]
Tool Constructor...
Motor Constructor...
tool[nl=,np=,nw=,fac=(]
motor[nm=]
Tool Constructor...
Bicycle Constructor...
tool[nl=,np=,nw=,fac=a]
Bicycle[comp=]
Tool Constructor...
Motor Constructor...
Bicycle Constructor...
motoBicycle Constructor...
tool[nl=,np=,nw=,fac=旋a]
motoBicycle[price=]
Tool Constructor...
Motor Constructor...
Car Constructor...
tool[nl=,np=,nw=,fac=橗a]
motor[nm=]
Car[pov=]
Tool Constructor...
Motor Constructor...
Truck Constructor...
tool[nl=,np=,nw=,fac=h齛]
motor[nm=]
Truck[pov=]
Tool Constructor...
Motor Constructor...
Bus Constructor...
tool[nl=,np=,nw=,fac=8齛]
motor[nm=]
Bus[pov=]

通过virtual解决二义性

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

【c++习题】【17/4/16】动态分配内存的更多相关文章

  1. c/c++动态分配内存和malloc的使用

    c/c++动态分配内存  为什么需要动态分配内存 ---很好的解决的了传统数组的4个缺陷 动态内存分配举例 ---动态数组的构造 使用动态数组的优点:    1. 动态数组长度不需要事先给定: 2. ...

  2. 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域

    [源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链 ...

  3. C++动态分配内存

    动态分配(Dynamic Memory)内存是指在程序运行时(runtime)根据用户输入的需要来分配相应的内存空间. 1.内存分配操作符new 和 new[] Example: (1)给单个元素动态 ...

  4. 标C编程笔记day06 动态分配内存、函数指针、可变长度參数

    动态分配内存:头文件 stdlib.h     malloc:分配内存     calloc:分配内存,并清零     realloc:调整已分配的内存块大小     演示样例:         in ...

  5. C&C++动态分配内存(手动分配内存)三种方式

    1. malloc函数 函数原型:void *malloc(unsigned int size)函数的作用是:在内训的动态存储区开辟一个size个字节的连续空间,返回所分配区域的首字节地址. 可以看到 ...

  6. mfc 动态分配内存

     动态内存分配new  为数组动态分配内存  为多维数组分配内存  释放内存delete malloc free  动态内存分配new int * pi; pi= new int ;  为 ...

  7. 指针和动态分配内存 (不定长度数组)------新标准c++程序设计

    背景: 数组的长度是定义好的,在整个程序中固定不变.c++不允许定义元素个数不确定的数组.例如: int n; int a[n]; //这种定义是不允许的 但是在实际编程中,往往会出现要处理的数据数量 ...

  8. 数据结构复习之C语言malloc()动态分配内存概述

    #include <stdio.h> #include <malloc.h> int main(void) { ] = {, , , , }; // 计算数组元素个数 ]); ...

  9. C++——动态分配内存问题

    class Obj { public: float score; ]; }; class Result { public: int id; ]; Obj obj[]; }; 合法,可动态分配内存给Re ...

随机推荐

  1. socket文件权限变更引起crs无法启动故障

    Crs无法正常启动,也无法关闭.[root@rac101 ~]# crsctl stop crsStopping resources. This could take several minutes. ...

  2. (一)Spring容器相关操作

    一.spring事件 spring的事件有如下两个成员. 1.ApplicationEvent,容器事件,由容器发布 2.ApplicationListener 监听器,可以由容器中的任何监听器Bea ...

  3. win7下安装memcached

    memcached server端服务在win7下的安装.启动图解 1.首先下载解压memcached-1.2.6-win32-bin.zip到某一盘下,如下图 2.通过管理员方式运行cmd.exe. ...

  4. PAT Advance 1020

    题目: 1020. Tree Traversals (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue S ...

  5. 51NOD 1013(3的幂的和)

    题目链接:传送门 题目大意:求(3^0+3^1+3^2+3^3+...+3^n)%1e9的值 题目思路:乘法逆元裸题 #include <iostream> #include <cs ...

  6. 【BZOJ4870】[Shoi2017]组合数问题 动态规划(矩阵乘法)

    [BZOJ4870][Shoi2017]组合数问题 Description Input 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < ...

  7. dubbo zookeeper报错failed to connect to server , error message is:No route to host

    failed to connect to server , error message is:No route to host 转自:http://blog.csdn.net/miaohongyu1/ ...

  8. 【CSS选择器】理解汇总和记录

    1.选择器中符号含义汇总(这部分包含了对选择器的通用理解): 1.1.多元素组合符号:(共6个,一个是CSS3的)(适用所有元素:ID组合,类组合,属性组合,标签组合,伪类组合,以及以上所有混合组合) ...

  9. Java——BeanUtils基本用法

    为了操作JavaBean的属性,sun公司自己写了一套内省的api(在Java.beans.*)中,但是我们发现操作起来还是比较复杂的,所以apache公司就自己写了一套api替代了它,大大方便了开发 ...

  10. 第三课作业——set类型、sorted set类型的增删改查,redis的事务

    第三课时作业 静哥 by 2016.2.23~2016.3.6   [作业描述] 1.总结什么是set以及什么是sorted set,并完成对set以及sorted set的增删改查(查需要至少4种方 ...