Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Assignment-4/FirstMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//Using constructor initialization

#include <iostream>
#include <string>
using namespace std;

class Person {
private:
string name;
int age;

public:
Person(string name, int age) {
this->name = name;
this->age = age;
}

void displayInfo() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont use short forms like info

cout << "Name = " << name << endl;
cout << "Age = " << age << endl;
}
};
10 changes: 10 additions & 0 deletions Assignment-4/SecondMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//Using Public data members instead of private
#include <iostream>
#include <string>
using namespace std;

class Person {
public:
string name;
int age;
};
31 changes: 31 additions & 0 deletions Assignment-4/ThirdMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//Using appropriate functions instead of getters and setters)

#include <iostream>
#include <string>

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;
}