#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. VS本地调试oracle报错解决方法

    同事的项目,SVN下载下来以后一直报错,后来确认一下 1本地要安装oracle 2代码用的是64位的,所以本地安装也要64位的oracle 3VS调试用的IIS Express也要是64位的,激活方法 ...

  2. Linux系统常用工具集

    整理Linux系统下一些日常工作中常用工具,旨在提高效率: 1.截图软件Shutter 2.通讯聊天工具pidgin 3.守护进程工具daemontools 4.远程桌面服务TigerVNC 5.Ma ...

  3. 隐式意图调用系统自带组件的各种Uri总结

    调用系统应用解析(必需要加各自使用的权限)  android intent 隐式意图和显示意图(activity跳转) 显示意图要求必须知道被激活组件的包和class 隐式意图仅仅须要知道跳转acti ...

  4. Appcompat实现Action Bar的兼容性处理

    Appcompat实现Action Bar时,如果使用到split action bar或者Navigating Up with the App Icon需要考虑兼容性.下面介绍下split acti ...

  5. Powershell Exchange Server UP Time

    Server up time Get-ExchangeServer | where{$_.name -like'wendy*'} | %{ if(Test-Connection $_.name -Co ...

  6. linux必学

    memcache zookeeper activemq

  7. 系统根据用户cookies,为用户打上各种标签

    DSP营销学院_品友学院 | 品友推广官网 http://e.ipinyou.com/school_article40.html 智能算法+动态出价=最大发挥推广费用的价值 针对每一个曝光进行甄别和竞 ...

  8. <2014 03 18> Term BreakPoint

  9. JS eval()函数

    js  eval()函数   这个函数可以把一个字符串当作一个JavaScript表达式一样去执行它.   举个小例子:    //执行表达式  var the_unevaled_answer = & ...

  10. Charles抓包工具简单操作

    一.界面介绍 1.功能是clear,清理掉所有请求显示信息. 2.功能是搜索关键字,也可以使用ctrl+f实现,可以设置搜索的范围 3.功能是开始或暂停 4.显示所抓取的数据包 5.抓取数据包的请求及 ...