From 5cfb0b85dd95266c618568f467bee09e06d41e74 Mon Sep 17 00:00:00 2001 From: PradeepITT <148972812+PradeepITT@users.noreply.github.com> Date: Tue, 19 Mar 2024 17:11:44 +0530 Subject: [PATCH] Different Approach to remove Getter and Setter --- FirstMethod.cpp | 21 +++++++++++++++++++++ SecondMethod.cpp | 22 ++++++++++++++++++++++ ThirdMethod.cpp | 18 ++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 FirstMethod.cpp create mode 100644 SecondMethod.cpp create mode 100644 ThirdMethod.cpp 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; +}