美文网首页
C艹之路 1.3b--

C艹之路 1.3b--

作者: 农家小升 | 来源:发表于2020-01-08 19:36 被阅读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;
}

相关文章

  • C艹之路 1.3b--

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

  • C艹之路总览

    提醒:目前还没有完成,所以请不用看本文章,因为谁也不知道,会不会太监了进度:目前完成了V1.1,V1.3,基本完成...

  • C艹之路 1.3c--

    3.5 例子3.1p70 例子3.2p72 例子3.3p74 例子3.4p 例子3.5p79 例子3.6p 例子3...

  • C艹之路 1.3a--

    例子1.1p2 //输出一行字符 例子1.2p3 // 例子1.3p4 例子1.4p5 例子1.5p 例子1.5p...

  • C艹之路 1.3f--

    例子6.1p204 例子6.2p204

  • C艹之路 1.3de--

    例子5.1p160 例子5.2p160 例子5.3p167 例子5.4p169 例子5.5p171 例子5.6p1...

  • C艹之路 V1.1 知识补习

    V1.1主要以谭浩强的书籍为主 简单知识 与C语言(所有定义必须放在函数体最前面)相比,随用随定义namespac...

  • C艹之路 V1 C++基础语法复习

    正在快速熟悉语法中... 因为需要恢复手感,所以干脆就拿着谭浩强书籍开始第一步,然后在快速进入状态.等语法差不多了...

  • C艹之路 1.1a--引用的目的,注意

    目的 联想swap()函数机会明白了[p20]使用引用时,在swap()的参数直接设为引用参数就可以了 注意 可以...

  • C艹之路 1.1b--类的知识

    类是抽象的,不占据内存空间,对象是具体的,占用存储空间 struct也可以声明类但是默认都是公有地private,...

网友评论

      本文标题:C艹之路 1.3b--

      本文链接:https://www.haomeiwen.com/subject/qkosactx.html