当前位置: 代码迷 >> 综合 >> C++中堆栈对象实例化笔记
  详细解决方案

C++中堆栈对象实例化笔记

热度:65   发布时间:2023-10-24 04:26:10.0

1:假设有一个学生类

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;
}




  相关解决方案