Advertisement
Guest User

Copy Construct

a guest
Oct 15th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. struct Student {
  8. string first_name;
  9. string mid_name;
  10. string last_name;
  11. int fak_num;
  12. int grades[5];
  13.  
  14. Student ()
  15. {
  16. first_name=" ";
  17. mid_name=" ";
  18. last_name=" ";
  19. fak_num=-1;
  20. for (int i=0; i<5; i++) {
  21. grades[i]=0;
  22. }
  23. }
  24.  
  25.  
  26.  
  27. //Overwrite constructor
  28.  
  29. Student (string first,string second, string last, int fak_number,vector <double> m)
  30. {
  31. first_name=first;
  32. mid_name=second;
  33. last_name=last;
  34. fak_num=fak_number;
  35. for (int i=0; i<5; i++) {
  36. if (m[i]>6 || m[i]<2)
  37. grades[i]=6;
  38. else
  39. grades[i]=m[i];
  40.  
  41. }
  42. }
  43.  
  44. //Copy Constructor
  45.  
  46. Student (const Student& s) {
  47. cout << "Copy" << endl;
  48. first_name=s.first_name;
  49. mid_name=s.mid_name;
  50. last_name=s.last_name;
  51. fak_num=s.fak_num;
  52. for(int i=0; i<5; i++) {
  53. grades[i]=s.grades[i];
  54. }
  55. }
  56.  
  57.  
  58.  
  59. void print_student(){
  60. string arr_subjects[5]{"Math", "Science", "Chemistry", "Art", "Music"};
  61. cout<<"Name: "<<first_name<<" "<<mid_name<<" "<<last_name<<"\nFaculty number: "<<fak_num<<endl;
  62. for(int i=0; i<5; i++) {
  63. cout<<"Subject: "<<arr_subjects[i]<<" Grade: "<<grades[i]<<endl;
  64. }
  65. }
  66.  
  67. //Avarage
  68.  
  69. double avg() {
  70. double sum = 0;
  71. double avarage = 1;
  72. for(int i=0; i<5; i++) {
  73. sum = sum + grades[i];
  74. }
  75. avarage = sum / 5;
  76. return avarage;
  77. }
  78.  
  79. //Read
  80.  
  81. void read(istream& in=cin){
  82. cout<<"Enter first name: ";
  83. in>>first_name;
  84. cout<<"Enter middle name: ";
  85. in>>mid_name;
  86. cout<<"Enter last name: ";
  87. in>>last_name;
  88. cout<<"Enter faculty number :";
  89. in>>fak_num;
  90. for(int i=0; i<5; i++) {
  91. in>>grades[i];
  92. }
  93. }
  94.  
  95. };
  96.  
  97. //Functions (Not in the Structure)
  98.  
  99. Student read_student(){
  100. Student s;
  101. cout<<"Enter first name: ";
  102. cin>>s.first_name;
  103. cout<<"Enter middle name: ";
  104. cin>>s.mid_name;
  105. cout<<"Enter last name: ";
  106. cin>>s.last_name;
  107. cout<<"Enter faculty number :";
  108. cin>>s.fak_num;
  109. for(int i=0; i<5; i++) {
  110. cin>>s.grades[i];
  111. }
  112. return s;
  113. }
  114.  
  115. Student& read_student_ref(){
  116. Student* s=new Student;
  117. cout<<"Enter first name: ";
  118. cin>>s->first_name;
  119. cout<<"Enter middle name: ";
  120. cin>>s->mid_name;
  121. cout<<"Enter last name: ";
  122. cin>>s->last_name;
  123. cout<<"Enter faculty number :";
  124. cin>>s->fak_num;
  125. for(int i=0; i<5; i++) {
  126. cin>>s->grades[i];
  127. }
  128. return *s;
  129. }
  130.  
  131. int main () {
  132.  
  133. Student s1;
  134. Student s2={"Boyko","Metodiev","Borisov",001,{6,6,6,6,7}};
  135. cout<<s2.first_name<<" "<<s2.last_name<<" "<<s2.fak_num<<" Grades: "<<s2.grades<<endl;
  136. Student s4=s2;
  137. s4.print_student();
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement