zoukankan      html  css  js  c++  java
  • C++ 面向对象学习1

    #include "stdafx.h"
    #include <iostream>  //不要遗漏  否则不能使用cout
    using namespace std;
    
    class Time{
    public :
        Time();// 自定义了构造函数  那么必须默认写一个空的构造函数
        Time(int h,int m ,int s);
        void setTime(int hour,int min,int sec);
        void printMilitary();
        void printStandard();
    private:
        int hour;
        int min;
        int sec;
    };
    
    Time::Time(){
    }
    
    Time::Time(int h, int m, int s){
        //hour=h;
        //min=m;
        //sec=s;
        setTime(h,m,s);
    }
    
    
    void Time::setTime(int h, int m, int s){//这里的形参不要写为hour 否则函数中无法区别参数和成员变量
        hour=h;
        min=m;
        sec=s;
    }
    
    void Time::printMilitary(){
        //std::cout<<(hour<10?"0":"")<<hour;
        //std::cout<<(min<10?"0":"")<<min;
        cout<<(hour<10?"0":"")<<hour<<":";
        cout<<(min<10?"0":"")<<min;
        cout<<endl;
    }
    
    void Time::printStandard(){
        //cout<<(hour%12)+" : "<<endl;//写法错误
        //cout<<hour%12<<":"<<endl;//ok
        cout<<((hour==0||hour==12)?12:hour%12)<<":";//三目运算符似乎要求两者类型一致?
        //cout<<((hour==0||hour==12)?"12":hour%12)<<":";//总之当其中一个是int 一个是字串就编译失败
        cout<<(min<10?"0":"")<<min;
        cout<<endl;
    
    
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Time t1(22,22,22);
        t1.printMilitary();
        t1.printStandard();
        t1.setTime(9,8,7);
        t1.printStandard();
    
    }

    C++里面一般不这么做  一般来说 C++将class的定义放在xxx.h文件中  类中函数的实现则放在同名的xxx.cpp中

    最后在另一个含有main函数的cpp文件中调用它

    Time.h

    #ifndef TIME_H
    #define TIME_H
    
    class Time{
    public :
        Time();// 自定义了构造函数  那么必须默认写一个空的构造函数
        Time(int h,int m ,int s);
        void setTime(int hour,int min,int sec);
        void printMilitary();
        void printStandard();
    private:
        int hour;
        int min;
        int sec;
    };
    
    #endif

     && #ifndef 后面的TIME_H 这个要和头文件命名一致  不是和类的命名一致  其中将头文件的xx.h中的.改为_

    最好都用大写

    Time.cpp

    #include <iostream> //引用标准库文件
    #include "Time.h"
    using namespace std;
    Time::Time(){
    }
    
    Time::Time(int h, int m, int s){
        setTime(h,m,s);
    }
    
    
    void Time::setTime(int h, int m, int s){//这里的形参不要写为hour 否则函数中无法区别参数和成员变量
        hour=(h>=0&&h<=24)?h:0;
        min=(m>=0&&m<=60)?m:0;
        sec=(s>=0&&s<=60)?s:0;
    }
    
    void Time::printMilitary(){
        cout<<(hour<10?"0":"")<<hour<<":";
        cout<<(min<10?"0":"")<<min;
        cout<<endl;
    }
    
    void Time::printStandard(){
        cout<<((hour==0||hour==12)?12:hour%12)<<":";//三目运算符似乎要求两者类型一致?    
        cout<<(min<10?"0":"")<<min;
        cout<<endl;
    }

    Test.cpp

    #include "stdafx.h"
    #include <iostream>  //不要遗漏  否则不能使用cout
    #include "Time.h"
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Time t1(22,22,22);
        t1.printMilitary();
        t1.printStandard();
        t1.setTime(99,89,7);
        t1.printStandard();
    
    }
  • 相关阅读:
    c++ 全局变量初始化的一点总结
    新的博客
    Git常用命令总结
    git 分支 branch 操作
    状态管理Vuex
    让 markdown 生成带目录的 html 文件
    mysql安装时,提示:Failed to start service MYSQL80
    【React 资料备份】React Hook
    【React 资料备份】React v16.3之后的生命周期
    两个数组对象取并集
  • 原文地址:https://www.cnblogs.com/cart55free99/p/3355931.html
Copyright © 2011-2022 走看看