Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1430: Programming in C++.

Similar presentations


Presentation on theme: "CS 1430: Programming in C++."— Presentation transcript:

1 CS 1430: Programming in C++

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

3 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();

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

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

6 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 Default Constructor is called implicitly when class variables are declared.

7 Class Default Constructor
class Student { private: string id; string firstName, lastName; float gpa; public: Student() id = “”; firstName = “”; lastName = “”; gpa = 0.0; } }; // Empty strings and zero: special values. // Not garbage!

8 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; } };

9 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

10 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 = 0.0; // can set gpa with a default value };

11 Class Constructors // Student(string sID) is called
// 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();

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

13 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”);

14 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; };

15 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);

16 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) };

17 Class Constructors // Student(string sID) is called
// 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);

18 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) };

19 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(); // Invalid!

20 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) */ };

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

22 // --------------------------------------------------
// Each class must have a comment block // to describe it. class Student { private: string id; string firstName, lastName; float gpa; public: // constructors Student() . . . Student(string sID, string first, string last) // methods void Read() void Write() float GetGPA() };

23 class Student { private: string id, firstName, lastName; float gpa; // Why do we need it? // Can we do this? // What is this? Student (string sID) id = sID; } public: // constructors Student() . . . Student(string sID, string first, string last) // methods void WhatMethod(. . .) };

24 class Student { private: string id, firstName, lastName; float gpa; // private constructor Student (string sID) . . . // private method // what is this? int WhatToDo() public: // constructors Student() Student(string sID, string first, string last) // methods void WhatMethod(. . .) };

25 class Student { private: string id, firstName, lastName; float gpa; Student (string sID) int WhatToDo() public: Student() Student(string sID, string first, string last) // methods void WhatMethod() void Read() void Write() string GetFirst() float getGPA() void setGPA( float value ) void updateGPA( float amount ) };

26 class Student { private: . . . int WhatToDo() public: void WhatMethod() }; int main() Student s1, s2; s1.Read(); s1.Write(); s1.SetGPA(3.1); s1.WhatMethod(); // valid? s1.WhatToDo(); // valid? return 0; }

27 class Student { . . . }; int main() Student s1, s2; s1.Read(); s1.Write(); Student s3(“Qi”, “Yang”, “1418”); s3.SetGPA(3.1); s2 = s3; // valid? s2.Write(); if (s1 > s2) // valid? cout << “Student ” << s1.GetFirst() << “ is better than ” << s2.GetFirst() << ‘.’; return 0; }

28 class Student { . . . }; int main() Student s1, s2; s1.Read(); s1.Write(); Student s3(“Qi”, “Yang”, “1418”); s3.SetGPA(3.1); s2 = s3; // Good! Copy all fields. s2.Write(); if (s1 > s2) // Invalid! cout << “Student ” << s1.GetFirst() << “ is better than ” << s2.GetFirst() << ‘.’; return 0; }

29 class Student { . . . }; int main() Student s1, s2; Student s3(“Qi”, “Yang”, “1418”); s3.SetGPA(3.1); s2 = s3; // Good! Copy all fields. if (s1.GetGPA() > s2.GetGPA()) // Valid! cout << “Student ” << s1.GetFirst() << “ is better than ” << s2.GetFirst() << ‘.’; cin >> s1; // Valid? cout << s1; // Valid? return 0; }

30 Schedule Quiz7-2: Due Monday Program 4 Lab8 Test 2

31 Prog4 Five required functions You can make additional functions
Follow the Programming Rules


Download ppt "CS 1430: Programming in C++."

Similar presentations


Ads by Google