Pages

CS304P ASSIGNMENT NO. 1 SPRING 2023 || 100% RIGHT SOLUTION || OBJECT ORIENTED PROGRAMMING (PRACTICAL) || BY VuTech

CS304P ASSIGNMENT NO. 1 SPRING 2023 || 100% RIGHT SOLUTION || OBJECT ORIENTED PROGRAMMING (PRACTICAL) || BY VuTech

CS304P ASSIGNMENT NO. 1 SPRING 2023 || 100% RIGHT SOLUTION || OBJECT ORIENTED PROGRAMMING (PRACTICAL) || BY VuTech

Visit Website For More Solutions
www.vutechofficial.blogspot.com

KINDLY, DON’T COPY PASTE

QUESTION:

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

#include <iostream>

#include <cstring>

using namespace std;

class Person {

private:

    char name[50];

    int vuid;

    char gender[10];

public:

    Person() {

        name[0] = '\0';

        vuid = 0;

        gender[0] = '\0';

    }

    Person(const char* n, int id, const char* g) {

        strcpy(name, n);

        vuid = id;

        strcpy(gender, g);

    }

    Person(const Person& p) {

        strcpy(name, p.name);

        vuid = p.vuid;

        strcpy(gender, p.gender);

    }

    void display() {

        cout << "Name= " << name << endl;

        cout << "VUID= " << vuid << endl;

        cout << "Gender= " << gender << endl;

    }

};

int main() {

    // Create a Person instance using default constructor

    Person p1;


    // Create another Person instance using parameterized constructor

    Person p2("John", 1234567, "M");


    // Create a third Person instance using copy constructor and pass it p2

    Person p3 = p2;


    // Display the details of all three persons

    cout << "Person details:" << endl;

    p1.display();


    cout << endl << "Person details:" << endl;

    p2.display();


    cout << endl << "Person details:" << endl;

    p3.display();


    return 0;

}




KINDLY, DON’T COPY PASTE
SUBSCRIBE, SHARE, LIKE AND COMMENTS FOR MORE UPDATES
SEND WHATSAPP OR E-MAIL FOR ANY QUERY
0325-6644800
kamranhameedvu@gmail.com
Visit Website For More Solutions
www.vutechofficial.blogspot.com