1 / 24

Class Student

Class Student. class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA( float value ) void updateGPA( float amount ) . . . }; // Student is a data type

mirit
Download Presentation

Class Student

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Class Student class Student { private: string id; string firstName, lastName; float gpa; public: void Read() void Write() string getGPA() void setGPA( float value ) void updateGPA( float amount ) . . . }; // Student is a data type // Data and functions together

  2. Class and Object // Declare variable and reserve memory space int numStudents; // Declare class variable // memory space for data fields // methods Student s1; // Input data into object s1 s1.Read(); // No values for data members s1.Write();

  3. Initial Values // Declare variable with initial value int numStudents = 0; // Can we declare class variable with initial value? Student s1; Use Class Constructors!

  4. Class Default Constructor class Student { private: string id; string firstName, lastName; float gpa; public: Student() { id = “12345”; firstName = “first”; lastName = “last”; gpa = 2.5; } }; // Syntax for constructor // Default constructor has no parameters

  5. Default Class Constructor // Default constructor is called Student s1; // The default constructor has set the // values for data members s1.Write(); // This will change the data fields s1.Read(); // Output data of object s1 s1.Write(); Constructor is called implicitly when class variables are declared.

  6. Constructor with Parameters class Student { private: string id; string firstName, lastName; float gpa; public: Student(string sID, string first, string last, float g) { id = sID; firstName = first; lastName = last; gpa = g; } };

  7. Constructor with Parameters // Class constructor is called Student s1(“12345”, “John”, “Smith”, 2.8); // The constructor has set the // values for data members s1.Write(); // This will change gpa s1.UpdateGPA(0.1); // Output data of object s1 s1.Write();

  8. Class Constructors with Different Parameters class Student { private: . . . public: Student(string sID) { id = sID; // could set other default values } Student(string first, string last) { firstName = first; lastName = last; gpa = 2.5; // can still set gpa } };

  9. Class Constructors // Student(string sID) is called Student s1(“56789”); // Student(string first, string last) is called Student s2(“Qi”, “Yang”); // Student(String sID, string first, string last, // float g) is called Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Student() is called Student s4();

  10. Different Constructors Must Have Different Parameters class Student { private: . . . public: Student(string last, string first) { firstName = first; lastName = last; } Student(string first, string last) { firstName = first; lastName = last; } }; Will this work?

  11. Different Constructors Must Have Different Parameters // Which one will be called? // Student(string first, string last) // Student(string last, string first) Student s(“Qi”, “Yang”);

  12. Different Parameters:Different Type class Student { private: . . . public: Student(string last, string first) { firstName = first; lastName = last; } Student(string sID, float g) { id = sID; gpa = g; } };

  13. Different Parameters:Different Type // Student(string first, string last) is called Student s(“Qi”, “Yang”); // Student(string sID, float g) is called Student s(“12345”, 3.3);

  14. Class Constructors class Student { private: . . . public: Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) };

  15. Class Constructors // Student(string sID) is called Student s1(“56789”); // Student(string first, string last) is called Student s2(“Qi”, “Yang”); // Student(String sID, string first, string last, // float g) is called Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Student() is called Student s4(); // Student(string sID, float g) is called Student s(“12345”, 3.3);

  16. No Default Constructor class Student { private: . . . public: // Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) };

  17. No Default Constructor // Student(string sID) is called Student s1(“56789”); // Student(string first, string last) is called Student s2(“Qi”, “Yang”); // Student(String sID, string first, string last, // float g) is called Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Student(string sID, float g) is called Student s(“12345”, 3.3); // No default constructor if a constructor is defined. Student s4();

  18. No Constructors class Student { private: . . . public: /* Student() Student(string sID) Student(string first, string last) Student(string sID, float g) Student(string sID, string first, string last, float g) */ };

  19. Class Constructors // Invalid Student s1(“56789”); // Invalid Student s2(“Qi”, “Yang”); // Invalid Student s3(“13579”, “Joe”, “Clifton”, 3.9); // Invalid Student s(“12345”, 3.3); // Default constructor is good! Student s4(); // Will not work! s4.Write(); s4.UpdateGPA( 0.1 );

  20. Copy Constructor class Student { private: string id, firstName, lastName; float gpa; public: Student(string sID, string first, string last, float g) // Copy constructor: // gets initial values from object s Student( Student s) { id = s.id; firstName = s.firstName; lastName = s.lastName; gpa = s.gpa; } };

  21. Class Constructors // Student(String sID, string first, string last, // float g) is called Student s1(“13579”, “Joe”, “Clifton”, 3.9); int x, y = 10; // Copy constructor is called Student s2(s1); // Similar to the copy constructor x = y; s2.Write(); s2.UpdateGPA( 0.1 ); s2.Write();

  22. Schedule • Quiz7-2: Due Wednesday • Program 5 Pairs by Wednesday

  23. Test 1 Test 2 Test 3 Final

  24. Good Luck! • Come to classes/labs • Do the work by yourself • Get Help

More Related