create instance of new class c++ code example
Example 1: declaring instance of class c++
#include <iostream>
using namespace std;
class Foo
{
public:
Foo ( )
{
cout << "constructor Foo\n";
}
};
class Bar
{
public:
Bar ( Foo )
{
cout << "constructor Bar\n";
}
};
int main()
{
Foo* foo1 = new Foo ();
Foo* foo2 = new Foo;
Foo foo3;
Foo foo4 = Foo::Foo();
Bar* bar1 = new Bar ( *new Foo() );
Bar* bar2 = new Bar ( *new Foo );
Bar* bar3 = new Bar ( Foo foo5 );
Bar* bar3 = new Bar ( Foo::Foo() );
return 1;
}
Example 2: create class instance c++
MyClass* MyObject = new MyClass();