class student

24
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 1

Upload: mirit

Post on 25-Feb-2016

51 views

Category:

Documents


1 download

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 Presentation

TRANSCRIPT

Page 1: Class Student

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

Page 2: Class Student

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

Page 3: Class Student

Initial Values

// Declare variable with initial valueint numStudents = 0;

// Can we declare class variable with initial value?Student s1;

Use Class Constructors!

3

Page 4: Class Student

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

Page 5: Class Student

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

Page 6: Class Student

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

Page 7: Class Student

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

Page 8: Class Student

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

Page 9: Class Student

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

Page 10: Class Student

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

Page 11: Class Student

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

Page 12: Class Student

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

Page 13: Class Student

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

Page 14: Class Student

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

Page 15: Class Student

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

Page 16: Class Student

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

Page 17: Class Student

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

Page 18: Class Student

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

Page 19: Class Student

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

Page 20: Class Student

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

Page 21: Class Student

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

Page 22: Class Student

Schedule

• Quiz7-2: Due Wednesday

• Program 5Pairs by Wednesday

22

Page 23: Class Student

23

Test 1

Test 2

Test 3

FinalFinal

Page 24: Class Student

24

Good Luck!

• Come to classes/labs

• Do the work by yourself

• Get Help