C++: Difference between revisions

fix inconsistent spacing
No edit summary
(fix inconsistent spacing)
 
 
The current standardized C++ version is C++20, which added the "consteval" keyword and the Concepts TS, among lots of other improvements.
 
The C++ language has played a big role in CollabVM's history, most notably in CollabVM version 1.1 and onward, which is the rewrite of the CollabVM server by Cosmic Sans, along with CollabVM 2.0 also by Cosmic. It is also used in CollabVM 3.0 by modeco80, which is similarly known as a prolific and incredibly talented C++ programmer.
 
== Examples ==
 
=== Hello World ===
 
This program prints out "Hello, world!" to the console.
 
 
int main() { // Main function: must return a variable of int type. This is the program's exit code.
std::cout << "Hello, world!\n";
return 0; // Quit with the exit code of 0
}
</syntaxhighlight>
 
=== Fizz Buzz ===
 
This is a more complex example: this program prints out "Fizz" for each multiple of 3, "Buzz" for each multiple of 5, and "Fizz Buzz" for each multiple of both. This program demonstrates functions besides <code>main()</code>, the <code>if</code> statement, the ternary operator (<code>condition ? ifTrue : ifFalse</code>), and loops.
 
 
enum class FizzBuzzType {
None,
FizzBuzz,
Fizz,
Buzz
};
 
FizzBuzzType GetFizzBuzzType(int number) {
if(number % 3 == 0 && number % 5 == 0) {
return FizzBuzzType::FizzBuzz;
}
 
if(number % 3 == 0) {
return FizzBuzzType::Fizz;
}
 
if(number % 5 == 0) {
return FizzBuzzType::Buzz;
}
 
return FizzBuzzType::None;
}
 
int main() {
// i starts as 1, the loop continues until it is above 100, and i increments each loop.
for(int i = 1; i <= 100; i++) {
// This refactored switch statement is better than before.
// It also calculates the value only once.
switch(GetFizzBuzzType(i)) {
case FizzBuzzType::FizzBuzz:
i < 100 ? std::cout << "Fizz Buzz, " : std::cout << "Fizz Buzz" << '\n';
break;
 
case FizzBuzzType::Fizz:
i < 100 ? std::cout << "Fizz, " : std::cout << "Fizz" << std::endl;
break;
 
case FizzBuzzType::Buzz:
i < 100 ? std::cout << "Buzz, " : std::cout << "Buzz" << std::endl;
break;
 
case FizzBuzzType::None:
i < 100 ? std::cout << i << ", " : std::cout << i << '\n';
break;
}
}
}
return 0;
}
</syntaxhighlight>
 
== Virtual Functions ==
 
Virtual functions are functions part of a class which can be implemented in another inheriting class, allowing the actual implementation to be located somewhere else or even hidden from code.
 
The example code here shows how a completely abstract class can be implemented inside of a different translation unit (source file), so that the main program only needs the interface to work.
the interface to work.
 
Interface.h:
// and also sets this class up for proper RTTI data generation.
virtual ~MyInterface() = default;
 
// Do something.
virtual void Run() = 0;
 
namespace {
// Implements MyInterface.
class MyInterfaceImpl : public MyInterface {
public:
 
== Generic code ==
 
C++ supports "templates" which are the front and center way of writing generic code. Alongside this, is now the Concepts TS, allowing us to
with ease constrain what types a template can operate on.
}
</syntaxhighlight>
 
[[Category:Programming Languages]]