Code:
In class header file:
class Coconuts
{
public:
Coconuts constructor();
};
In class .cpp file:
#include "Coconuts.h"
#include <iostream>
#include <string>
using namespace std;
Coconuts::constructor()
{
cout << "\nYay coconuts are initialized";
};
In main():
Coconuts Object1;
My program runs without any erros whatsoever, but the constructor isn’t initialized and the message
is not displayed. Suggestions, anyone?
Constructors are not functions named
constructor. The “name” of a constructor is the name of the class itself. Note that constructors are not normal functions and cannot be directly referenced by name, which is why I put “name” in quotation marks.Your code should be as follows: