| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 649 人关注过本帖
标题:大家好,C++新手的问题
只看楼主 加入收藏
terry64no
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2010-8-29
收藏
 问题点数:0 回复次数:5 
大家好,C++新手的问题
我刚学C++没多久,刚了解完类与对象,在做一个练习时,是关于输出时间的练习,下面是我的代码,但通过不了编译:

#include<iostream>

using std::cout;
using std::cin;
using std::endl;

#include<string>

using std::string;
using std::getline;

class Date
{
public:
       Date( int monthdate, int daydate, int yeardate )  //定义构造函数
       {
              setMonth( monthdate );
              setDay( daydate );
              setYear( yeardate );
       }
      
       void setMonth( int monthdate )   //month设置函数
       {
            if ( monthdate <= 12 )     //将 形参赋值给实参,if语句,判断作用
            month = monthdate;
                        
            if ( monthdate >12 )
            {
               
                monthdate = 1;
                month = monthdate;
                cout << "Month \"" << monthdate << "exceeds maximun number(12)\n"
                << endl;
            }   
       }
      
      
       int getMonth( )    //month获取函数
       {
           return month;        //返回数据成员month的值
       }
      
      void setDay( int daydate )    //day设置函数
      {
            day = daydate;
      }
      
      int getDay()         //定义day获取函数
      {
          return day;
      }
      
      void setYear ( int yeardate )   //定义yaer设置函数
      {
           year = yeardate;
      }
      
      int getYear()
      {
          return year;
      }
      
           
       void displayMessage()
       {
            cout << "Your FL year is:" << getMonth() << "/" << getDay() << "/" << getYear()
            << "~~!" << endl;
       }  //后面由主函数直接调用,显示 “月/日/年”。
      
private:
        int month, day, year;   //三个数据成员
     
};     //完成类定义

int main()
{
     
    int  monthOfFl;
    int  dayOfFl;
    int  yearOfFl;
    Date myDate;   //创立对象myDate,编译器显示这行有问题,不明白?
   
    cout << "initial date is: " << myDate. getMonth()<< "/"
    << myDate. getDay() << "/" << myDate. getYear <<endl;
   
    cout << "enter the month!" << endl;
    getline( cin, monthOfFl );
    myDate.setMonth( monthOfFl );
   
    cout << "enter the day!" << endl;
    getline( cin, dayOfFl );
    myDate.setDay( monthOfFl );
   
    cout << "enter the year!" << endl;
    getline( cin, yearOfFl );
    myDate.setYear( yearOfFl );
   
    cout << endl;
    myDate.displayMessage();
    system("pause");
    return 0;
}
               
请大家指教,我用的是DEV
搜索更多相关主题的帖子: include public 
2010-08-29 20:23
无名可用
Rank: 4
等 级:业余侠客
帖 子:79
专家分:259
注 册:2010-7-27
收藏
得分:0 
程序代码:
#include<iostream>

using std::cout;
using std::cin;
using std::endl;

#include<string>

using std::string;
using std::getline;

class Date
{
public:
   
       Date() //缺少一个不带参数的构造方法,因为你创建对象时没有给出参数
       {
              month=1;
              day=1;
              year=1900;             
       }
      
       Date( int monthdate, int daydate, int yeardate )  //定义构造函数
       {
              setMonth( monthdate );
              setDay( daydate );
              setYear( yeardate );
       }
      
       void setMonth( int monthdate )   //month设置函数
       {
            if ( monthdate <= 12 )     //将 形参赋值给实参,if语句,判断作用
            month = monthdate;
                       
            if ( monthdate >12 )
            {
              
                monthdate = 1;
                month = monthdate;
                cout << "Month \"" << monthdate << "exceeds maximun number(12)\n"
                << endl;
            }  
       }
      
      
       int getMonth( )    //month获取函数
       {
           return month;        //返回数据成员month的值
       }
      
      void setDay( int daydate )    //day设置函数
      {
            day = daydate;
      }
     
      int getDay()         //定义day获取函数
      {
          return day;
      }
     
      void setYear ( int yeardate )   //定义yaer设置函数
      {
           year = yeardate;
      }
     
      int getYear()
      {
          return year;
      }
     
          
       void displayMessage()
       {
            cout << "Your FL year is:" << getMonth() << "/" << getDay() << "/" << getYear()
            << "~~!" << endl;
       }  //后面由主函数直接调用,显示 “月/日/年”。
      
private:
        int month, day, year;   //三个数据成员
    
};     //完成类定义

