第七周第二项目——友元类

/*Copyright (c)2016,烟台大学计算机与控制工程学院
02.*All rights reserved.
03.*文件名称:main.cpp
04.*作    者:田志伟
05.*完成日期:2016年5月9日
06.*版 本 号:v1.0
07.*
08.*问题描述: 友元时间类
09. 输入描述:
10.*输出描述: 年月日小时分钟秒
*/
#include <iostream>
using namespace std;
class Date; //对Date类的提前引用声明
class Time
{
public:
    Time(int,int,int);
    void add_a_second(Date &);  //增加1秒,1秒后可能会到了下一天,乃到下一月、下一年
    void display(Date &);  //显示时间,格式:月/日/年 时:分:秒
private:
    int hour;
    int minute;
    int sec;
};

class Date
{
public:
    Date(int,int,int);
    friend class Time; //Time为Date的友元类
private:
    int month;
    int day;
    int year;
};
Time::Time(int a,int b,int c)
{
    hour=a;
    minute=b;
    sec=c;
}
Date::Date(int x,int y,int z)
{
    month=x;
    day=y;
    year=z;
};
void Time::add_a_second(Date&d1)
{
    sec=sec+1;

}
void Time::display(Date&d1)
{

    if(sec>=60)
    {
        minute=sec/60+minute;
        sec=sec%60;

    }
    if(minute>=60)
    {
        hour=minute/60+hour;
        minute=minute%60;

    }
    if(hour>=24)
    {
        d1.day=d1.day+hour/24;
        hour=hour%24;


    }

    if(d1.day>31)
    {
        d1.month=d1.day/30+d1.month;
        d1.day=d1.day%30;

    }
    if(d1.month>12)
    {
        d1.year=d1.month/12+d1.year;
        d1.month=d1.month%12;

    }

    cout<<d1.month<<"/"<<d1.day<<"/"<<d1.year<<"  "<<hour<<":"<<minute<<":"<<sec<<endl;
}
int main( )
{
    Time t1(23,59,32);
    Date d1(2,28,2013);   //测试时,再试试Date d1(2,28,2013)会如何
    for(int i=0; i<=100; i++)
    {
        t1.add_a_second(d1);
        t1.display(d1);
    }
    return 0;
}

程序运行:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
保护继承是指子类继承父类的成员,但是只有类内和友元可以访问这些成员,对于类外部的其他代码是不可见的。这样可以保护父类的实现细节,同时也可以在子类中使用这些成员。 下面以学生信息类为例,演示如何使用保护继承。 ```c++ #include <iostream> #include <string> using namespace std; // 父类:人类 class Person { public: Person(string name, int age) : m_name(name), m_age(age) {} void showInfo() { cout << "姓名:" << m_name << endl; cout << "年龄:" << m_age << endl; } protected: // 保护成员 string m_name; // 姓名 int m_age; // 年龄 }; // 子类:学生类 class Student : protected Person { public: Student(string name, int age, int score) : Person(name, age), m_score(score) {} void showInfo() { Person::showInfo(); // 调用父类的 showInfo 函数 cout << "成绩:" << m_score << endl; } private: int m_score; // 成绩 }; int main() { Student s("小明", 18, 90); s.showInfo(); // 调用子类的 showInfo 函数 return 0; } ``` 在上面的例子中,父类 `Person` 中的成员 `m_name` 和 `m_age` 被声明为保护成员,子类 `Student` 继承了这两个成员。在子类中,我们使用 `protected` 访问修饰符将父类的成员变量和成员函数设置为保护成员,这样子类就可以访问这些成员了。 在子类中,我们重写了 `showInfo` 函数,并在其中调用了父类的 `showInfo` 函数,然后输出了子类新增的成员 `m_score`。最后在 `main` 函数中,我们创建了一个 `Student` 对象,并调用了 `showInfo` 函数,输出了学生的信息。 在这个例子中,我们使用保护继承来访问父类的成员,这样可以保护父类的实现细节,同时也可以在子类中使用这些成员。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值