pointer objects

I was going through the following code in Bjarne Stroustrups book, when i encountered the following code :
1
2
3
4
5
6
Shape* fct() {

Text tt{Point{200,200},"Annemarie"}
Shape* p =  new Text{Point{100,100}, "Nicholas"};
return p;
}


Can somebody elaborate on line 4. I mean when what kind of code would it be ,where it is useful to use

 
Shape* p =new Text{….};

instead of

 
Shape* p =  new Shape; //  


and VICE -VERSA

Thanks!
Last edited on
Was there word polymorphism in that chapter?
Can somebody elaborate on line 4. I mean when what kind of code would it be ,where it is useful to use

Shape* p =new Text{….};


When Text is a class derived from the abstract class Shape, and vice versa for: Text* ptr=new Shape

Aceix.
@Aceix and how would and when would it come to use, to allocate a derived class's
constructor to that of a base class..
You don't. Not "constructor in constructor".

You store the address of an object in a pointer.

Derived inverits its base and therefore derived IS-A base and can be used where base is used.

Websearch polymorphism.
> where it is useful to use
> Shape* p =new Text{….};
> instead of
> Shape* p = new Shape;
¿when is it useful to use Shape* p = new Shape;?
http://www.cplusplus.com/forum/general/138037/#msg731921
Topic archived. No new replies allowed.