Laurand Osmeni

Let's build a faster web.


How do I use constructors in C++.

So what is a constructor? a constructor is a function which is caught as sooner is created an object. Constructors don't have a return type, all I have to do is type the constructor name which is always the class name, either type the name or copy and paste the class name. Program automatically knows it is a constructor.

Laurand(string z){ .. constructor is body.. }

And here it is how it calls automatically as soon I create the object.

Laurand landiobj("Accessing constructor automatically. \n");

Watch live in action creating the program, or scroll below to copy the entire code.

passing arrays to functions
How do I use constructors in C++.
#include <iostream> using namespace std; class Laurand{ public: Laurand(string z){ setName(z); } void setName(string x){ name = x; } string getName(){ return name; } private: string name; }; int main() { Laurand landiobj("Accessing constructor automatically. \n"); cout << landiobj.getName(); return 0; }

Feel free to use the code.

Last update .

Navigate between pages.