Chapter 11

55
Chapter 11 Class

description

Chapter 11. Class. Student Record. Student ID First name Last name Gender major GPA How do we store the data?. Store Student Records in Variables. // For one student at a time float GPA; string firstName , lastName ; string major; long id; char gender; …. - PowerPoint PPT Presentation

Transcript of Chapter 11

Chapter 11

Chapter 11ClassStudent RecordStudent IDFirst nameLast nameGendermajorGPA

How do we store the data?Store Student Records in Variables// For one student at a timefloat GPA;string firstName, lastName;string major;long id;char gender; 3Store Student Records in Parallel Arraysconst int MAX_STUDENTS = 100; long id [MAX_STUDENTS]; string firstName[MAX_STUDENTS]; string lastName [MAX_STUDENTS]; char gender [MAX_STUDENTS]; string major [MAX_STUDENTS]; float gpa [MAX_STUDENTS];

C++ Data Typesstructuredarray struct union class addresspointer referencesimple integral enumchar short int long boolfloatingfloat double long double5Store Student Records in Classclass Student{private: long id; string firstName; string lastName; char gender; string major; float gpa; public: long GetID ( ) { ... } void SetID ( long id ) { ... } ...}; // data fields (something new in class)// methods (something new in class)// semicolon after }// Student is the identifier of this class// indicates following variables are only accessible within class Student // indicates following methods can be used outside class Student C++ ClassSyntax:

class, private, and public are key words.

A class usually has two types of members: data fields and methods.Data fields should always be private!Method can be private or public. (We will explain this later.)class ClassName{private: DataMemberList; MethodMemberList1;public: MethodMemberList2;};Class Objectclass Student{private: long id; string firstName; ...};

Student stu1;

// Student is a new data type!// stu1 is an object of Student.// A class object is a variable of the class!// A Student object is a variable of Student data type!Class Methodsclass Student{private: long id; ... public: ... void PrintStudentRecord () { cout