diff --git a/FirstMethod.cpp b/FirstMethod.cpp new file mode 100644 index 0000000..ed5ec20 --- /dev/null +++ b/FirstMethod.cpp @@ -0,0 +1,21 @@ +//Using Public Member Variables +#include +#include +using namespace std; + +class Person { +public: + string name; + int age; +}; + +int main() { + Person person; + person.name = "John Doe"; + person.age = 30; + + cout << "Name: " << person.name << endl; + cout << "Age: " << person.age << endl; + + return 0; +} diff --git a/SecondMethod.cpp b/SecondMethod.cpp new file mode 100644 index 0000000..b2d6d45 --- /dev/null +++ b/SecondMethod.cpp @@ -0,0 +1,22 @@ +//Using a Constructor to Initialize Values +#include +#include +using namespace std; + +class Person { +public: + string name; + int age; + + // Constructor + Person(string n, int a) : name(n), age(a) {} +}; + +int main() { + Person person("John Doe", 30); + + cout << "Name: " << person.name << endl; + cout << "Age: " << person.age << endl; + + return 0; +} diff --git a/ThirdMethod.cpp b/ThirdMethod.cpp new file mode 100644 index 0000000..1a5367f --- /dev/null +++ b/ThirdMethod.cpp @@ -0,0 +1,18 @@ +//Using Struct +#include +#include +using namespace std; + +struct Person { + string name; + int age; +}; + +int main() { + Person person = {"John Doe", 30}; + + cout << "Name: " << person.name << endl; + cout << "Age: " << person.age << endl; + + return 0; +}