C++ class Keyword
Example
Create a class and then create an object of the class to access its attributes:
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.myNum = 15;
myObj.myString = "Some text";
// Print attribute values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}
Definition and Usage
The class
keyword is used to create a class.
Related Pages
Read more about classes in our C++ Classes/Objects Tutorial.