SSL: 1
Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893

Warning: Cannot modify header information - headers already sent by (output started at /www/blog/wp-includes/load.php:1646) in /www/blog/wp-includes/rest-api/class-wp-rest-server.php on line 1893
{"id":472,"date":"2016-08-11T17:44:24","date_gmt":"2016-08-11T09:44:24","guid":{"rendered":"https:\/\/blog.weskiller.com\/?p=472"},"modified":"2016-08-12T14:03:49","modified_gmt":"2016-08-12T06:03:49","slug":"c-%e7%bb%83%e4%b9%a0","status":"publish","type":"post","link":"https:\/\/blog.gamein.vip\/472.html","title":{"rendered":"C++ \u7ec3\u4e60"},"content":{"rendered":"

\u4eca\u5929\u4e0b\u5348\u770b\u4e66\u7684\u65f6\u5019\u53d1\u73b0\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u95ee\u9898\u3002<\/span>
\n\u300aC++ Primer Plus\u300b<\/span>
\n\u793a\u4f8b14.1(studentc.h)\u4e2d\uff0c\u5b9a\u4e49\u4e86\u4e24\u4e2a\u975e\u5e38\u5947\u602a\u7684\u7c7b\u6210\u5458\u51fd\u6570<\/span><\/p>\n

\r\n#ifndef STUDENTC_H_\r\n#define STUDENTC_H_\r\n\r\n#include <iostream>\r\n#include <string>\r\n#include <valarray>\r\n\r\nclass Student\r\n{\r\nprivate:\r\n        typedef std::valarray<double> ArrayDb;\r\n        std::string name;\r\n        ArrayDb scores;\r\n        std::ostream & arr_out(std::ostream & os) const;\r\npublic:\r\n        Student() :name("Null Student"), scores() {}\r\n        explicit Student(const std::string & s) :name(s), scores() {}\r\n        explicit Student(int n) :name("Nully"), scores(n) {}\r\n        Student(const std::string & s, int n) :name(s), scores(n) {}\r\n        Student(const std::string & s, const ArrayDb & a) : name(s), scores(a) {}\r\n        Student(const char * str, const double * pd, int n) :name(str), scores(pd,n) {}\r\n        ~Student() {}\r\n        double Average() const;\r\n        const std::string & Name() const;\r\n\/\/[]\u91cd\u8f7d\u51fd\u65701\uff0c\u4f3c\u4e4e\u80fd\u5339\u914d\u6240\u6709\u7684\u8c03\u7528\r\n        double & operator[](int i);\r\n\/\/[]\u91cd\u8f7d\u51fd\u65702\uff0c\u5728\u6709\u4e0a\u9762\u7684\u91cd\u8f7d\u51fd\u6570\u4e0b\uff0c\u4f3c\u4e4e\u6c38\u8fdc\u5339\u914d\u4e0d\u5230\r\n        double operator[](int i) const;\r\n\r\n        friend std::istream & operator>>(std::istream & is, Student & stu);\r\n        friend std::istream & getline(std::istream & is, Student & stu);\r\n        friend std::ostream & operator<<(std::ostream & os,const Student & stu);\r\n};\r\n\r\n#endif\r\n<\/pre>\n

\u5728\u5b9e\u73b0\u4e2d\u5206\u522b\u6dfb\u52a0\u4e24\u4e2a\u91cd\u8f7d\u51fd\u6570\u7684\u8f93\u51fa\uff0c\u67e5\u770b\u8c03\u7528\u7684\u60c5\u51b5<\/span><\/p>\n

\r\n#include "studentc.h"\r\nusing std::ostream;\r\nusing std::endl;\r\nusing std::istream;\r\nusing std::string;\r\n\r\ndouble Student::Average() const\r\n{\r\n\tif (scores.size() > 0)\r\n\t\treturn scores.sum() \/ scores.size();\r\n\telse\r\n\t\treturn 0;\r\n}\r\n\r\nconst string & Student::Name() const\r\n{\r\n\treturn name;\r\n}\r\n\r\ndouble & Student::operator[](int i)\r\n{\r\n\tstd::cout << "Use double & " << std::endl;\r\n\treturn scores[i];\r\n}\r\n\r\ndouble Student::operator[](int i) const\r\n{\r\n\tstd::cout << "Use double " << std::endl;\r\n\treturn scores[i];\r\n}\r\n\r\nostream & Student::arr_out(ostream & os) const\r\n{\r\n\tint i;\r\n\tint lim = scores.size();\r\n\tif (lim > 0)\r\n\t{\r\n\t\tfor(i = 0; i < lim; i++)\r\n\t\t{\r\n\t\t\tos << scores[i] << " ";\r\n\t\t\tif ( i % 5 == 4)\r\n\t\t\t\tos << endl;\r\n\t\t}\r\n\t\tif ( i % 5 != 0)\r\n\t\t\tos << endl;\r\n\t}\r\n\telse\r\n\t\tos << " empty array ";\r\n\treturn os;\r\n}\r\n\r\nistream & operator>>(istream & is, Student & stu)\r\n{\r\n\tis >> stu.name;\r\n\treturn is;\r\n}\r\n\r\nistream & getline(istream & is, Student & stu)\r\n{\r\n\tgetline(is,stu.name);\r\n\treturn is;\r\n}\r\n\r\nostream & operator<<(ostream & os, const Student & stu)\r\n{\r\n\tos << "Scores for " << stu.name << ":\\n";\r\n\tstu.arr_out(os);\r\n\treturn os;\r\n}\r\n<\/pre>\n
\r\n#include "studentc.h"\r\n#include <iostream>\r\nusing std::cin;\r\nusing std::cout;\r\nusing std::endl;\r\n\r\nvoid set(Student & sa, int n);\r\nconst int pupils = 3;\r\nconst int quizzes = 5;\r\n\r\nint main(void)\r\n{\r\n\tStudent ada[pupils] = { Student(quizzes), Student(quizzes), Student(quizzes) };\r\n\tint i;\r\n\tfor (i = 0; i < pupils; ++i)\r\n\t\tset(ada[i], quizzes);\r\n\tcout << "\\nStudent List:\\n";\r\n\tfor (i = 0; i < pupils; ++i)\r\n\t\tcout << ada[i].Name() << endl;\r\n\tcout << "\\nResults:";\r\n\tfor (i = 0; i < pupils; ++i)\r\n\t{\r\n\t\tcout << endl << ada[i];\r\n\t\tcout << "average: " << ada[i].Average() << endl;\r\n\t}\r\n\tcout << "Done.\\n";\r\n\treturn 0;\r\n}\r\n\r\nvoid set(Student & sa, int n)\r\n{\r\n\tcout << "Please enter the student's name: ";\r\n\tgetline(cin,sa);\r\n\tcout << "Please enter " << n << " quiz scores:\\n";\r\n\tfor (int i = 0; i < n; i++)\r\n\t\tcin >> sa[i];\r\n\twhile (cin.get() != '\\n')\r\n\t\tcontinue;\r\n}\r\n[code title="\u8f93\u51fa\u7ed3\u679c"]\r\nPlease enter the student's name: Gil Bayts\r\nPlease enter 5 quiz scores:\r\nUse double & \r\n92 94 96 93 95\r\nUse double & \r\nUse double & \r\nUse double & \r\nUse double & \r\nPlease enter the student's name: Pat Roone\r\nPlease enter 5 quiz scores:\r\nUse double & \r\n83 89 72 78 95\r\nUse double & \r\nUse double & \r\nUse double & \r\nUse double & \r\nPlease enter the student's name: Fleur O'Day\r\nPlease enter 5 quiz scores:\r\nUse double & \r\n92 89 96 74 64\r\nUse double & \r\nUse double & \r\nUse double & \r\nUse double & \r\n\r\nStudent List:\r\nGil Bayts\r\nPat Roone\r\nFleur O'Day\r\n\r\nResults:\r\nScores for Gil Bayts:\r\n92 94 96 93 95 \r\naverage: 94\r\n\r\nScores for Pat Roone:\r\n83 89 72 78 95 \r\naverage: 83.4\r\n\r\nScores for Fleur O'Day:\r\n92 89 96 74 64 \r\naverage: 83\r\nDone.\r\n<\/pre>\n

\u53ef\u4ee5\u770b\u5230\uff0cconst\u7248\u672c\u7684operator[]\u5b8c\u5168\u6ca1\u6709\u5339\u914d\u5230\u3002<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"

\u4eca\u5929\u4e0b\u5348\u770b\u4e66\u7684\u65f6\u5019\u53d1\u73b0\u4e00\u4e2a\u5f88\u6709\u8da3\u7684\u95ee\u9898\u3002 \u300aC++ Primer Plus\u300b \u793a\u4f8b14.1(studentc. […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[23],"class_list":["post-472","post","type-post","status-publish","format-standard","hentry","category-c","tag-c"],"_links":{"self":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/comments?post=472"}],"version-history":[{"count":26,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/472\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/posts\/472\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/media?parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/categories?post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gamein.vip\/wp-json\/wp\/v2\/tags?post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}