#include <iostream>
using namespace std;
/********************************类的学习***********************************************/
///*
///
// * 类有6个默认函数
// ** /
/*
* 构造函数
*/
class Date
{
public:
/*
*构造函数定义了,系统自带的默认构造函数就会消失
*/
Date(){
}
Date(int y=0,int m=0,int d=0)
{
_year=y;
_month=m;
_day=d;
}
/*
* 拷贝构造
*
**/
Date(const Date &d){
_year=d._year;
_month=d._month;
_day=d._day;
}
/*
* 对象结束时,自动调用。
* 默认成员函数
* 用来释放对象
*
*/
~Date(){
cout<<"xigou"<<this<<endl;
}
void print()
{
cout<<_year<<" "<<_month<<" "<<_day<<endl;
}
bool operator==(const Date& d2){
return this->_year==d2._year
&& this->_month==d2._month
&& this->_day==d2._day;
}
bool operator>(const Date& d)
{
if(this->_year >=d._year)
{
if(this->_month >=d._month)
{
if(this->_day>d._day)
{
return true;
}
}
}
return false;
}
public:
int _year;
int _month;
int _day;
};
void test01()
{
Date d1();//1.默认 2.无参 3.全缺省 其中任意一个
Date d2(1,3,4);
}
/*
* 析构函数的学习
* 析构函数会自动清理成员变量
*/
class Stack
{
public:
Stack(int n =10)
{
cout<<"gouzhao"<<endl;
_a=(int *)malloc(sizeof (int)*n);
_size=0;
_len=n;
}
~Stack(){
cout<<"xigou"<<endl;
free(_a);
}
private:
int *_a;
int _size;
int _len;
};
void test02()
{
Stack s1;
}
/*******************************************************************************/
/*************************拷贝构造******************************************************/
/*
* 拷贝构造,但是调用函数时,要先传参,传参又是一个拷贝函数调用,必须用引用
*
*/
bool IsDateEqual(const Date& d1,const Date& d2)
{
//不用&就得拷贝构造,会浪费资源
}
void test03(){
Date d1(1,3,4);
Date d2(d1);//调用的拷贝构造
Date d3=d1;
d1.print();
d2.print();
d3.print();
}
bool operator==(const Date& d1,const Date& d2)
{
return d1._year == d2._year && d1._month==d2._month&&d1._day==d2._day;
}
void func()
{
Date d1(1,3,4);
Date d2(d1);//调用的拷贝构造
Date d3=d1;
cout<<(d1==d2)<<endl;//一般不这样写
cout<<(operator==(d1,d2))<<endl;
}
/*******************************************************************************/
/**************************运算符的重载*****************************************************/
/*运算符的重载是为了好的完成需要
* 可以类内与类外。为了增强函数的可读性
* 1.不能自己创建新的
* 2,必须有一个类的
* 3,用于内置函数的操作符,含义不能更改
* 4,带.基本都不可以重载
* @brief test04
*/
void test04()
{
Date d1(3,3,2);
Date d2(3,3,2);
cout<<(d2>d1)<<endl;
}
/*******************************************************************************/
/****************************实现一个完善的日期类***************************************************/
class Date01{
private:
int _year,_month,_day;
public:
int getMonthDay(int year,int month)
{
int monthDays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(month==2 && ((year%4==0&&year %100!=0)||year%400==0))
{
monthDays[2]=29;
}
return monthDays[month];
}
Date01(int year=0,int month=1,int day=1)
{
if(year >=0
&& month >=1
&& day>=1){
_year=year;
_month=month;
_day=day;
}else
{
cout<<"非法日期"<<endl;
}
}
void Pint()
{
cout<<_year<<" "<<_month<<" "<<_day<<endl;
}
};
void test05(){
Date01 d1;
d1.Pint();
Date01 d2(0,0,1);
d2.Pint();
}
/*******************************************************************************/
int main()
{
test05();
return 0;
}
c++学习笔记04
最新推荐文章于 2024-07-24 20:55:30 发布