C/C++学习笔记五

1、模板函数+冒泡排序

#include "stdafx.h"
#include <iostream.h>

template <class Type>								//定义一个模板
void Sort(Type Array[], int nLen)						
{
	for(int i=0; i<nLen-1; i++)							//起泡法排序
	{
		for(int j=0; j<nLen-i-1; j++)
		{
			if (Array[j] > Array[j+1])					//交换数组元素
			{
				Type nTmp = Array[j];
				Array[j] = Array[j+1] ;
				Array[j+1] = nTmp;
			}
		}
	}
}


int main(int argc, char* argv[])
{
	int nArray[] = {85, 98, 45, 76, 75};
	Sort(nArray, 5);
	cout << "整数排序" << endl;
	//输出结果
	for(int i=0; i<5; i++)
	{
		cout << nArray[i] << endl;
	
	}
	double dArray[] = {76.85, 95.75, 84.56, 85.5, 67.4};
	Sort<double>(dArray, 5);
	//输出结果
	cout << "实数排序" << endl;
	for(int j=0; j<5; j++)
	{
		cout << dArray[j] << endl;
	}
	return 0;
}

2、根据年月日计算是一年中的第几天

        需要注意区分平年和闰年。

// OutDays.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "string.h"
#include "stdlib.h"
#include "iostream.h"

//判断某一年是否是闰年
bool IsLeapyear(int nYear)
{
	if (nYear % 4 == 0 && nYear % 100 != 0)
	{
		return true;
	}
	else if (nYear % 100 == 0 && nYear % 400 == 0)
	{
		return true;
	}

	return false;
}

//pszData格式XXXX-XX-XX
int GetDays(const char* pszDate)
{
	//解析年月日
	char szYear[5] = {0};
	char szMonth[3] = {0};
	char szDay[3] = {0};
	strncpy(szYear, pszDate, 4);
	
	int nYear = atoi(szYear);
	char* pszMonth = (char*)pszDate + 5;
	strncpy(szMonth, pszMonth, 2);
	int nMonth = atoi(szMonth);
	
	char* pszDay =  pszMonth + 3;
	int nDay = atoi(pszDay);
	
	//判断日期是否正确
	if (nMonth < 1 || nMonth > 12)
		return -1;
	if (nDay < 1 )
		return -1;
	if (nMonth == 1 || nMonth == 3 || nMonth == 5 || nMonth == 7 
			|| nMonth == 8 || nMonth == 10 || nMonth == 12)
	{
		if (nDay > 31)
			return -1;
	}
	else if (nDay > 30)
	{
		return -1;
	}
	
	bool bLeapYear = IsLeapyear(nYear);
	if (bLeapYear && nMonth == 2)
	{
		if (nDay > 29)
			return -1;
	}
	else if (nMonth == 2)
	{
		if (nDay > 28)
			return -1;		
	}

	int nDayNum = nDay;
    // 循环月份
	for (int i=1; i<nMonth; i++)
	{    
        // 判断是大月还是小月
		if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
		{
			nDayNum += 31;
		}
		else if (i==2)
		{
			if (bLeapYear)
			{
				nDayNum += 29;
			}
			else
			{
				nDayNum += 28;
			}
		}
		else 
		{
			nDayNum += 30;
		}
	}
	return nDayNum;
}

int main(int argc, char* argv[])
{
	int nNum = GetDays("2009-05-31");
	cout << "第" << nNum << "天" << endl;
	return 0;
}

3、实现struct和了解方法原型

#include "stdafx.h"
#include "iostream.h"
#include "string.h"

struct CUser							//定义一个类
{
	char m_Username[128] ;				//定义数据成员
	char m_Password[128];				//定义数据成员
	bool Login();						//定义方法原型
};
bool CUser::Login()						//实现CUser类中的Login方法
{
	if (strcmp(m_Username,"MR")==0 && strcmp(m_Password,"KJ")==0)
	{
		cout << "登录成功!" << endl;
		return true;
	}
	else
	{
		cout << "登录失败!" << endl;
		return false;
	}
}


int main(int argc, char* argv[])
{
	CUser User;								//定义一个对象User
	strcpy(User.m_Password, "KJ");				//设置m_Password成员数据
	strcpy(User.m_Username, "MR");				//设置m_Username成员数据
	User.Login();								//调用Login方法
	return 0;
}

4、体现封装原则的类

        使用private/public设置成员访问级别;成员对象设置私有;调用/设置方法设置公开;

#include "stdafx.h"
#include "iostream.h"
#include "string.h"

class CUser										//定义一个类
{
private:											//设置成员访问级别
	char m_Username[128];							//定义数据成员
	char m_Password[128];							//定义数据成员
public:											//设置成员访问级别
	void SetUsername(const char *pUsername)			//公有方法,设置用户名称
	{
		if (pUsername != NULL)						//判断参数是否为空
		{
			strcpy(m_Username,pUsername);
		}
	}
	char* GetUsername()							//公有方法,获取用户名称
	{
		return (char*)m_Username;					//返回用户名
	}
	void SetPassword(const char *pPassword)			//公有方法,设置密码
	{
		if (pPassword != NULL)						//判断参数是否为空
		{
			strcpy(m_Password,pPassword);
		}
	}
	char* GetPassword()								//公有方法,获取用户密码
	{
		return (char*)m_Password;
	}
	bool Login()									//定义方法
	{
		if (strcmp(m_Username,"MR")==0 && strcmp(m_Password,"KJ")==0)
		{
			cout << "登录成功!" << endl;
			return true;
		}
		else
		{
			cout << "登录失败!" << endl;
			return false;
		}
	}
};

int main(int argc, char* argv[])
{
	CUser User;									//定义CUser类对象
	//strcpy(User.m_Password, "KJ");					//不能够直接访问m_Password成员
	//strcpy(User.m_Username, "MR");					//不能够直接访问m_Username成员
	User.SetUsername("MR");						//调用SetUsername设置m_Username成员
	User.SetPassword("KJ");						//调用SetPassword设置m_Password成员
	char* pszUser = User.GetUsername();				//获取m_Username成员数据
	User.Login();									//调用Login方法
	return 0;
}

5、无参和有参构造函数

#include "stdafx.h"
#include "iostream.h"
#include "string.h"

class CUser										//定义一个类
{
private:											//设置成员访问级别
	char m_Username[128];							//定义数据成员
	char m_Password[128];							//定义数据成员
public:	
	CUser()											//默认构造函数
	{
		strcpy(m_Username, "MR");					//为数据成员赋值
		strcpy(m_Password, "KJ");					//为数据成员赋值
		cout << "构造函数被调用!" << endl;				//输出信息
	}
	CUser(const char* pszUser, const char* pszPassword)	//有参数的构造函数
	{
		strcpy(m_Username, pszUser);
		strcpy(m_Password, pszPassword);
		cout << "有参构造函数被调用!" << endl;				//输出信息
	}
	char* GetUsername()const							//公有方法,获取用户名称
	{
		return (char*)m_Username;						//返回用户名
	}
	char* GetPassword()	const							//公有方法,获取用户密码
	{
		return (char*)m_Password;
	}
};



int main(int argc, char* argv[])
{
	CUser defUser;									//定义CUser对象
	cout << "用户名: " << defUser.GetUsername() << endl;		//输出用户名
	CUser paramUser("VCBCCD", "MRSOFT");				//定义CUser对象
	cout << "用户名: " << paramUser.GetUsername() << endl;		//输出用户名
	return 0;
}

6、析构函数

        析构函数在对象释放时调用,用于进行清理对象的工作。

        那么对象何时释放?

        如果对象是在堆栈中构建的,则对象在其作用域消失时释放。例如:

{
    CUser User;
}

        如果对象是在堆中构建的,则在使用delete运算符作用于对象时将调用对象的析构函数。

CUser *pUser = new CUser();    //在堆中构建对象
delete pUse;    //将调用析构函数

        完整示例

#include "stdafx.h"
#include "iostream.h"
#include "string.h"


class CUser										//定义一个类
{
private:											//设置成员访问级别
	char m_Username[128];							//定义数据成员
	char m_Password[128];							//定义数据成员
public:	
	CUser()										//默认构造函数
	{
		strcpy(m_Username, "MR");					//为数据成员赋值
		strcpy(m_Password, "KJ");					//为数据成员赋值
		cout << "构造函数被调用!" << endl;				//输出信息
	}
	CUser(const CUser &refUser)						//定义复制构造函数
	{
		strcpy(m_Username, refUser.m_Username);
		strcpy(m_Password, refUser.m_Password);
		cout << "复制构造函数被调用!" << endl;
	}
	~CUser()										//定义析构函数
	{
		strcpy(m_Username, "");
		strcpy(m_Password, "");	
		cout << "析构函数被调用!" << endl;				//输出信息
	}
};


void OutputUser(CUser User)
{
	cout << "调用OutputUser函数!" << endl;				//输出信息
}
int main(int argc, char* argv[])
{
	CUser User;									//定义一个CUser类对象
	OutputUser(User);								//调用OutputUser函数
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bashendixie5/article/details/124201774