Write programs and run on computer Create a class named Student with three priva
ID: 3546693 • Letter: W
Question
- Write programs and run on computer
Create a class named Student with three private member variables: name, gpa, and age. Based on the main() given below, you need to implement the following functions for class Student:
- Constructors: default constructor, parameterized constructor, and copy constructor. Note that the copy constructor clones a Student object with the same info but 10 years older. Default values for a Student object are John Doe/3.0/20
- Destructor: just print out a message that says the destructor is being called.
- Assignment operator = overloading function
- A print function that outputs all available information about a student.
- In each constructor and the destructor, add an output message indicating the constructor or destructor is being called, like "copy constructor is being called by Student John Doe"
- Mark which statement outputs which message
- Constructors: default constructor, parameterized constructor, and copy constructor. Note that the copy constructor clones a Student object with the same info but 10 years older. Default values for a Student object are John Doe/3.0/20
s1 = s2;
s1.print(); s2.print(); s3.print(); s4.print(); s5.print(); s6->print(); delete s6; return 0; }
2. [Reference Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. How does this relate to passing-by-reference in function calls? Give an example.
int a = 8; int & p = a; cout << a << endl; p = 10; cout << a << endl;
using namespace std;
//Sum the numbers in an array int sum(int * nums, int size) { int s = 0; for (int i = 0; i < size; i++) { s = s + nums[i]; } return s; }
3. [Pointer Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. Is passing-by-reference or passing-by-value used to pass the array to function sum?
int main() { int MAX = 3; int a[5] = {12, 2, 5, 3, 8}; int * b = new int[MAX]; int * p; p = a; cout << *a << endl; cout << a[0] << endl; cout << *(a + 3) << endl; cout << *(p + 3) << endl; for (int i=0; i < MAX; i++) b[i]=a[i]; cout << sum(a, 5) << endl; cout << sum(p, 4) << endl; cout << sum(b, MAX) << endl;
return 0; } int main() { Student s1; Student s2("Jesus"); Student s3("Marissa Love", 3.75, 21); Student s4(s3); Student s5 = s2; Student * s6 = new Student("Jose");
s1 = s2;
s1.print(); s2.print(); s3.print(); s4.print(); s5.print(); s6->print(); delete s6; return 0; }
2. [Reference Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. How does this relate to passing-by-reference in function calls? Give an example.
int a = 8; int & p = a; cout << a << endl; p = 10; cout << a << endl;
using namespace std;
//Sum the numbers in an array int sum(int * nums, int size) { int s = 0; for (int i = 0; i < size; i++) { s = s + nums[i]; } return s; }
3. [Pointer Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. Is passing-by-reference or passing-by-value used to pass the array to function sum?
int main() { int MAX = 3; int a[5] = {12, 2, 5, 3, 8}; int * b = new int[MAX]; int * p; p = a; cout << *a << endl; cout << a[0] << endl; cout << *(a + 3) << endl; cout << *(p + 3) << endl; for (int i=0; i < MAX; i++) b[i]=a[i]; cout << sum(a, 5) << endl; cout << sum(p, 4) << endl; cout << sum(b, MAX) << endl;
return 0; } int main() { Student s1; Student s2("Jesus"); Student s3("Marissa Love", 3.75, 21); Student s4(s3); Student s5 = s2; Student * s6 = new Student("Jose");
s1 = s2;
s1.print(); s2.print(); s3.print(); s4.print(); s5.print(); s6->print(); delete s6; return 0; }
2. [Reference Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. How does this relate to passing-by-reference in function calls? Give an example.
int a = 8; int & p = a; cout << a << endl; p = 10; cout << a << endl;
2. [Reference Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. How does this relate to passing-by-reference in function calls? Give an example.
int a = 8; int & p = a; cout << a << endl; p = 10; cout << a << endl;
using namespace std;
//Sum the numbers in an array int sum(int * nums, int size) { int s = 0; for (int i = 0; i < size; i++) { s = s + nums[i]; } return s; }
3. [Pointer Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. Is passing-by-reference or passing-by-value used to pass the array to function sum?
int main() { int MAX = 3; int a[5] = {12, 2, 5, 3, 8}; int * b = new int[MAX]; int * p; p = a; cout << *a << endl; cout << a[0] << endl; cout << *(a + 3) << endl; cout << *(p + 3) << endl; for (int i=0; i < MAX; i++) b[i]=a[i]; cout << sum(a, 5) << endl; cout << sum(p, 4) << endl; cout << sum(b, MAX) << endl;
return 0; } using namespace std;
//Sum the numbers in an array int sum(int * nums, int size) { int s = 0; for (int i = 0; i < size; i++) { s = s + nums[i]; } return s; }
3. [Pointer Variables] Given the following code fragment, what is the output? Run the program on computer and compare your answer. Is passing-by-reference or passing-by-value used to pass the array to function sum?
int main() { int MAX = 3; int a[5] = {12, 2, 5, 3, 8}; int * b = new int[MAX]; int * p; p = a; cout << *a << endl; cout << a[0] << endl; cout << *(a + 3) << endl; cout << *(p + 3) << endl; for (int i=0; i < MAX; i++) b[i]=a[i]; cout << sum(a, 5) << endl; cout << sum(p, 4) << endl; cout << sum(b, MAX) << endl;
return 0; }
Explanation / Answer
Answer 2)
when you declare a array such as a[5]. it use a as a constant variable to hold the address of the first element of array. Constant means this address stored in a can not be changed.
and here p is a simple pointer to an integer which point to start of the array a .
so here
passing-by-reference is used to pass the array.
Answer 1) see comment link of paste bucket.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.