C++ syntax: Difference between revisions

Content deleted Content added
Line 550:
A simple example of using C++ modules is as follows:
 
{{mono|HelloMyClass.cppm}}
<syntaxhighlight lang="cpp">
export module myproject.HelloMyClass;
 
import std;
 
using String = std::string;
export namespace hello {
 
void printHello() {
export namespace hellomyproject {
std::println("Hello world!");
class MyClass {
private:
int x;
String name;
public:
MyClass(int x, const String& name):
x{x}, name{name} {}
 
int getX() const noexcept {
return x;
}
 
void setX(int newX) noexcept {
x = newX;
};
 
String getName() const noexcept {
return name;
}
 
void setName(const String& newName) noexcept {
name = newName;
}
}
Line 565 ⟶ 587:
{{mono|Main.cpp}}
<syntaxhighlight lang="cpp">
import myproject.Hellostd;
 
import myproject.MyClass;
 
using myproject::MyClass;
 
int main() {
MyClass me(10, "MyName");
hello::printHello();
me.setX(15);
 
std::println("Hello, world{}!", me);
}
</syntaxhighlight>