What is the difference between using parenthesis and not using the parenthesis at the end of the object declaration using C++

  • Thread starter Thread starter David student
  • Start date Start date
D

David student

Guest
Hello All,

I am a computer science student. When working on one of the C++ programs, I found the below statement.

Base *pb = new Derived;

I understood the above statement and it is:

pb is a base class pointer that points to Derived class.

is my understanding correct?

And also here in place of

Base *pb = new Derived;

If i use the below statement

Base *pb = new Derived();

both are working. Is there any difference between these two?

Can i Use any of the above statements?

Below is my complete program:

class Base{
public:
virtual void func1()
{
cout<<"Base class virtual function"<<endl;
}
};

class Derived: public Base{
public:
void func1()
{
cout<<"Derived class virtual function"<<endl;
}
};

int main()
{

Base *pb = new Derived;

//Base *pb = new Derived();

pb->func1();

return 0;
}

Could anyone please clarify my doubts?

Thanks & Regards,

David Sudent

Continue reading...
 
Back
Top