Laurand Osmeni

Let's build a faster web.


How do I use Classes and Objetcs in C++.

Declaring a class name as Laurand classes always are outside of main, and everything I create suppose to be inside the body of the class which is are the curly brackets {};.

class Laurand { .. class body goes here..};

Access specifier, if I want my function to be in public outside of the class then I must use specifier public, otherwise private: so my functions will not be accessed.


Now creating a global function to be accessed outside the class.

void LandiAdds()

And printing on the screen something like Laurand is public specifier.

cout << "Laurand is public" << endl;

I have the class ready and the function, but now I need to create and Object, because this is how we access outside the class.

In order me to create an Object I must include the class name which is Laurand and then giving the Object name as LandiObj.

Laurand LandiObj;

Now i'm using LandiObj with dot operator to access a function, example LandiObj.LandiAdds(); so now function is being accessed inside the main function.

So this is how I use classes and objects.

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

C++ Array Calculation
How do I use Classes and Objects in C++.
#include <iostream> using namespace std; class Laurand{ public: void LandiAdds(){ cout << "Laurand is public" << endl; } }; int main() { Laurand LandiObj; LandiObj.LandiAdds(); return 0; }

Feel free to use the code.

Last update .

Navigate between pages.