标题:C++新手,完成老师布置的作业,练习多重继承,代码出现错误,弄半天弄不懂, ...
只看楼主
a853052879
Rank: 2
等 级:论坛游民
帖 子:39
专家分:10
注 册:2015-8-17
结帖率:60%
已结贴  问题点数:20 回复次数:3 
C++新手,完成老师布置的作业,练习多重继承,代码出现错误,弄半天弄不懂,求大神指教
#include<iostream>
#include<string>
using namespace std;
Class Date
{
    public:
        Date(int Nian,int Yue,int Ri)
        {
            nian=Nian;
            yue=Yue;
            ri=Ri;
        }
        void display()
        {
        cout<<nian<<" "<<yue<<" "<<ri<<endl;
        }
        private:
            int nian;
            int yue;
            int ri;
};

Class Time
{
    public:
        Time(int Shi,int Fen,int Miao){Shi=shi;Fen=fen;Miao=miao;}
        void display1()
        {
            cout<<shi<<":"<<fen<<":"<<miao<<endl;
        }
        private:
            int shi;
            int fen;
            int miao;
};

Class Week:public Date,public Time
{
    public:
        Week(int Nian,int Yue,int Ri,int Shi,int Fen,int Miao,string xq):
        Date(Nian,Yue,Ri),Time(Shi,Fen,Miao)
        void show()
        {
            cout<<Nian<<" "<<Yue<<" "<<Ri<<endl;
            cout<<Shi<<":"<<Fen<<":"<<Miao<<endl;
            cout<<xq<<endl;
        }
    private:
        string xq;
        };
int main()
{
    Week week(2015,11,11,22,43,50,"xqs");
    week.show();
    return 0;
}
代码出现如下错误:D:\Microsoft Visual Studio\Common\MSDev98\Bin\Cpp1.cpp(5) : error C2146: syntax error : missing ';' before identifier 'Date'
D:\Microsoft Visual Studio\Common\MSDev98\Bin\Cpp1.cpp(5) : error C2501: 'Class' : missing storage-class or type specifiers
D:\Microsoft Visual Studio\Common\MSDev98\Bin\Cpp1.cpp(5) : fatal error C1004: unexpected end of file found
执行 cl.exe 时出错.
求大牛指教!!!1
搜索更多相关主题的帖子: private display include public 
2015-11-11 23:08
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
得分:10 
程序代码:
#include<iostream>
#include<string>
using namespace std;
class Date
{
    public:
        Date(int Nian,int Yue,int Ri)
        {
            nian=Nian;
            yue=Yue;
            ri=Ri;
        }
        void display()
        {
        cout<<nian<<" "<<yue<<" "<<ri<<endl;
        }
        private:
            int nian;
            int yue;
            int ri;
friend class Week;
};

class Time
{
    public:
        Time(int Shi,int Fen,int Miao){shi=Shi;fen=Fen;miao=Miao;}
        void display1()
        {
            cout<<shi<<":"<<fen<<":"<<miao<<endl;
        }
        private:
            int shi;
            int fen;
            int miao;
        friend class Week;
};

class Week:public Date,public Time

{
    public:;
        Week(int Nian,int Yue,int Ri,int Shi,int Fen,int Miao,string xq_):
        Date(Nian,Yue,Ri),Time(Shi,Fen,Miao),xq(xq_){};
        void show()
        {
            cout<<nian<<" "<<yue<<" "<<ri<<endl;
            cout<<shi<<":"<<fen<<":"<<miao<<endl;
            cout<<xq<<endl;
        }
    private:
        string xq;
        };
int main()
{
    Week week(2015,11,11,22,43,50,"xqs");
    week.show();
    return 0;
}


你的程序问题太多了,首先是类声明就错了,class的c要小写,子类不能访问父类成员,想访问要声明为友元,还有你对对于给私有成员赋值的方法没弄清,都写反了

[此贴子已经被作者于2015-11-12 01:52编辑过]

2015-11-12 01:51
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:5 
你这不是C++,C++不是这样

程序代码:
#include <iostream>
#include <iomanip>

class Date
{
public:
    Date( int year, int month, int day ) : year_(year), month_(month), day_(day)
    {
    }
private:
    int year_, month_, day_;
    friend std::ostream& operator<<( std::ostream&, const Date& );
};
std::ostream& operator<<( std::ostream& os, const Date& date )
{
    std::ostream::char_type c = os.fill();
    os.fill( '0' );
    os << std::setw(4) << date.year_
       << '-' << std::setw(2) << date.month_
       << '-' << std::setw(2) << date.day_;
    os.fill( c );
    return os;
}

class Time
{
public:
    Time( int hour, int minute, int second ) : hour_(hour), minute_(minute), second_(second)
    {
    }
private:
    int hour_, minute_, second_;
    friend std::ostream& operator<<( std::ostream&, const Time& );
};
std::ostream& operator<<( std::ostream& os, const Time& time )
{
    std::ostream::char_type c = os.fill();
    os.fill( '0' );
    os << std::setw(2) << time.hour_
       << ':' << std::setw(2) << time.minute_
       << ':' << std::setw(2) << time.second_;
    os.fill( c );
    return os;
}

class Week : public Date, public Time
{
public:
    enum DayOfWeek{ Sun, Mon, Tue, Wed, Thu, Fri, Sat };
    Week( int year, int month, int day, int hour, int minute, int second, DayOfWeek dow ) : Date(year,month,day), Time(hour,minute,second), dayofweek_(dow)
    {
    }
private:
    DayOfWeek dayofweek_;
    friend std::ostream& operator<<( std::ostream&, const Week& );
};
std::ostream& operator<<( std::ostream& os, const Week& week )
{
    static const char* names_[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
    os << (const Date&)week << ' ' << (const Time&)week << ' ' << names_[week.dayofweek_];
    return os;
}

using namespace std;

int main( void )
{
    Week week( 2015,11,12, 9,1,30, Week::Thu );
    cout << week << endl;

    return 0;
}
输出
2015-11-12 09:01:30 Thu

2015-11-12 09:03
凉生泪Vin
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2015-11-4
得分:5 
看不懂,帮你顶上去
2015-11-14 20:30



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-459114-1-1.html




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

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