码迷,mamicode.com
首页 > 编程语言 > 详细

4.22c++练练手

时间:2015-04-22 15:18:56      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:   c++   函数重载   构造函数   

/*#include <iostream>
using namespace std;
class TDate
{
	public:
		void SetDate(int y, int m, int d);
		int IsLeapYear();
		void Print();
	private:
		int year,month,day;
};
void TDate::SetDate(int y, int m, int d)
{
	year = y;
	month = m;
	day = d;
}
int TDate::IsLeapYear()
{
	return (year%4 == 0 && year%100 != 0) || (year%400 == 0); 
}
void TDate::Print()
{
	cout<<year<<"."<<month<<"."<<day<<endl;
}
int main()
{
	TDate date1,date2;
	date1.SetDate(1996,5,4);
	date2.SetDate(1998,4,9);
	int leap = date1.IsLeapYear();
	cout<<leap<<endl;
	date1.Print();
	date2.Print();
	return 0;
}*/
/*class TPoint
{
	public:
		void SetPoint(int x, int y);
		int Xcoord()
		{
			return X;
		}
		int Ycoord()
		{
			return Y;
		}
		void Move(int xOffset, int yOffset);
	private:
		int X,Y;
};
void TPoint::SetPoint(int x, int y)
{
	X = x;
	Y = y;
}
void TPoint::Move(int xOffset, int yOffset)
{
	X+=xOffset;
	Y+=yOffset;
}
#include <iostream>
using namespace std;
int main()
{
	TPoint p1,p2;
	p1.SetPoint(3,5);
	p2.SetPoint(8,10);
	p1.Move(2,1);
	p2.Move(1,-2);
	cout<<"x1="<<p1.Xcoord()<<"y1="<<p1.Ycoord()<<endl;
	cout<<"x2="<<p2.Xcoord()<<"y2="<<p2.Ycoord()<<endl;
}*/
/*#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	double a = 21.23;
	cout<<setprecision(1)<<a<<endl;
	return 0;
}*/
/*#include<iostream>
using namespace std;
int main()
{
	double a = 1.545;
	cout.precision(3);		//输出3位
	cout<<a<<endl;
	return 0;
}*/

/*#include <iostream>
using namespace std;
class TDate1
{
	public:
		TDate1(int y, int m, int d);
		~TDate1();
		void Print();
	private:
		int year,month,day;
};
TDate1::TDate1(int y, int m, int d)
{
	this->year = y;
	this->month = m;
	day = d;
	cout<<"Constructor called.\n";
}
TDate1::~TDate1()
{
	cout<<"Destructor called.\n";
}
void TDate1::Print()
{
	cout<<this->year<<"."<<month<<"."<<day<<endl;
}
int main()
{
	TDate1 today(1998,4,9),tomorrow(1998,4,10);
	cout<<"today is";
	today.Print();
	cout<<"tomorrow is";
	tomorrow.Print();

	system("pause");
}*/

/*#include <iostream>
#include <iomanip>

using namespace std;
int main()
{
	int a;
	cout<<"input a:";
	cin>>a;
	cout<<setbase(16)<<a<<endl;
	cout<<"dec:"<<dec<<a<<endl;
	cout<<"hex:"<<setbase(16)<<setw(10)<<a<<endl;
	cout<<"oct:"<<oct<<a<<endl;
	char *p = "China";
	cout<<setw(10)<<p<<endl;
	getch();
	return 0;
}*/
/*#include <iostream>
using namespace std;
class TDate												//定义一个日期类
{
	public:
		TDate(int x,int y,int z);						//构造函数,对成员变量进行初始化
		int IsLeapyear();								//成员函数,判断是否为闰年
		void print();									//输出各成员的值
	private:
		int year,month,day;
};
TDate::TDate(int x, int y, int z)
{
	year = 	x;
	month = y;
	day = z;
}
int TDate::IsLeapyear()
{
	return ((year%4 == 0 && year%100 != 0)||(year%400 == 0));
}
void TDate::print()
{
	cout<<year<<"."<<month<<"."<<day<<endl;
}
int main()
{
	int leap;
	TDate date1(1996,5,4);								
	leap = date1.IsLeapyear();
	date1.print();
	if(leap == 1)
		cout<<"是闰年"<<endl;
	else
		cout<<"不是闰年"<<endl;
	return 0;
}*/
#include <iostream>
using namespace std;
int add(int x,int y)					//求和函数的重载
{
	int z;
	z = x+y;
	return z;
}
double add(double x,double y)
{
	double z;
	z = x+y;
	return z;
}
int main()
{
	cout<<add(1,2)<<endl;				//参数都为int型
	cout<<add(1.2,2.0)<<endl;			//参数都为double型
	return 0;
}

4.22c++练练手

标签:   c++   函数重载   构造函数   

原文地址:http://blog.csdn.net/byz1993/article/details/45194821

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!