CS201 MID TERM SOLVED MCQs || PAST PAPERS || GROUP-1 || INTRODUCTION TO PROGRAMMING || VuTech Visit Website For More Solutions www.vutechofficial.blogspot.com …
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
We provide Virtual University of Pakistan Study Materials such as Solution of Assignments, GDBs, Mid Term Solved Papers, Final Term Solved Papers, Mid Term Solved MCQs, and Final Term Solved MCQs. We also provide regular Semester Quizzes, Updated Handouts, and Short Questions and Answers. We help you with your research and many other educational-related topics, as far as we know. Furthermore, Share your problem with us and Please feel free to ask any related questions.