zoukankan      html  css  js  c++  java
  • myDate类的对象作为student类的私有变量

    源程序:

    #include <iostream>
    #include <string>
    using namespace std;

    class myDate
    {
    private:
    int year,month,day;
    public:
    myDate()
    {
    year=1970;
    month=1;
    day=1;
    }
    myDate(int y,int m,int d)
    {
    year=y;
    month=m;
    day=d;
    }
    void setDate(int y,int m,int d)
    {
    year=y;
    month=m;
    day=d;
    }
    void setDate(myDate oneD) //oneD是一个对象
    {
    day=oneD.day;
    }
    myDate getDate()
    year=oneD.year;
    month=oneD.month;
    {
    return *this;
    }
    void setYear(int y)
    {
    year=y;
    }
    int getMonth()
    {
    return month;
    }
    inline void myDate::printDate() const
    {
    cout<<year<<month<<day;
    return;
    }
    }; //类结束


    class student
    {
    private:
    string name;
    myDate birthday;
    public:


    void setStudent(string s,myDate d)
    {
    name=s;
    birthday.setDate(d);
    return;
    }

    void setName(string n)
    {
    name=n;
    return;
    }
    string getName()
    {
    return name;
    }
    void setBirthday(myDate d)
    {
    birthday.setDate(d);
    return;
    }
    myDate getBirthday()
    {
    return birthday;
    }
    void printStudent() const
    {
    cout<<name;
    birthday.printDate();
    cout<<endl;
    }

    };


    int main()
    {
    myDate D;
    student S;
    S.setStudent("zhangsan",D);
    S.printStudent();
    return 0;
    }

  • 相关阅读:
    创新工场 笔试1
    C++的异常处理
    阿里笔试题2
    阿里笔试题1
    控制CPU占用率曲线
    再探结构体字节对齐问题
    1401
    pancake sort的几个问题
    CF198 D2
    谨慎设计一个单例类
  • 原文地址:https://www.cnblogs.com/duanqibo/p/15497172.html
Copyright © 2011-2022 走看看