联系我们: 手动添加方式: 微信>添加朋友>企业微信联系人>13262280223 或者 QQ: 1483266981
Block 5: Classes Carlos Díaz (cdv7@leicester.ac.uk) Academic Year 2021-2022 EC7103-C++ Programming for Finance 2021-2022 Block 5: Classes EC7103 1 / 14 Introduction Class: User-defined datatype which groups together related pieces of information. Example: We can define a class called student that will contain the following information (fields or members). ID number. Mark in Coursework 1. Mark in Coursework 2. Mark in Coursework 3. Mark in the Final Exam. We can define this class as class student { public: int IDnumber; double CW1; double CW2; double CW3; double FE; }; 2021-2022 Block 5: Classes EC7103 2 / 14 Objects An instance is an occurrence of a class (object). We can declare an instance as follows: class student { public: int IDnumber; double CW1; double CW2; double CW3; double FE; }; int main(){ student student1; student student2; return 0; } 2021-2022 Block 5: Classes EC7103 3 / 14 Accessing members We can access the members of a class by using the dot (.) operator. int main(){ student student1; student student2; student1.IDnumber = 134876209; cout << "The ID number of student 1 isn"; cout << student1.IDnumber << endl; return 0; } 2021-2022 Block 5: Classes EC7103 4 / 14 Passing members to function We can pass (by value or reference) classes to functions double mark(student s){ double mark = 0.0; return mark = (s.CW1+s.CW2+s.CW3+s.FE)/4.0; } int main(){ student student1; student1.IDnumber = 135675943; student1.CW1 = 85.0; student1.CW2 = 90.0; student1.CW3 = 75.0; student1.FE = 60.0; double mark_student1 = mark(student1); cout << "The mark of student with ID no " << student1.IDnumber << " is " << mark_student1; return 0; } 2021-2022 Block 5: Classes EC7103 5 / 14 Methods Methods are functions which are part of a class. mark is a method of the class student. The following print function is also a method of the same class. void print(student s) { cout << "Student ID number " << s.IDnumber << endl; cout << "Mark in Coursework 1 " << s.CW1 << endl; cout << "Mark in Coursework 2 " << s.CW2 << endl; cout << "Mark in Coursework 3 " << s.CW3 << endl; cout << "Mark in the final exam " << s.FE << endl; } 2021-2022 Block 5: Classes EC7103 6 / 14 Implementing methods separately We can create a header file with extension .h containing members and methods and named after the class #ifndef STUDENT_H_ #define STUDENT_H_ class student { public: int IDnumber; double CW1; double CW2; double CW3; double FE; void print(); double mark(); }; #endif 2021-2022 Block 5: Classes EC7103 7 / 14 Implementing methods separately Methods are usually programmed in a different cpp file. #include #include "student.h" using namespace std; double student::mark() { double mark = 0.0; return mark = (CW1+CW2+CW3+FE)/4.0; } void student::print() { cout << "Student ID number " << IDnumber << endl; cout << "Mark in Coursework 1 " << CW1 << endl; cout << "Mark in Coursework 2 " << CW2 << endl; cout << "Mark in Coursework 3 " << CW3 << endl; cout << "Mark in the final exam " << FE << endl; } 2021-2022 Block 5: Classes EC7103 8 / 14 Public and private members Public members can be accessed from everywhere outside the class (e.g. the main function). Private members (default or declared under private:) can only accessed within the class. class student { private: int IDnumber; double CW1; public: double CW2; double CW3; double FE; }; 2021-2022 Block 5: Classes EC7103 9 / 14 Constructors and destructors A constructor is a member function invoked when an object is declared. Constructors initialize the members of the class. A constructor must have the same name than the class. If a constructor is not explicitly declared, the system will automatically create one. In our case, a constructor would be something like: student(int id,double c1,double c2,double c3,double f) { IDnumber = id; CW1 = c1; CW2 = c2; CW3 = c3; FE = f; } We can use it when declaring an instance in the main function: student student1(135675943,85.0,90.0,75.0,60.0); 2021-2022 Block 5: Classes EC7103 10 / 14 The vector class The vector class allows to work with vectors. To use the class we must include the header. #include A vector is declared as follows vector name(size); Some members of the vector class: Member Function size() Return the size resize(a,b) Change the size assign(a,b) Assign values to vector push_back(a) Append an element at the end clear Clear the content insert(i,a) Inserts ’a’ in position ’i-1’ erase(i) Erase the element in position ’i’ 2021-2022 Block 5: Classes EC7103 11 / 14 The vector class Vectors are similar to arrays. vector v1; for(int i=1;i<=100;i++){ v1.push_back(i); cout << v1[i-1] << endl; } double mean = 0.0; for (int i=0;imean+=v1[i]/v1.size(); } double var = 0.0; for (int i=0;ivar+=(v1[i]-mean)*(v1[i]-mean)/(v1.size()-1); } 2021-2022 Block 5: Classes EC7103 12 / 14 The vector class Vectors can be passed as arguments of functions. double mean_var(vector x, double& var) { double mx = 0.0; for (int i=0;imx+=x[i]/x.size(); } for (int i=0;ivar+=(pow((x[i]-mx),2))/(x.size()-1); } return mx; } 2021-2022 Block 5: Classes EC7103 13 / 14 The vector class We can use vectors to load data from an external file. ifstream source("file.txt"); vector y; double a; if (source.is_open()){ while(!source.eof()){ source >> a; y.push_back(a); } source.close(); } We need to include vector and fstream. 2021-2022 Block 5: Classes EC7103 14 / 14 Seminar Question Download the file data.txt Load it to a new cpp file Calculate and display the mean of this data 2021-2022 Block 5: Classes EC7103 15 / 14


发表评论