几点点基础知识和一个小程序

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;
}
相关文章
相关标签/搜索