C++面向对象类的实例题目一

在一个程序中,实现如下要求:

(1)构造函数重载

(2)成员函数设置默认参数

(3)有一个友元函数

(4)有一个静态函数

(5)使用不同的构造函数创建不同对象

code:

#include<iostream>
using namespace std;
class A
{
	public:
		A()
		{
			cout<<"Defalut constructor called."<<endl;//默认构造函数
			count++; 
		}
		A(int i)
		{
			cout<<"constructor1=====>a:"<<i<<endl;	//有一个参数的构造函数 
			a = i;
			count++; 
		}
		friend void show(A &a1);	//定义了一个友元函数 
		static void  show_num()		//定义了一个静态函数 
		{
			cout<<"number:"<<count<<endl; 
		}
		void set(int i=0)		//定义一个具有默认参数的成员函数 
		{
			a = i;
		} 
		int a;
		static int count;
};
int A::count = 0;	//在类的定义之外初始化静态成员变量
void show(A &a1)
{
	cout<<"a:"<<a1.a<<endl; 
} 
int main()
{
	A a1,a2(5);
	show(a1);
	show(a2);
	A::show_num();
}

输出:

Defalut constructor called.
constructor1=====>a:5
a:0
a:5
number:2

posted @ 2013-12-31 15:43  千手宇智波  阅读(312)  评论(0编辑  收藏  举报