1class MyClass { // The class
2 public: // Access specifier
3 MyClass() { // Constructor
4 cout << "Hello World!";
5 }
6};
1class MyClass {
2 public:
3 MyClass(); // This is the constructor
4 private:
5 int someInt;
6};
7
8MyClass::MyClass(void) {
9 someInt = 1;
10}
1struct S {
2 int n;
3 S(int); // constructor declaration
4 S() : n(7) {} // constructor definition.
5 // ": n(7)" is the initializer list
6 // ": n(7) {}" is the function body
7};
8S::S(int x) : n{x} {} // constructor definition. ": n{x}" is the initializer list
9int main()
10{
11 S s; // calls S::S()
12 S s2(10); // calls S::S(int)
13}
1Line::Line( double len): length(len) {
2 cout << "Object is being created, length = " << len << endl;
3}
4
1class Book {public: Book(const char*); ~Book(); void display();private: char* name;};