<file_sep> ///
 /// @file    constructor.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 18:18:52
 ///
 
#include <iostream>
using std :: cout;
using std :: cin;
using std :: endl;

class point
{
public:
	point ( int x, int y )  //有参构造函数
	{
		cout << "对象创建时构造函数被自动调用" << endl;
		_xPos = x;
		_yPos = y;
	}
	void print ()
	{
		cout << "xPos: " << _xPos << ", yPos: " << _yPos << endl;
	}
private:  //私有成员
	int _xPos;
	int _yPos;
};

int main()
{
	// point pt0 ();
	// pt0.print ();
	point pt1 (3,4);  //调用有参构造函数声明point类变量（类对象）pt1
	pt1.print ();
	return 0;
}
<file_sep> ///
 /// @file    constructor_default_parameter.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 22:21:31
 ///
 
#include <iostream>
using std :: cout;
using std :: cin;
using std :: endl;

class point
{
	public:
		point (int x = 0, int y = 0) //有参构造函数
		{
			cout << "对象创建时构造函数自动调用" << endl;
			_xPos = x;
			_yPos = y;
		}
		void print ()
		{
			cout << "xPos: " << _xPos << ",yPos: " << _yPos << endl;
		}
	private:
		int _xPos;
		int _yPos;
};

int main()
{
	point pt0;
	pt0.print ();
	point pt1 (3, 4);
	pt1.print ();
	return 0;
}

<file_sep> ///
 /// @file    initialize_expression.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 22:39:20
 ///
 
#include <iostream>

using std :: cin;
using std :: cout;
using std :: endl;

class point
{
	public:
		point(int x)   //初始化表取决于成员声明的顺序
		:_xPos (x)
		 ,_yPos (_xPos)  
		{
		}
		void print ()
		{
			cout << "_xPos: " << _xPos << ",_yPos: " << _yPos << endl;
		}
	private:
		int _yPos;  //先定义
		int _xPos;  //后定义
	//	int _xPos;
	//	int _yPos;
};

int main()
{
	point pt1 (3);
	pt1.print ();
	return 0;
}
<file_sep> ///
 /// @file    learn_class1.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 09:09:03
 ///

//在类定义的外部定义成员函数

class computer //类定义，起到接口作用
{
public:
	//3个public成员函数的原型声明
	void print();
	void SetBrand(const char *sz);
	void SetPrice(float pr);
private:
	char brand [20];
	float price;
};

#include <iostream>
#include <cstring>
using namespace std;

void computer :: print()  //成员函数的实现，注意作用域限定符的使用
{
	cout << "品牌：" << brand << endl;
	cout << "价格：" << price << endl;
}

void computer :: SetBrand(const char *sz)
{
	strcpy (brand,sz);  //字符串复制
}

void computer :: SetPrice (float pr)
{	price = pr;
}

int main()
{
	computer com1;
	com1.SetPrice (5000);
	com1.SetBrand ("Lenovo");
	com1.print();  
	return 0;
}


<file_sep> ///
 /// @file    exercise1.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 17:33:18
 ///

//创建文件student.cpp，并在文件中定义SetInfo和Display成员函数的实现

#include <iostream>
#include <cstring>
using std :: cout;
using std :: cin;
using std :: endl;
//using namespace std;
class CStudent
{
public:
	void SetInfo (int id,const char * pszName);  //将学生ID和姓名赋值给成员变量
	void Display ();  //打印出成员变量
private:
	int _m_ild;
	char _m_szName [32];
};

void CStudent :: SetInfo (int ild, const char * pszName)
{
	_m_ild = ild;
	strcpy (_m_szName, pszName);
}

void CStudent :: Display ()
{
	cout << "the student named" << _m_szName 
		<< "s id is" << _m_ild << endl;
}

//最后在main.cc中使用该类
int main()
{
	//栈中实现
	CStudent stud;
	stud.SetInfo (3, "Tom" );
	stud.Display ();

	//堆中实现
	CStudent * pStud = new CStudent;
	pStud -> SetInfo (0, "Bill" );
	pStud -> Display();

	(* pStud).Display ();

	delete pStud;  //释放申请的空间
	pStud = NULL;  //指针赋空值
	
//	return 0;
}
<file_sep> ///
 /// @file    computer_class.h
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 08:53:04
 ///
 
class computer
{
	char brand [20];  //默认为private类型
public：              //这里的private不能省略，因为不是在类定义的开始位置
	void print();  
private:
	float price;
public:
	void SetBrand (char * sz);
	void SetBrand (float pr);
};

<file_sep> ///
 /// @file    object_declaration.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 17:14:36
 ///


class computer
{
public:
	void print ();
	void SetBrand (const char * sz );
	void SetPrice (float pr );
private:
	char _brand [20];
	float _price;
};  //不能忘记;



#include <iostream>
#include <cstring>

using namespace std;

void computer :: print()
{
	cout << "品牌：" << _brand << endl;
	cout << "价格：" << _price << endl;
}

void computer :: SetBrand (const char * sz)
{
	strcpy(_brand, sz);
}

void computer :: SetPrice (float pr)
{
	_price = pr;
}

int main()
{
	computer com1;
	com1.SetBrand ("lenovo");
	com1.SetPrice (8000);
	com1.print ();
	return 0;
}

<file_sep> ///
 /// @file    overloaded_constructor.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 18:33:01
 ///
 
#include <iostream>

using std :: cin;
using std :: cout;
using std :: endl;

class point 
{
	public:
		point ( int x, int y )  //有参构造函数
		{
			cout << "有参构造函数的调用" << endl;
			_xPos = x;
			_yPos = y;
		}
		point ()
		{
			cout << "无参构造函数的调用" << endl;
			_xPos = 0;
			_yPos = 0;
		}
		void print ()
		{
			cout << "xPos: " << _xPos << ",yPos: " << _yPos << endl;
		}
	private:
		int _xPos;
		int _yPos;
};

int main()
{
	point pt1 (3,4);
	pt1.print ();
	point pt2;
	pt2.print ();
	return 0;
		
}
<file_sep> ///
 /// @file    learn_class.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 08:47:48
 ///


//在类定义时定义成员函数

#include <iostream>
#include <cstring>
using namespace std;

class computer
{
public:
	//在类定义的同时实现了3个成员函数
	void print()
	{
		cout << "品牌：" << brand << endl;
		cout << "价格：" << price << endl;
	}
	void SetBrand(const char * sz)
	{
		strcpy(brand,sz);  //字符串复制
	}
	void SetPrice(float pr)
	{
		price = pr;
	}
private:
	char brand [20];
	float price;
};

//#include "computer_class.h"  //包含了computer类的定义
int main()
{
	computer com1;  //声明创建一个类对象
	com1.SetPrice (5000);  
	com1.SetBrand ("Lenovo");
	com1.print();
	return 0;
}

<file_sep> ///
 /// @file    area_of_rectangle.cc
 /// @author  Islotus(<EMAIL>)
 /// @date    2016-06-08 08:16:24
 ///


//用面向对象的方法求矩形的面积
//首先定义一个矩形类，然后矩形有长、宽属性，有求面积的方法等

#include <iostream>
using namespace std;

class CRectangle
{
public:
	CRectangle(float l = 0.0f, float w = 0.0f)
	{
		this -> length = l;
		this -> width = w;
	}
	float area()
	{
		return this -> length * this -> width;
	}
	//...
private:
	float length;
	float width;
};

int main()
{
	CRectangle rect(3.0f, 4.0f);
	cout << rect.area() << endl;
	return 0;
}

