Header Ads

Create a C++ class called Person that has the following private data members

Create a C++ class called Person that has the following private data members:

name: a char array representing the first name of the student.

vuid: an integer representing the numerical part of the student vuID.

gender: a char array representing the gender of the person

The class should have the following public member functions:

1. A default constructor that initializes the name and gender to empty char array and VUID to 0.

2. A parameterized constructor that takes in a char array for the name, an int for theVUID, and a char array for the gender.

3. A copy constructor that creates a new Person object that is a copy of an existing Person object.

Next, create a main function that:

1. Creates an instance of the Person class using the default constructor.

2. Uses the parameterized constructor to create another instance of the Person class with the following values: name=student_first_name, vuid=student_vuid, gender='M'.

3. Creates a third instance of the Person class using the copy constructor and pass it the second instance of the Person class.

Solution:

Program:


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

class Person {
private:
    char name[50];
    int vuid;
    char gender[10];

public:
    // Default constructor
    Person() {
        strcpy(name, "");
        vuid = 0;
        strcpy(gender, "");
    }

    // Parameterized constructor
    Person(const char* personName, int personVuid, const char* personGender) {
        strcpy(name, personName);
        vuid = personVuid;
        strcpy(gender, personGender);
    }

    // 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() {
    // Create an instance of Person using the default constructor
    Person person1;

    // Create an instance of Person using the parameterized constructor
    Person person2("Student_first_name", mc19000000, "M");

    // Create an instance of Person using the copy constructor
    Person person3(person2);
    // Print the Default Constructor
    cout << "-------------------------------------------------------------\n" 
cout<<"Default constructor called\n"
    // Print the values of the created Person objects
    cout << "-------------------------------------------------------------\n" 
cout << " Parameterized constructor called with name: " << person2.getName() << "\n vuid: " << person2.getVuid() << "\n gender: " << person2.getGender() << endl;
cout << "----------------------------------------" <<endl;
cout<<"Copy constructor called\n"
cout << "----------------------------------------\n Person Details:\n Name: " << person1.getName() << "\n VUID: " << person1.getVuid() << "\n Gender: " << person1.getGender() << endl;
    cout << "----------------------------------------\n Person Details:\n Name: " << person2.getName() << "\n VUID: " << person2.getVuid() << "\n Gender: " << person2.getGender() << endl;
    cout << "----------------------------------------\n Person Details:\n Name: " << person3.getName() << "\n VUID: " << person3.getVuid() << "\n Gender: " << person3.getGender() << endl;

    return 0;

Output:





No comments

Powered by Blogger.