幾點點基礎知識和一個小程序

typedef  int  pint              爲int類型起一個別名 在其做用域裏這個別名就是類型ios

#define A B               編譯時遇到A直接替換爲B,,,,c++

===========================================ubuntu

typedef int * pint學習

#define PINT int *測試

const pint p  ===== const (int *)p      (int*)是總體,是一個類型      const 限定 "p"            this

typedef int* pint;
#define Pint int*
        int a = 5;
	int b = 9;
	const pint p = &a;
	cout << *p<<endl;
	*p = b;
	cout << *p <<endl;

	輸出
5
9

const PINT p ====== const int * p      const 限定 "*p"spa

        int a = 5;
	int b = 9;
	const Pint p = &a;
	cout << *p<<endl;
	p = &b;
	cout << *p <<endl;
	輸出
5
9

=================================================================================
調試

學習c++時候的一個做業,操做符重載,完成時間(時分秒)的運算code

可以完成+/-24小時內的運算(應該加個day的做業沒要求,就沒寫,能夠很簡單的類推出來,不過想到的時候寫了一半了,對象

分了一個.h頭文件和一個實現.cpp        Ctimetest.cpp是測試文件

在ubuntu 14.04下運行調試沒有問題

.h

#ifndef _Ctime_H__
#define _Ctime_H__

class Ctime  
{
	private:
		int hour;
		int minute;
		int second;
	public:
		void print();
		Ctime(int h = 0,int m = 0,int s = 0);
		void setTime(int h,int m,int s);
		//比較運算符(二目)的重載
		bool operator > (Ctime &t);
		bool operator < (Ctime &t);
		bool operator >= (Ctime &t);
		bool operator <= (Ctime &t);
		bool operator == (Ctime &t);
		bool operator != (Ctime &t);  
		//二目運算符的重載  
		Ctime operator+(Ctime &c);//返回c所規定的時、分、秒後的時間,例t1(8,20,25),t2(11,20,50),t1+t2爲:41:15  
		Ctime operator-(Ctime &c);//對照+理解
		Ctime operator+(int s);//返回s秒後的時間  
		Ctime operator-(int s);//返回s秒前的時間  
		//一目運算符的重載  
		Ctime operator++(int);//後置++,下一秒 
		Ctime operator++();//前置++,下一秒,前置與後置返回值不同 
		Ctime operator--(int);//後置--,前一秒
		Ctime operator--();//前置--,前一秒  
		//賦值運算符的重載       
		Ctime operator+=(Ctime &c);  
		Ctime operator-=(Ctime &c);  
		Ctime operator+=(int s);   
		Ctime operator-=(int s);   
}; 

#endif

.cpp

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

void Ctime::print()
{
	std::cout << hour << ":" << minute << ":" << second << std::endl;
}

Ctime::Ctime(int h,int m,int s)
{
	hour = h;
	minute = m;
	second = s;
}
void Ctime::setTime(int h,int m,int s)
{
	if(s < 0)
	{
		m = m+s/60-1;
		s = 60+s%60;
	}
	if(s >= 60)
	{
		m += s/60;
		s = s%60;
	}
	if(m < 0)
	{
		h = h+m/60-1;
		m = 60+m%60;
	}
	if(m >= 60)
	{
		h += m/60;
		m = m%60;
	}
	if(h < 0)
		h = 24+h%24;
	hour = h;
	minute = m;
	second = s;
}

bool Ctime::Ctime::operator > (Ctime &t)
{
	if(hour > t.hour)
		return true;
	else if(hour == t.hour && minute > t.minute)
		return true;
	else if(hour == t.hour && minute == t.minute && second > t.second)
		return true;
	else return false;
}
bool Ctime::operator < (Ctime &t)
{
	if(hour < t.hour)
		return true;
	else if(hour == t.hour && minute < t.minute)
		return true;
	else if(hour == t.hour && minute == t.minute && second < t.second)
		return true;
	else 
		return false;
}
bool Ctime::operator >= (Ctime &t)
{
	if(hour == t.hour && minute == t.minute && second == t.second)
		return true;
	if(hour > t.hour)
		return true;
	else if(hour == t.hour && minute > t.minute)
		return true;
	else if(hour == t.hour && minute == t.minute && second > t.second)
		return true;
	else return false;
}
bool Ctime::operator <= (Ctime &t)
{
	if(hour == t.hour && minute == t.minute && second == t.second)
		return true;
	if(hour < t.hour)
		return true;
	else if(hour == t.hour && minute < t.minute)
		return true;
	else if(hour == t.hour && minute == t.minute && second < t.second)
		return true;
	else return false;
}
bool Ctime::operator == (Ctime &t)
{
	if(hour == t.hour && minute == t.minute && second == t.second)
		return true;
}
bool Ctime::operator != (Ctime &t)
{
	if(hour != t.hour && minute != t.minute && second != t.second)
		return true;
	return false;
}

Ctime Ctime::operator+(Ctime &c)
{
	Ctime tmp;
	int th,tm,ts;
	th = hour + c.hour;
	tm = minute + c.minute;
	ts = second + c.second;
	tmp.setTime(th,tm,ts);
	return tmp;
}

Ctime Ctime::operator-(Ctime &c)
{
	Ctime tmp;
	int th,tm,ts;
	th = hour - c.hour;
	tm = minute - c.minute;
	ts = second - c.second;
	tmp.setTime(th,tm,ts);
	return tmp;
}
Ctime Ctime::operator+(int s)
{
	second += s;
	setTime(hour,minute,second);
	return *this;
}
Ctime Ctime::operator-(int s)
{
	second -= s;
	setTime(hour,minute,second);
	return *this;
}
Ctime Ctime::operator++(int)
{
	Ctime tmp;
	int th,tm,ts;
	th = hour;
	tm = minute;
	ts = second++;
	tmp.setTime(th,tm,ts);
	return tmp;
	
}
Ctime Ctime::operator++()
{
	++second;
	setTime(hour,minute,second);
	return *this;
}
Ctime Ctime::operator--(int)
{
	Ctime tmp;
	int th,tm,ts;
	th = hour;
	tm = minute;
	ts = second--;
	tmp.setTime(th,tm,ts);
	return tmp;
}
Ctime Ctime::operator--()
{
	--second;
	setTime(hour,minute,second);
	return *this;
}
Ctime Ctime::operator+=(Ctime &c)
{
	*this = *this + c;
	return *this;
}
Ctime Ctime::operator-=(Ctime &c)
{
	*this = *this -c;
	return *this;
} 
Ctime Ctime::operator+=(int s)
{
	*this = *this + s;
	return *this;
}
Ctime Ctime::operator-=(int s)
{
	*this = *this - s;
	return *this;
}

測試

#include"Ctime.h"
#include<iostream>
using namespace std;

int main()
{
	Ctime test1,test2,test3,test4;
	test1.print();
	test1.setTime(10,20,30);
	test4.setTime(5,10,15);
	test1.print();
	cout << (test1 > test2 ? 1:0) << endl;
	cout << (test1 < test2 ? 1:0) << endl;
	cout << (test1 >= test2 ? 1:0) << endl;
	cout << (test1 <= test2 ? 1:0) << endl;
	cout << (test2 >= test3 ? 1:0) << endl;
	cout << (test2 <= test3 ? 1:0) << endl;
	cout << (test1 != test2 ? 1:0) << endl;
	cout << (test2 != test3 ? 1:0) << endl;
	(test1+test4).print();
	(test1-test4).print();
	(test4-test1).print();
	(test2+100).print();
	(test2+100-50).print();
	cout << "自增:" << endl;
	test3.print();
	(test3++).print();
	test3.print();
	(++test3).print();
	test3.print();
	cout << "自減:" << endl;
	(test3--).print();
	test3.print();
	(--test3).print();
	test3.print();
	cout << "對象的 +=和-= " << endl;
	test1.print();
	test2.print();
	test1 += test2;
	test1.print();
	test1 -= test2;
	test1.print();
	cout << "int +=和-= " << endl;
	test1 += 5;
	test1.print();
	test1 -= 5;
	test1.print();

	return 0;
}
相關文章
相關標籤/搜索