From baed20960877053e95c81069ea1534b99fea868a Mon Sep 17 00:00:00 2001 From: nitin-tayal Date: Tue, 19 Mar 2024 16:27:08 +0530 Subject: [PATCH] Assignment-4 added --- Assignment-4/FirstMethod.cpp | 22 ++++++++++++++++++++++ Assignment-4/SecondMethod.cpp | 10 ++++++++++ Assignment-4/ThirdMethod.cpp | 31 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 Assignment-4/FirstMethod.cpp create mode 100644 Assignment-4/SecondMethod.cpp create mode 100644 Assignment-4/ThirdMethod.cpp 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; +}