We are doing create an object of class then called constructor automatically
×
Create an Object of Class
#include <iostream>
using namespace std;
class MyClass { // The class
public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
We are doing access attributes of class and then modify it.
×
Access and Modify Attribute of Class
#include <iostream>
using namespace std;
class MyClass { // The class
public: // Public access specifier
int x; // Public attribute (int variable)
};
int main() {
MyClass myObj; // Create an object of MyClass
// Access attributes and set values
myObj.x = 15;
// Print values
cout << myObj.x;
return 0;
}