int main()
{
    
    int  monthOfFl;
    int  dayOfFl;
    int  yearOfFl;
    Date myDate;   //创立对象myDate,编译器显示这行有问题,不明白?...//你创建的是一个不带参数的对象
   
    cout << "initial date is: " << myDate. getMonth()<< "/"
    << myDate. getDay() << "/" << myDate. getYear <<endl;
   
    cout << "enter the month!" << endl;
    cin>>monthOfFl;//将getline()改为cin>>就行了,getline()是用于输入string的
    myDate.setMonth( monthOfFl );
   
    cout << "enter the day!" << endl;
    cin>>dayOfFl;//将getline()改为cin>>
    myDate.setDay( monthOfFl );
   
    cout << "enter the year!" << endl;
    cin>>yearOfFl;//将getline()改为cin>>
    myDate.setYear( yearOfFl );
   
    cout << endl;
    myDate.displayMessage();
    system("pause");
    return 0;
2010-08-29 20:44
ragnaros
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:15
专家分:105
注 册:2010-7-26
收藏
得分:0 
程序代码:
#include<iostream>

using std::cout;
using std::cin;
using std::endl;

#include<string>

using std::string;
using std::getline;

class Date
{
public:
       Date() //缺少一个不带参数的构造方法,因为你创建对象时没有给出参数
       {
        month=1;
        day=1;
        year=1900;            
       }

       Date( int monthdate, int daydate, int yeardate )  //定义构造函数
       {
              setMonth( monthdate );
              setDay( daydate );
              setYear( yeardate );
       }
      
       void setMonth( int monthdate )   //month设置函数
       {
            if ( monthdate <= 12 )     //将 形参赋值给实参,if语句,判断作用
            month = monthdate;
                       
            if ( monthdate >12 )
            {
              
                monthdate = 1;
                month = monthdate;
                cout << "Month \"" << monthdate << "exceeds maximun number(12)\n"
                << endl;
            }  
       }
      
      
       int getMonth( )    //month获取函数
       {
           return month;        //返回数据成员month的值
       }
      
      void setDay( int daydate )    //day设置函数
      {
            day = daydate;
      }
     
      int getDay()         //定义day获取函数
      {
          return day;
      }
     
      void setYear ( int yeardate )   //定义yaer设置函数
      {
           year = yeardate;
      }
     
      int getYear()
      {
          return year;
      }
     
          
       void displayMessage()
       {
            cout << "Your FL year is:" << getMonth() << "/" << getDay() << "/" << getYear()
            << "~~!" << endl;
       }  //后面由主函数直接调用,显示 “月/日/年”。
      
private:
        int month, day, year;   //三个数据成员
    
};     //完成类定义

int main()
{
    
    int  monthOfFl;
    int  dayOfFl;
    int  yearOfFl;
    Date myDate;  
   
    cout << "initial date is: " << myDate. getMonth()<< "/" 
        << myDate. getDay() << "/" << myDate. getYear() <<endl;  //这里getYear后面漏掉括号
   
    cout << "enter the month!" << endl;
    cin >> monthOfFl;  //改为cin, 因为getline是用来输入string类型的
    myDate.setMonth( monthOfFl );
   
    cout << "enter the day!" << endl;
    cin >> dayOfFl;    //改为cin, 因为getline是用来输入string类型的
    myDate.setDay( dayOfFl );   //这里 monthOfFl 要改成dayOfFl
   
    cout << "enter the year!" << endl;
    cin >> yearOfFl;   //改为cin, 因为getline是用来输入string类型的
    myDate.setYear( yearOfFl );
   
    cout << endl;
    myDate.displayMessage();
    system("pause");
    return 0;
}
参考楼上那位大哥,顺便改了下程序中的小错误。
2010-08-29 22:23
terry64no
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2010-8-29
收藏
得分:0 
看了楼上两位的改正,明白原理了!
2010-08-30 20:32
terry64no
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2010-8-29
收藏
得分:0 
还有一个问题,为什么我不用这一段
Date() //缺少一个不带参数的构造方法,因为你创建对象时没有给出参数
       {
        month=1;
        day=1;
        year=1900;            
       }

改用在主函数里直接用

int  monthOfFl;
    int  dayOfFl;
    int  yearOfFl;
    Date myDate(1,1,1900);    //直接在这里赋初值

却通不过编译?
不可以这样吗?
2010-08-30 20:52
terry64no
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2010-8-29
收藏
得分:0 
原来是符号错了。。。。
2010-08-30 20:56
快速回复:大家好,C++新手的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015093 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved