Home > Back-end >  Is it will cause a memory leak?
Is it will cause a memory leak?

Time:02-16

 
#include
#include
#include

Using STD: : cin;
Using STD: : cout;
Using STD: : endl;
Using STD: : string;

The class Grand
{
Private:
Int hold;
Public:
Grand (int h=0) : hold (h) {}
Virtual void the Speak () const {cout & lt; <"I am a grand class! \n"; }
Virtual int Value const () {return hold; }
};

The class Superb: public Grand
{
Public:
Superb (int h=0) : Grand (h) {}
Void the Speak () const {cout & lt; <"I am a superb class!!!!! \n"; }
Virtual void Say () const {cout & lt; <"I hold the superb value of" & lt; };

The class Magnificent: public Superb
{
Private:
char ch;
Public:
Magnificent (int h=0, char c='A') : Superb (h), ch (c) {}
Void the Speak () const {cout & lt; <"I am a magnificent class!!!!!! \n"; }
Void Say () const {cout & lt; <"I hold the character" & lt; };

Grand * GetOne ();

Int main ()
{
Srand (time (0));
Grand * pg;
Superb * ps;
For (int I=0; I & lt; 5; I++)
{
Pg=GetOne ();
Pg - & gt; Speak ();
If (ps=dynamic_cast & lt; Superb * & gt; Ps - (pg)) & gt; Say ();
}
cout <"Bye. \ n";
return 0;
}

Grand * GetOne ()
{
Grand * p;
The switch (rand () % 3)
{
Case 0:
P=new Grand (rand () % 100);
break;
Case 1:
P=new Superb (rand () % 100);
break;
Case 2:
P=new Magnificent (rand () % 100, 'A' + rand () % 26);
break;
}
Return the p;
}

GetOne new memory function, need to release?

CodePudding user response:

All things new must delete back

CodePudding user response:

new memory to release
  • Related