diff --git a/Assignment-4/FirstMethod.cpp b/Assignment-4/FirstMethod.cpp new file mode 100644 index 0000000..aa6179f --- /dev/null +++ b/Assignment-4/FirstMethod.cpp @@ -0,0 +1,22 @@ +//Using constructor initialization + +#include +#include +using namespace std; + +class Person { + private: + string name; + int age; + + public: + Person(string name, int age) { + this->name = name; + this->age = age; + } + + void displayInfo() { + cout << "Name = " << name << endl; + cout << "Age = " << age << endl; + } +}; diff --git a/Assignment-4/SecondMethod.cpp b/Assignment-4/SecondMethod.cpp new file mode 100644 index 0000000..2d465d0 --- /dev/null +++ b/Assignment-4/SecondMethod.cpp @@ -0,0 +1,10 @@ +//Using Public data members instead of private +#include +#include +using namespace std; + +class Person { + public: + string name; + int age; +}; diff --git a/Assignment-4/ThirdMethod.cpp b/Assignment-4/ThirdMethod.cpp new file mode 100644 index 0000000..a0c6de2 --- /dev/null +++ b/Assignment-4/ThirdMethod.cpp @@ -0,0 +1,31 @@ +//Using appropriate functions instead of getters and setters) + +#include +#include + +using namespace std; + +class Person { + private: + string name; + int age; + + public: + void displayPersonInfo() { + cout << "Name = " << name << endl; + cout << "Age = " << age << endl; + } + + void updatePersonInfo(const string& name, int age) { + this->name = name; + this->age = age; + } +}; + +int main() { + Person person; + person.updatePersonInfo("Nitin", 23); + person.displayPersonInfo(); + + return 0; +}