c++课程实验报告源代码

本文档详细记录了一次C++课程实验的过程,包含了完整的源代码和详细的注释,旨在帮助读者理解C++编程的基本概念和技术,包括类、对象、继承、多态等核心特性。

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

1.

#include <iostream>
using namespace std;
class TAdd
{
public:
    TAdd(int a,int b) 
{
        x=a;y=b;
        cout<<"constructor."<<endl;
    cout<<x<<","<<y<<endl;
}
~TAdd()
{
    cout<<"destructor."<<endl;
    cout<<x<<","<<y<<endl;
}
int add(){return x+y;}
private:
    int x,y;
};
int main()
{
TAdd p1(3,4);
cout<<"x+y="<<p1.add()<<endl;
TAdd p2(30,40); 	
cout<<"x+y="<<p2.add()<<endl;
    return 0;
}

2.

#include <iostream>
using namespace std;
class Date
{
public:
  Date()
  {
    cout<<"Input Date:";
    cin>>y>>m>>d;
  }
  int beLeapYear()
  {
    return ((y%4==0&&y%100!=0)||(y%400==0));
  }
  void addOneDay()
  {
            d++;
         if((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&(d==32))
            {
                d=1;
                m++;
                if(m>12)
                {
                    m=1;
                    y++;
                }
            }
            if(beLeapYear())
            {
                if(m==2&&d==28)
                {
                    d=1;
                    m++;
                }
            }
            if(!beLeapYear())
            {
                if(m==2&&d==29)
                {
                    d=1;
                    m++;
                }
            }
                else if((m==4)||(m==6)||(m==9)||(m==11)&&(d==31))
                {
                        m++;
                        d=1;
                }
  }
  void showDate()
  {
    cout<<y<<"-"<<m<<"-"<<d<<endl;
   }
private:
  int y,m,d;
  };
int main()
{
   Date d;
   d.showDate();
   d.addOneDay();
   d.showDate();
   return 0;
}

2-1


#include <iostream>
#include <string>
using namespace std;
class 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值