class student
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
Class Studentclass 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
1
Class and Object
// Declare variable and reserve memory spaceint numStudents;
// Declare class variable// memory space for data fields// methodsStudent s1;
// Input data into object s1s1.Read();
// No values for data memberss1.Write();
2
Initial Values
// Declare variable with initial valueint numStudents = 0;
// Can we declare class variable with initial value?Student s1;
Use Class Constructors!
3
Class Default Constructorclass 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
4
Default Class Constructor
// Default constructor is calledStudent s1;
// The default constructor has set the // values for data memberss1.Write();
// This will change the data fieldss1.Read();
// Output data of object s1s1.Write();
Constructor is called implicitly when class variables are declared.
5
Constructor with Parametersclass 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; }};
6
Constructor with Parameters
// Class constructor is calledStudent s1(“12345”, “John”, “Smith”, 2.8);
// The constructor has set the // values for data memberss1.Write();
// This will change gpas1.UpdateGPA(0.1);
// Output data of object s1s1.Write();
7
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 }}; 8
Class Constructors
// Student(string sID) is calledStudent s1(“56789”);
// Student(string first, string last) is calledStudent s2(“Qi”, “Yang”);
// Student(String sID, string first, string last, // float g) is calledStudent s3(“13579”, “Joe”, “Clifton”, 3.9);
// Student() is calledStudent s4();
9
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?10
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”);
11
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; }};
12
Different Parameters:Different Type
// Student(string first, string last) is calledStudent s(“Qi”, “Yang”);
// Student(string sID, float g) is calledStudent s(“12345”, 3.3);
13
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)
};
14
Class Constructors
// Student(string sID) is calledStudent s1(“56789”);
// Student(string first, string last) is calledStudent s2(“Qi”, “Yang”);
// Student(String sID, string first, string last, // float g) is calledStudent s3(“13579”, “Joe”, “Clifton”, 3.9);
// Student() is calledStudent s4();
// Student(string sID, float g) is calledStudent s(“12345”, 3.3);
15
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)
};
16
No Default Constructor
// Student(string sID) is calledStudent s1(“56789”);
// Student(string first, string last) is calledStudent s2(“Qi”, “Yang”);
// Student(String sID, string first, string last, // float g) is calledStudent s3(“13579”, “Joe”, “Clifton”, 3.9);
// Student(string sID, float g) is calledStudent s(“12345”, 3.3);
// No default constructor if a constructor is defined.Student s4();
17
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)*/};
18
Class Constructors// InvalidStudent s1(“56789”);
// InvalidStudent s2(“Qi”, “Yang”);
// InvalidStudent s3(“13579”, “Joe”, “Clifton”, 3.9);
// InvalidStudent s(“12345”, 3.3);
// Default constructor is good!Student s4();
// Will not work!s4.Write();s4.UpdateGPA( 0.1 );
19
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; }}; 20
Class Constructors// Student(String sID, string first, string last, // float g) is calledStudent s1(“13579”, “Joe”, “Clifton”, 3.9);
int x, y = 10;
// Copy constructor is calledStudent s2(s1);
// Similar to the copy constructorx = y;
s2.Write();
s2.UpdateGPA( 0.1 );
s2.Write();21
Schedule
• Quiz7-2: Due Wednesday
• Program 5Pairs by Wednesday
22
23
Test 1
Test 2
Test 3
FinalFinal
24
Good Luck!
• Come to classes/labs
• Do the work by yourself
• Get Help