CS201 MID TERM SOLVED MCQs || PAST PAPERS || GROUP-1 || INTRODUCTION TO PROGRAMMING || VuTech Visit Website For More Solutions www.vutechofficial.blogspot.com …
CS304 ASSIGNMENT NO. 1 SPRING 2023 || 100% RIGHT SOLUTION || OBJECT ORIENTED PROGRAMMING || BY VuTech
Visit Website For More Solutions www.vutechofficial.blogspot.com
KINDLY, DON’T COPY PASTE
QUESTION
Suppose a university wants to keep track of its students' information, such as their name, age, and student ID, for various purposes such as enrollment, attendance, and grading. The university could create a Student class that includes these member variables to represent each student, and use the class to store and manipulate the information of each student.
Considering the given scenario create a C++ program with Student class that includes three member variables: name, age, and studentId, and provides a default constructor, a parameterized constructor, a copy constructor that performs a shallow copy, and an assignment operator that performs a deep copy. The main function creates three Student objects using different constructors, and demonstrates the differences between shallow and deep copying by modifying one object's name and printing the values of all three objects. The Student class could be used in a real-life scenario such as a university keeping track of students' information for enrollment, attendance, and grading purposes.
Class Name:
Student
Data
Members
- name: string
- age: int
- studentId: int
Methods
- Default constructor
- Parameterized constructor
- Copy constructor (shallow copy)
- Assignment operator (deep copy)
- Getter method for name
- Getter method for age
- Getter method for studentId
- Setter method for name
- Setter method for age
- Setter method for studentId
- Print method to display student info
Main
- Creates s1 using parameterized ctor
- Creates s2 using copy constructor
- Creates s3 using assignment operator
- Modifies name of s1
- Prints all three Student objects
SOLUTION
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int age;
std::string studentId;
public:
// Default constructor
Student() {
name = "";
age = 0;
studentId = "";
}
// Parameterized constructor
Student(const std::string& name, int age, const std::string& studentId) {
this->name = name;
this->age = age;
this->studentId = studentId;
}
// Copy constructor (shallow copy)
Student(const Student& other) {
name = other.name;
age = other.age;
studentId = other.studentId;
}
// Assignment operator (deep copy)
Student& operator=(const Student& other) {
if (this != &other) {
name = other.name;
age = other.age;
studentId = other.studentId;
}
return *this;
}
// Getter methods
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
std::string getStudentId() const {
return studentId;
}
// Setter methods
void setName(const std::string& name) {
this->name = name;
}
void setAge(int age) {
this->age = age;
}
void setStudentId(const std::string& studentId) {
this->studentId = studentId;
}
// Print method
void print() const {
std::cout << name << ", Age: " << age << ", Student ID: " << studentId << std::endl;
}
};
int main() {
// Create s1 using parameterized constructor
Student s1("Maryam", 18, "BC123456");
// Create s2 using copy constructor
Student s2 = s1;
// Modify name of s1
s1.setName("Ali");
// Create s3 using assignment operator
Student s3;
s3 = s1;
// Print all three Student objects
std::cout << "s1: ";
s1.print();
std::cout << "s3: ";
s3.print();
std::cout << "s2: ";
s2.print();
return 0;
}
SAMPLE OUTPUT:
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.