您的位置:首页 > 编程语言 > C语言/C++

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();
};
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: