C艹之路 1.3b--

2020-01-08  本文已影响0人  农家小升

目录

例子2.1;用类来实现输入和输出时间(时:分:秒)//;这里面没有函数//;author


例子2.1p54

//;用类来实现输入和输出时间(时:分:秒)//;这里面没有函数//;author
#include<iostream>
using namespace std;

class Time
{
//private:
public:

    int hour;
    int minute;
    int sec;
};
int main()
{
    Time t1;
    cin >> t1.hour;
    cin >> t1.minute;
    cin >> t1.sec;
    cout << t1.hour
        << ":"
        << t1.minute
        << ":"
        << t1.sec
        << endl;
    return 0;
}

例子2.2ap55

//;用类来实现输入和输出时间(时:分:秒)//;在这里再加入一个对象//;author
#include<iostream>
using namespace std;

class Time
{
//private:
public:
    int hour;
    int minute;
    int sec;
};
int main()
{
    Time t1;
    Time t2;

    cin >> t1.hour;
    cin >> t1.minute;
    cin >> t1.sec;
    cout << t1.hour
        << ":"
        << t1.minute
        << ":"
        << t1.sec
        << endl;

    cin >> t2.hour;
    cin >> t2.minute;
    cin >> t2.sec;
    cout << t2.hour
        << ":"
        << t2.minute
        << ":"
        << t2.sec
        << endl;
    return 0;
}

例子2.2bp56

//;用类来实现输入和输出时间(时:分:秒)//;在这里再加入一个对象//;author(这里没有彻底掌握形参用法)
#include<iostream>
using namespace std;

class Time
{
//private:
public:
    int hour;
    int minute;
    int sec;
};
int main()
{
    void setTime(Time &);
    void showTime(Time &);
    Time t1;
    Time t2;
    setTime(t1);
    showTime(t1);
    setTime(t2);
    showTime(t2);
    return 0;
}
void setTime(Time &t)
{
    cin >> t.hour >> t.minute >> t.sec;
}
void showTime(Time &t)
{
    cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}

例子2.2cp57

//;用类来实现输入和输出时间(时:分:秒)//;这里使用默认参数,这里不用输入值//;author(这里没有彻底掌握形参用法)
#include<iostream>
using namespace std;

class Time
{
//private:
public:
    int hour;
    int minute;
    int sec;
};
int main()
{
    void setTime(Time &, int hour = 0, int minute = 0,int sec=0);
    void showTime(Time &);
    Time t1;
    Time t2;
    setTime(t1,10,32,43);
    showTime(t1);
    setTime(t2);
    showTime(t2);
    return 0;
}
void setTime(Time &t,int hour,int minute,int sec)//定义函数可以不用指定默认参数
{
    t.hour = hour;
    t.minute = minute;
    t.sec = sec;
}
void showTime(Time &t)
{
    cout << t.hour << ":" << t.minute << ":" << t.sec << endl;
}

例子2.3p58

//;用类来实现输入和输出时间(时:分:秒)//;这里使用成员函数(同时类外定义)//;author(圆满)
#include<iostream>
using namespace std;

class Time
{
public:
    void setTime();
    void showTime();
private:
    int hour;
    int minute;
    int sec;
};

int main()
{
    Time t1;
    t1.setTime();
    t1.showTime();
    Time t2;
    t2.setTime();
    t2.showTime();

    return 0;
}
void Time::setTime()
{
    cin >> hour >> minute >> sec;
}
void Time::showTime()
{
    cout << hour << ":" << minute << ":" << sec << endl;
}

例子2.4p60

//找出一个整型数组中的元素的最大值//有问题
#include<iostream>
using namespace std;

class ArrayMax
{
public:
    void setValue();
    void maxValue();
    void showValue();
private:
    int array[10];
    int max;
};
void ArrayMax::setValue()
{
    int i;
    for (i = 0; i < 10; i++)
        cin >> array[10];
}
void ArrayMax::maxValue()
{
    int i;
    max = array[0];
    for (i = 0; i < 10; i++)
    {
        if (array[i] > max)max = array[i];
    }
}
void ArrayMax::showValue()
{
    cout << "max=" << max << endl;

}
int main()
{
    ArrayMax arrmax;
    arrmax.setValue();
    arrmax.maxValue();
    arrmax.showValue();

    return 0;
}
上一篇 下一篇

猜你喜欢

热点阅读