Syntax

C++ Define a Class Syntax

In C++, define a class: defines a class with a constructor — class Dog { public: explicit Dog(std::string n) : name(std::move(n)) {} private: std::string name; };

Quick Answer

Syntax
class Dog { public: explicit Dog(std::string n) : name(std::move(n)) {} private: std::string name; };
C++Syntax

Define a Class

Code example
class Dog { public: explicit Dog(std::string n) : name(std::move(n)) {} private: std::string name; };

Defines a class with a constructor.

Learn More

How to define a class in C++

  1. Open your C++ file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C++ Define a Class Syntax

Use class Dog { public: explicit Dog(std::string n) : name(std::move(n)) {} private: std::string name; };.

Related Syntax References

Frequently asked questions

What is the C++ syntax for define a class?

Use this pattern: class Dog { public: explicit Dog(std::string n) : name(std::move(n)) {} private: std::string name; };

Can I copy this C++ code example?

Yes. Use Copy code and adapt the example to your program.

Is this C++ syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify