从堆中实例化
从栈中实例化
1 //类中有构造函数的情况下
class temp
{
public:
//构造函数
temp(int Num, string abc)
{
theNum = Num;
theabc = abc;
}
void printf()
{
cout << theNum << theabc << endl;
}
private:
int theNum;
string theabc;
};
int main()
{
//从栈中实例化
temp ywb(10,"yangwenbin");
ywb.printf();
system("pause");
}
int main()
{
//从堆中实例化
temp *p = new temp(10,"yangwenbin");
p->printf();
getchar();
}
类中无自定义的构造函数时,编译器会自动生成
class temp
{
public:
int theNum;
string theabc;
char name[10];
};
int main()
{
//从堆中实例化
temp *p = new temp[10];
temp *q = new temp();
q->theNum = 10;
q->theabc = "ywb";
delete []p;
delete q;
return 0;
}
int main()
{
//从栈中实例化
temp ywb;
temp ywb1[10];
ywb.theabc = "yangwenbin";
ywb.theNum = 10;
ywb1->theabc = "yanwenbin1";
}