1. 类的基础知识
查看菜鸟教程:http://www.runoob.com/cplusplus/cpp-classes-objects.html
2. 总结
c++类有6个默认成员函数:构造函数,拷贝构造函数,析构函数,赋值操作符重载。取地址操作符重载,const修饰的取地址操作符重载。其中前四个默认成员函数是我们重点研究对象。下面以时间类为例子讲解类默认成员函数。
- 构造函数
1.函数名与类名相同。
2.无返回值
3.对象实例化时系统自动调用对应的构造函数。
4.构造函数可以重载。
5.构造函数可以在类中定义也可以在类外定义。
6.如果类定义中没有给出构造函数,则c++编译器会默认生成一个缺省的构造函数,但只要我们定义了一个构造函数,系统就不会自动生成缺省的构造函数。
7.无参的构造函数和全缺省值的构造函数都认为是缺省的构造函数,并且缺省的构造函数只能有一个
#include <iostream>
using namespace std;
class Date
{
public:
Date(); //无参构造函数
Date(int Year,int Month,int Day); //有参构造函数
int GetMonthDay(int Year,int Month);//获取当年当月的天数
void ShowDate(); //展示日期
private:
int m_nYear;
int m_nMonth;
int m_nDay;
};
Date::Date()
{
m_nYear=1976;
m_nMonth=1;
m_nDay=1;
}
Date::Date(int Year,int Month,int Day)
{
if (Year<1976||Month<1||Month>12||Day<1||Day>GetMonthDay(Year,Month))
{
cout<<"非法日期,请重新输入"<<endl;
return;
}
m_nYear=Year;
m_nMonth=Month;
m_nDay=Day;
}
int Date::GetMonthDay(int Year,int Month)
{
static int MonthDay[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int day=MonthDay[Month];
if (Month==2&&((Year%400==0)||((Year%4==0)&&(Year%100!=0))))
{
day+=1;
}
return day;
}
void Date::ShowDate()
{
if (m_nYear<1976||m_nMonth<1||m_nMonth>12||m_nDay<1||m_nDay>GetMonthDay(m_nYear,m_nMonth))
{
cout<<"未输入日期,请先输入日期"<<endl;
return;
}
cout<<"日期是:"<<m_nYear<<":"<<m_nMonth<<":"<<m_nDay<<endl;
}
void main()
{
Date dtDate; //调用无参构造函数
dtDate.ShowDate();
Date dtDate1(2018,2,30); //调用有参构造函数
dtDate1.ShowDate();
getchar();
}
构造函数一般情况下定义为全缺省构造函数
Date(int Year=1976,int Month=1,int Day=1)
{
if (Year<1976||Month<1||Month>12||Day<1||Day>GetMonthDay(Year,Month))
{
cout<<"非法日期,请重新输入"<<endl;
return;
}
m_nYear=Year;
m_nMonth=Month;
m_nDay=Day;
}
void main()
{
Date dtDate;
dtDate.ShowDate(); //日期是:1976:1:1
Date dtDate1(2018);
dtDate1.ShowDate(); //日期是:2018:1:1
Date dtDate2(2018,9);
dtDate2.ShowDate(); //日期是:2018:9:1
Date dtDate3(2018,9,26);
dtDate3.ShowDate(); //日期是:2018:9:26
getchar();
}
- 拷贝构造函数
1.拷贝构造函数其实是一个构造函数的重载。
2.拷贝构造函数的参数必须使用引用传参,使用传值方式会引发无穷递归。
3.若未显示定义,系统会默认生成一个缺省的拷贝构造函数。缺省的拷贝构造函数会依次拷贝类成员及进行初始化。
我们这里使用日期类的例子来讲解类的默认成员函数,日期类可以不用自己定义拷贝构造函数,但是有的类就必须自己实现一个拷贝构造函数,比如顺序表。顺序表系统自动生成的用不了,因为会产生内存泄漏,这其实是一个浅拷贝的问题。
Date(const Date &dtDate)
{
m_nYear=dtDate.m_nYear;
m_nMonth=dtDate.m_nMonth;
m_nDay=dtDate.m_nDay;
}
void main()
{
Date dtDate(2018,9,26);
dtDate.ShowDate(); //日期是:2018:9:26
Date dtDate1(dtDate);
dtDate1.ShowDate(); //日期是:2018:9:26
getchar();
}
- 析构函数
1.析构函数在类名前面加~。
2.析构函数无参数无返回值
3.一个类有且只有一个析构函数,若未显示定义,系统会自动生成缺省的析构函数。
4.对象生命周期结束时,c++编译器会自动调用析构函数。
5.注意析构函数函数体内并不是删除对象,而是做一些清理工作。这里的日期类可以不清理,但有的类必须及逆行清理,比如顺序表vector,不然会内存泄露,清不清理主要看有没有动态的资源。
~Date() //析构函数释放
{
}
- 运算符重载
1.重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。
2.重载运算符以后,不能改变运算符的优先级/结合性/操作数个数。
2.考点:5个c++不能重载的运算符:* :: sizeof ?: .
3.赋值运算符重载 拷贝构造函数是创建的对象,使用一个已有对象来初始化这个准备创建的对象。赋值运算符的重载是对一个已存在的对象进行拷贝赋值。
Date & operator=(const Date &d) //赋值运算符重载
{
this->m_nYear=d.m_nYear;
this->m_nMonth=d.m_nMonth;
this->m_nDay=d.m_nDay;
return *this;
}
更多运算符重载查看菜鸟教程:http://www.runoob.com/cplusplus/cpp-overloading.html
3. 易错难点
- class与struct区别
1.C语言中struct只能是一些变量的集合体,可以封装数据却不可以隐藏数据,而且成员也不可以是函数。
C++的struct可以当做class来用,他和C++中class唯一区别是,class中成员默认是private,而struct的成员默认为public。
2.一般情况下,使用class来定义类,而使用struct只表示数据对象没有成员函数。