类
1. 使用对象指针
调用成员函数的形式:
objectName.memberFunctionName(parameters);
如果是对象指针方式,则用 ->
objectName->memberFunctionName(parameters);
或
(*objectName).memberFunctionName(parameters);
2. 常成员函数
如果只对对象只读操作, 函数后面加上const
#include<iostream>
#include<iomanip>
using namespace std;
class Data
{
int year,month,day;
public:
void set(int y,int m ,int d);
bool isLeapYear() const;
void print() const;
};
inline void Data::set(int y,int m,int d)
{
year =y; month = m; day = d;
}
inline bool Data::isLeapYear() const
{
return (year%4 ==0 && year %100 !=0) || (year%400 ==0);
}
inline void Data::print() const {
cout<<setfill('0');
cout<<setw(4)<<year<<'-'<<setw(2)<<month<<'-'<<setw(2)<<day<<'\n';
cout<<setfill(' ');
}
void main()
{
Data* da = new Data;
da->set(2000,1,1);
if(da->isLeapYear())
da->print();
}
3. 重载成员函数
#include<iostream>
#include<iomanip>
using namespace std;
class Data
{
int year,month,day;
public:
void set(int y,int m ,int d);
void set(string& s);
bool isLeapYear() const;
void print() const;
};
inline void Data::set(int y,int m,int d)
{
year =y; month = m; day = d;
}
inline void Data::set(string& s)
{
year = atoi(s.substr(0,4).c_str());
month = atoi(s.substr(5,2).c_str());
day = atoi(s.substr(8,2).c_str());
}
inline bool Data::isLeapYear() const
{
return (year%4 ==0 && year %100 !=0) || (year%400 ==0);
}
inline void Data::print() const {
cout<<setfill('0');
cout<<setw(4)<<year<<'-'<<setw(2)<<month<<'-'<<setw(2)<<day<<'\n';
cout<<setfill(' ');
}
void main()
{
string s = "2000-01-01";
Data* da = new Data;
da->set(s);
if(da->isLeapYear())
da->print();
}
4. 操作符重载
#include<iostream>
using namespace std;
class Point
{
int x,y;
public:
void set(int a, int b) { x = a, y = b;}
void print() const { cout<<"("<<x<<","<<y<<")\n";}
friend Point operator+(const Point& a, const Point& b);
friend Point add(const Point& a ,const Point& b);
};
Point operator+(const Point& a, const Point& b)
{
Point s;
s.set(a.x + b.x, a.y +b.y);
return s;
}
Point add(const Point& a,const Point& b)
{
Point s;
s.set(a.x + b.x, a.y+b.y);
return s;
}
int main()
{
Point a, b;
a.set(3,2);
b.set(1,5);
(a+b).print();
operator+(a,b).print();
add(a,b).print();
}
因为需要对Point对象的私有数据访问,所以成员函数需要加上friend.
5.增量操作符
X& operator++(X& a) ;//前增量操作符
++a; //等于 operator++(a);
前增量操作数与返回值是同一变量,要求参数为对象的引用, 同时返回的仍然是对象参数的引用。
X operator(X& a, int b) //后增量操作符
a++ //等于 operator++(a,1);
后增量操作符重载,同样要求参数为对象的引用,因为在调用的上下文中,实参将发生变化,而返回值为临时变量,所以为非
引用的对象值,虽然前后增量操作符为不同函数,但因为两个操作符的参数相同,所以C++做了处理.
#include<iostream>
#include<iomanip>
using namespace std;
class Time
{
int hour, minute, second;
public:
void set(int h, int m,int s) {hour = h, minute = m, second = s;}
friend Time& operator++(Time& a);
friend Time operator++(Time& a,int );
friend ostream& operator<<(ostream& o, const Time& t);
};
Time& operator++(Time& a)
{
if(!(a.second=(a.second+1)%60) && !(a.minute = (a.minute +1)%60))
a.hour = (a.hour + 1)%24;
return a;
}
Time operator++(Time& a, int)
{
Time t(a);
if(!(a.second = (a.second+1)%60) && !(a.minute = (a.minute +1)%60))
a.hour = (a.hour +1)% 24;
return t;
}
ostream& operator<<(ostream& o, const Time& t)
{
o<<setfill('0')<<setw(2)<<t.hour<<":"<<setw(2)<<t.minute<<":";
return o<<setw(2)<<t.second<<"\n"<<setfill(' ');
}
int main()
{
Time t;
t.set(11,59,58);
cout<<t++;
cout<<++t;
}