程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> c++-C++類的組合編程問題求幫忙

c++-C++類的組合編程問題求幫忙

編輯:編程解疑
C++類的組合編程問題求幫忙

設計出生日期類birthday:
year,month,day
設計學生類student:
No(學號) ,Name(姓名),Birth(生日),Score(成績)
下面是主函數部分
int main()
{
Birthday d(2002,1,1);
Student s1;
Student
s2("102","AAA",98,2001,1,1);
s1.disp();
s2.disp();
Student
s3("103","BBB",90,d);
s3.disp();
return 0;
}

幫忙添加兩個類,再用類的組合實現並完善一下這個程序~初學者求解,謝謝

最佳回答:


 class CBirthday 
{
public:
    CBirthday()
    {
        m_nYear = 1990;
        m_nMonth = 9;
        m_nDay = 25;
    }
    CBirthday(int nYear, int nMonth, int nDay)
    {
        m_nYear = nYear;
        m_nMonth = nMonth;
        m_nDay = nDay;
    }
public:
int     m_nYear;
int     m_nMonth;
int     m_nDay;

};

class CStudent
{
public:
    CStudent()
    {
        m_strName = "李二狗";
        m_strNo = "110110";
        m_nScore = 0;
    }

    CStudent(const string &strNo, const string &strName, int nScore, int nYear, int nMonth, int nDay)
    {
        m_strNo = strNo;
        m_strName = strName;
        m_nScore = nScore;
        m_Birthday.m_nYear = nYear;
        m_Birthday.m_nMonth = nMonth;
        m_Birthday.m_nDay = nDay;
    }

    CStudent(const string &strNo, const string &strName, int nScore, CBirthday birthday)
    {
        m_strNo = strNo;
        m_strName = strName;
        m_nScore = nScore;
        m_Birthday = birthday;
    }

    void Disp()
    {
        cout<<"姓名:"<<m_strName.c_str() <<endl;
        cout<<"學號:"<<m_strNo.c_str() <<endl;
        cout<<"生日:"<<m_Birthday.m_nYear<<"-"<<m_Birthday.m_nMonth<<"-"<<m_Birthday.m_nDay <<endl;
        cout<<"成績:"<<m_nScore <<endl;
    }

private:
    string      m_strNo;
    string      m_strName;
    CBirthday   m_Birthday;
    int         m_nScore;
};

int _tmain(int argc, _TCHAR* argv[])
{
    CBirthday d(2002,1,1);
    CStudent s1;
    CStudent s2("102","AAA",98,2001,1,1);
    s1.Disp();
    s2.Disp();
    CStudent s3("103","BBB",90,d);
    s3.Disp();

    return 0;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved