CS 304p-Assignment 1
CS 304p-Assignment 1 |
#include <iostream>
#include <cstring>
using namespace std;
class Person {
private:
char name[100];
int vuid;
char gender[10];
public:
// Default constructor
Person() {
strcpy(name, "");
vuid = 0;
strcpy(gender, "");
}
// Parameterized constructor
Person(const char* newName, int newVuid, const char* newGender) {
strcpy(name, newName);
vuid = newVuid;
strcpy(gender, newGender);
}
// Copy constructor
Person(const Person& other) {
strcpy(name, other.name);
vuid = other.vuid;
strcpy(gender, other.gender);
}
// Getter methods
const char* getName() const {
return name;
}
int getVuid() const {
return vuid;
}
const char* getGender() const {
return gender;
}
};
int main() {
// Creates an instance of the Person class using the default constructor
Person p1;
// Uses the parameterized constructor to create another instance of the Person class
// with the provided values
Person p2("John", 123456789, "M");
// Creates a third instance of the Person class using the copy constructor
// and pass it the second instance of the Person class
Person p3 = p2;
// Accessing and printing the information of each person
cout << "Person 1: " << endl;
cout << "Name: " << p1.getName() << endl;
cout << "VUID: " << p1.getVuid() << endl;
cout << "Gender: " << p1.getGender() << endl;
cout << endl;
cout << "Person 2: " << endl;
cout << "Name: " << p2.getName() << endl;
cout << "VUID: " << p2.getVuid() << endl;
cout << "Gender: " << p2.getGender() << endl;
cout << endl;
cout << "Person 3: " << endl;
cout << "Name: " << p3.getName() << endl;
cout << "VUID: " << p3.getVuid() << endl;
cout << "Gender: " << p3.getGender() << endl;
cout << endl;
return 0;
}
0 Comments