码迷,mamicode.com
首页 > 其他好文 > 详细

类和对象-4

时间:2014-06-06 12:46:19      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

一个考试成绩结果统计的小程序,共3个文件

Analysis.h:头文件声明

bubuko.com,布布扣
// Analysis.h
// Definition of class Analysis that analyzes examination results.
// Member-function is defined in Analysis.cpp

// Analysis class definition
class Analysis
{
public:
    // process 10 students‘ examination results
    void processExamResults();
};    // end class Analysis
bubuko.com,布布扣

 

Analysis.cpp:统计分析的具体实现

bubuko.com,布布扣
// Analysis.cpp
// Member-functions for class Analysis that
// analyzes examination results

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

// include definition of class Analysis from Analysis.h
#include "Analysis.h"

// process the examination results of 10 students
void Analysis::processExamResults()
{
    // initializing variables in declaration
    int passes = 0;        // number of passes
    int failures = 0;    // number of failures
    int studentCounter = 1;    // student counter
    int result;            // one exam result ( 1 = pass, 2 = fail )

    // process 10 students using counter-controlled loop
    while (studentCounter<=10)
    {
        // prompt user for input and obtain value from user
        cout << "Enter result ( 1 = pass, 2 = fail )";
        cin >> result;    // input result

        // if...else nested in while
        if (result == 1)    // if result is 1
        {
            passes++;        // increment passes,
        } 
        else                // if result is not 1, so
        {    
            failures++;        // increment failures
        }

        studentCounter++;    // increment studentCounter to loop eventually terminates
    }    // end while

    // termination phase: display number of passes and failures
    cout << "Passed: " << passes << "\nFailures: " << failures << endl;

    // determine whether more than 8 students passes
    if( passes >= 8 )
        cout << "Raise tuition!\n";
}    // end function processExamResults
bubuko.com,布布扣

 

test.cpp:测试文件

bubuko.com,布布扣
// test.cpp
// Test program for class Analysis
#include "Analysis.h"

int main()
{
    Analysis application;        // create Analysis object
    application.processExamResults();    // call function to process results

    return 0;    // indicate successful termination
}    // end main
bubuko.com,布布扣

 

良好的编程习惯

与二元运算符不同,一元的自增和自减运算符应该紧邻其操作数,中间不能有任何数据

 

常见的编程错误

企图用表达式(不是一个可修改变量的名字和引用),例如++(x+1),作为自增或自减运算符操作将是一个语法错误

 

类和对象-4,布布扣,bubuko.com

类和对象-4

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/tmmuyb/p/3766791.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!