C++学习之类

C++提供了一种类class机制来定义新的数据类型,

类不仅可以定义了数据的复合,还可以定义该复合数据的操作。

一、类的定义

类机制定义的类class,是一种类型,其定义与struct定义类似,只是在定义体中添加了操作的方法,

如此是一段定义类以及对该类进行操作的代码:

#include <iostream>
#include <iomanip>

using namespace std;

class Date
{
	int year, month, day;

public:
	void set(int y, int m, int d);
	bool isLeapYear();
	void print();
};

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

bool Date::isLeapYear()
{
	return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

void Date::print()
{
	cout << setfill('0');
	cout << setw(4) << year << '-' << setw(2) << month << '-' << setw(2) << day << "\n";
	cout << setfill(' ');
}

int main()
{
	Date d;
	d.set(2012, 8, 19);
	if(d.isLeapYear())
		d.print();

	return 0;
}

二、类的成员函数

在定义类时,类的成员函数,可以再类定义体外定义,也可以在类定义体类定义。在类定义中包含的成员函数,就默认有内联的性质。

但是对于成员函数是否安排为内联的成员函数,要看函数是否足够简单,对于不符合内联条件的成员函数,调用开销还是不会免去的。所以,应尽量将成员函数的定义写到类定义的外部去,对于短小的、不超过三行的成员函数定义放在类定义中是适合的。

三、类的访问

一种是通过点号“.”访问,如下所示:

className class_object;

class_object.member;

还有一种是通过“->”访问,该方式主要是用于对对象指针进行访问,如下所示:

className *class_object;

class_object->member;

如下是一段实例代码:

int main()
{
	Date d1;
	d1.set(2012, 8, 19);
	if(d1.isLeapYear())
		d1.print();

	Date *d2 = new Date;;
	d2->set(2012, 8, 20);
	if(d2->isLeapYear())
		d2->print();

	return 0;
}


四、常成员函数

对于类定义中的一些成员函数,如果只对对象进行读操作,则该成员函数可以设计为常(const)成员函数。设计为常成员函数的好处是,让使用者一目了然地知道该成员函数不会改变对象值。

对于成员函数来,能够成为常成员函数的,应尽量写成常成员函数的形式。

常函数函数的声明和定义在形式上必须是一致的。即在函数形参列表的右括号后面加上const。

#include <iostream>
#include <iomanip>

using namespace std;

class Date
{
	int year, month, day;

public:
	void set(int y, int m, int d);
	bool isLeapYear()const;
	void print()const;
};

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

bool Date::isLeapYear()const
{
	return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

void Date::print()const
{
	cout << setfill('0');
	cout << setw(4) << year << '-' << setw(2) << month << '-' << setw(2) << day << "\n";
	cout << setfill(' ');
}

int main()
{
	Date d1;
	d1.set(2012, 8, 19);
	if(d1.isLeapYear())
		d1.print();

	Date *d2 = new Date;;
	d2->set(2012, 8, 20);
	if(d2->isLeapYear())
		d2->print();

	return 0;
}


五、重载成员函数

成员函数与普通函数一样,可以重载,对重载的识别和使用规则与普通函数也是相同。

成员函数重载定义如下:

指函数名相同,但是它的参数表列个数,顺序或类型不同,但是不能靠返回类型来判断。

1)相同的范围(在同一个作用域中)

2)函数名相同

3)参数不同

4)virtual关键字可有可无

5)返回类型可以不同

如下是重载函数的实例代码:

#include <iostream>
#include <iomanip>

using namespace std;

class Date
{
	int year, month, day;

public:
	void set(int y, int m, int d);
	void set(string& s);
	bool isLeapYear()const;
	void print()const;
};

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

void Date::set(string& s)
{
	year = atoi(s.substr(0, 4).c_str());
	month = atoi(s.substr(5, 1).c_str());
	day = atoi(s.substr(7, 2).c_str());
}
bool Date::isLeapYear()const
{
	return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

void Date::print()const
{
	cout << setfill('0');
	cout << setw(4) << year << '-' << setw(2) << month << '-' << setw(2) << day << "\n";
	cout << setfill(' ');
}

int main()
{
	Date d1;
	d1.set(2012, 8, 19);
	if(d1.isLeapYear())
		d1.print();

	Date *d2 = new Date;;
	d2->set(2012, 8, 20);
	if(d2->isLeapYear())
		d2->print();

	Date d3;
	string s = "2012-8-21";
	d3.set(s);
	if(d3.isLeapYear())
		d3.print();

	return 0;
}


学习自《C++程序设计教程》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值