c++ 类 helloworld

本文介绍了一个简单的C++日期类的实现,包括设置日期、判断闰年和打印日期等功能,并通过主函数展示如何使用这个类进行基本的日期操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

a.h

复制代码
#ifndef A_H_
#define A_H_
#include <iostream>

class Tdate{
public:
  void Set(int,int,int);
  int IsLeapYear();
  void Print();
private:
  int month;
  int day;
  int year;
};

#endif 
复制代码

  

a.cc

复制代码
#include <iostream>      
#include "a.h"                          
using namespace std;

void Tdate::Set(int m,int d,int y)
{
  month=m;  day=d;  year=y; 
}

int Tdate::IsLeapYear()
{
  return (year%4==0&&year%100!=0)||(year%400==0);
}

void Tdate::Print()
{
   cout <<month <<"/" <<day <<"/" <<year <<endl;
}
复制代码

  

main.cc

复制代码
#include        <cstdlib>
#include        <fstream>
#include        <iomanip>   
#include        <iostream>  
#include        "a.h"

using namespace std;
                                                                                                                                                                                 
        int
main ( int argc, char *argv[] )
{
    Tdate s;
    s.Set(2,15,1988);
    s.Print();
        cout    << "\nProgram " << argv[0] << endl << endl;

        return EXIT_SUCCESS;      
}               // ----------  end of function main  ---------- 
复制代码

  

编译:Makefile

all:main                                                                                                                                                                         
main:main.cc a.cc
        g++ -g -Wall -O0 main.cc a.cc -o main

  

*********output*******
2/15/1988

Program ./main

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值