C++第四章 编写一个类

编写一个类,要求
1.包含至少两个构造函数的重载
2.包含复制构造函数
3.包含静态数据成员
4.对以上数据成员的赋值,函数调用

 

class OneClass
{
    private:
        int age;
        static int id;
    public:
        OneClass() {}
        OneClass(int a,int b) {age = a; id = b; }
        OneClass(OneClass &one) {age = one.age;}
        Print() {cout<<age<<id<<endl;}
};

int main()
{
    OneClass one(18,33923);
    OneClass two(one);
    one.Print();
    two.Print();
}

 

代码如下,如有帮助,请采纳一下谢谢。

#include <iostream>
using namespace std;
class Test
{
private:
	int ma;
	static int mb ;
public:
	Test(){cout << "无参构造函数"<<endl;ma = 0; mb = 0;}
	Test(int a,int b){cout << "双参构造函数"<<endl;ma = a; mb = b;}
	Test(int a){cout << "单参构造函数"<<endl;ma = a;}
	Test(Test &s){cout << "拷贝构造函数"<<endl;ma = s.ma;mb= s.mb;}
	void display(){
		cout << "ma = " << ma << " ;mb = " << mb << endl;
	}
};

int Test::mb = 0;

int main()
{
	Test a;
	a.display();
	Test b(1);
	b.display();
	Test c(2,3);
	c.display();
	
	Test d = a;
	d.display();
	return 0;
}