c++ declare instance of class 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: classes c++
class Rectangle {
int width, height;
public:
void set_values (int,int);
int area (void);
} rect;
Example 3: classes c++
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} object_names;
Example 4: create class instance c++
MyClass* MyObject = new MyClass();