【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[];
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】动态分配内存的更多相关文章
- c/c++动态分配内存和malloc的使用
		
c/c++动态分配内存 为什么需要动态分配内存 ---很好的解决的了传统数组的4个缺陷 动态内存分配举例 ---动态数组的构造 使用动态数组的优点: 1. 动态数组长度不需要事先给定: 2. ...
 - 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域
		
[源码下载] 不可或缺 Windows Native (9) - C 语言: 动态分配内存,链表,位域 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 动态分配内存 链 ...
 - C++动态分配内存
		
动态分配(Dynamic Memory)内存是指在程序运行时(runtime)根据用户输入的需要来分配相应的内存空间. 1.内存分配操作符new 和 new[] Example: (1)给单个元素动态 ...
 - 标C编程笔记day06 动态分配内存、函数指针、可变长度參数
		
动态分配内存:头文件 stdlib.h malloc:分配内存 calloc:分配内存,并清零 realloc:调整已分配的内存块大小 演示样例: in ...
 - C&C++动态分配内存(手动分配内存)三种方式
		
1. malloc函数 函数原型:void *malloc(unsigned int size)函数的作用是:在内训的动态存储区开辟一个size个字节的连续空间,返回所分配区域的首字节地址. 可以看到 ...
 - mfc 动态分配内存
		
 动态内存分配new  为数组动态分配内存  为多维数组分配内存  释放内存delete malloc free  动态内存分配new int * pi; pi= new int ;  为 ...
 - 指针和动态分配内存 (不定长度数组)------新标准c++程序设计
		
背景: 数组的长度是定义好的,在整个程序中固定不变.c++不允许定义元素个数不确定的数组.例如: int n; int a[n]; //这种定义是不允许的 但是在实际编程中,往往会出现要处理的数据数量 ...
 - 数据结构复习之C语言malloc()动态分配内存概述
		
#include <stdio.h> #include <malloc.h> int main(void) { ] = {, , , , }; // 计算数组元素个数 ]); ...
 - C++——动态分配内存问题
		
class Obj { public: float score; ]; }; class Result { public: int id; ]; Obj obj[]; }; 合法,可动态分配内存给Re ...
 
随机推荐
- 【Raspberry Pi】crontab 定时任务
			
在linux上做定时任务一般用crond 两种方法上文已列,但昨天写的crond命令却一直都没有运行,上网查,有说是环境变量的,也有说是时间问题的,都改过,但还没有效. 今天再次认真读了一遍cront ...
 - 【转】MFC OnIdle的详细说明
			
转载出处:http://blog.csdn.net/tsing_best/article/details/25055707 CWinApp::OnIdlevirtual BOOL OnIdle( LO ...
 - MYSQL5.7:几个简单的show语句演示
 - plsql数组、表和对象
			
--数组DECLARE TYPE test_plsql_varray IS VARRAY(100) OF VARCHAR2(20); temp_varray1 test_PLSQL_VARRAY := ...
 - 再论IBatisNet + Castle进行项目的开发
			
随着项目的进展,Castle和IBatisNet给我的惊喜更多.Com+很重,不需要分布式的中小项目慎用,NHibernate虽好,NHibernate的2005-9-20发布了最新版本1.0-rc1 ...
 - 全局最小割模板(定S,不定T,找最小割)
			
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
 - 【MarkDown】使用Html样式和折叠语法
			
MarkDown很方便,但基本语法有些不足:比如无法使用折叠语法,无法让文字有不同的颜色. 这些功能可以实现,不过需要使用Html语法进行扩展.这篇文章主要是整理一下这些技巧,方便更好的使用. 一.折 ...
 - CoordinatorLayout Behaviors使用说明[翻译]
			
翻译与:Intercepting everything with CoordinatorLayout Behaviors 使用过Android Design Support Library的小伙伴应该 ...
 - c# public private protected internal protected internal
			
一个 访问修饰符 定义了一个类成员的范围和可见性.C# 支持的访问修饰符如下所示: public:所有对象都可以访问: private:对象本身在对象内部可以访问: protected:只有该类对象及 ...
 - Java 面向对象之 static 关键字
			
static 特点 static 是一个修饰符, 用于修饰成员 static 修饰的成员被所有的对象所共享 static 优先于对象存在, 因为 static 的成员随着类的加载就已经存在了 stat ...