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
21 changes: 21 additions & 0 deletions FirstMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//Using Public Member Variables
#include <iostream>
#include <string>
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;
}
22 changes: 22 additions & 0 deletions SecondMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//Using a Constructor to Initialize Values
#include <iostream>
#include <string>
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;
}
18 changes: 18 additions & 0 deletions ThirdMethod.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//Using Struct
#include <iostream>
#include <string>
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;
}