C++中堆栈对象实例化笔记
2017-07-06 14:32
495 查看
1:假设有一个学生类
class Student
{
public:
int[20] numj;
char[20] name;
void getScore();
};
class Student
{
public:
int[20] numj;
char[20] name;
void getScore();
};
class Student { public: int[20] numj; char[20] name; void getScore(); }; int main() { Student stu;//建立一个stu对象,此种方式所建立的实例化对象是在栈中进行实例化的对象,在程序使用完毕之后系统将会自动进行垃圾的回收操作 Student *student=new Studnet();//通过关键字new来获取的实例化对象是在堆当中开辟一个空间进行实例化对象的存放,需要用户通过关键字delete来进行内存空间的释放操作 delete studnet; }
#include<iostream> #include<stdlib.h> using namespace std; class Coordinate//建立一个坐标类(coordinate) { public: int x; int y; void printX() { cout<<"x="<<x<<endl; } void printY() { cout<<"y="<<y<<endl; } }; int main() { Coordinate coor;//完成在栈内存当中实例化一个坐标类的实例化对象 coor.x=10; coor.y=20; coor.printX(); coor.printY(); Coordinate *p=new Coordinate();//完成在堆内存空间当中实例化一个坐标类的实例化对象 if(p==NULL) { cout<<"申请的内存失败"<<endl; return 0; } p->x=100; p->y=200; p->printX(); p->printY(); delete p; p=NULL; }
相关文章推荐
- More Effective C++ 阅读笔记(十二)--怎样限制实例化对象的数量
- C++学习笔记(六)-- 类和对象 构造函数和析构函数 const成员函数 this指针 对象数组 堆栈管理变量
- C++研究笔记(6)存储模型和对象模型
- C++ FAQ阅读笔记[1] -- 类与对象
- c++入门学习笔记--类和对象
- 实例解析C++/CLI之基于堆栈的对象与跟踪引用
- 《面向对象基础:C++实现》学习笔记之七
- C++学习笔记(6)——多重继承类对象的构造函数参数的传递方法
- C++对象模型笔记
- 笔记--c++临时对象与const
- C++ 中的成员对象(学习笔记)
- 补补课,C++面对对象的比较好的笔记
- 《面向对象基础:C++实现》学习笔记之三
- 《面向对象基础:C++实现》学习笔记之一
- 深入C++对象模型学习笔记 第3章 Data 语意学
- C++学习笔记(1)——基类、派生类的对象空间
- Inside The C++ Object Model 学习笔记 -- 关于对象
- C++对象模型笔记:概述
- 《面向对象基础:C++实现》学习笔记之五
- c++对象模型笔记