HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to C++VariablesData TypesOperatorsStringsVectors (vector)Conditions (if / switch)Loops (for / while)FunctionsClasses (OOP)
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Array & Vector
🔁 Condition & Loop
🧩 Function & Class
Functionclass
C++›REFERENCE›Function & Class
🧩

Function & Class

Functions and classes (OOP).

CommandDescription
FunctionDefines a function.
classDefines your own type (class).

Function

Try it yourself →

Defines a function.

Syntax

returnType name(type p) { return ...; }

Description

Return type, name, parameters. Define before main or declare a prototype.

Example

#include <iostream>
using namespace std;
int square(int x) {
    return x * x;
}
int main() {
    cout << square(5) << endl;
    return 0;
}

Expected output

25

When to use?

Preventing code repetition.

class

Try it yourself →

Defines your own type (class).

Syntax

class Name { public: ... };

Description

public members are accessed externally. Constructor (same name as the class) runs upon object creation. Class definition ends with ;.

Example

#include <iostream>
#include <string>
using namespace std;
class Cat {
public:
    string name;
    Cat(string a) { name = a; }
    string meow() { return name + ": meow"; }
};
int main() {
    Cat k("Tabby");
    cout << k.meow() << endl;
    return 0;
}

Expected output

Tabby: meow

When to use?

Object-oriented design.

Common mistakes

  • ⚠️ Don't forget to put a ; after the class definition: class X { ... };
HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code