C++类构造拷贝析构函数

代码-F

#include<iostream>
using namespace std;
class Zhu{
public://共有人人可调用
    Zhu();//构造函数
    Zhu(Zhu &j);//拷贝函数
    char name[15]="zhu jia hao";
    void setbirthday(int a,int b,int c);//自己人
    void showbirthday();//自己人
    void showname();//自己人
    ~Zhu();//老夫是析构函数(别名:清洁工)
private://私有自己人可调用
    int year,month,day;
};
void Zhu::setbirthday(int a,int b,int c)
{
    year=a;month=b;day=c;
}
void Zhu::showbirthday()
{
    cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
void Zhu::showname()
{
    cout<<name<<endl;
}
Zhu::Zhu()
{
    year=10;month=9;day=8;
}
Zhu::Zhu(Zhu &j)
{   year=j.year;
    month=j.month;
    day=j.day;
    cout<<"我复制了一个东东!"<<endl;
}
Zhu::~Zhu()
{
    cout<<"我干了什么!"<<endl;
}
int main()
{Zhu first;
 first.showname();
 first.setbirthday(1999,9,24);
 first.showbirthday();
 cout<<endl;//打个空格
 Zhu second(first);
 second.showname();
 second.showbirthday();
cout<<endl;//打个空格
 return 0;
}

代码-S

#include<iostream>
using namespace std;
int main()
{
	int a[9] = { 9,5,2,3,4,6,1,8,7 };
	int chance, b, c;
	for (b = 0; b < 9; b++)
	{
		for (c = b + 1; c < 9; c++)
		{
			if (a[b] > a[c])
			{
				chance = a[b];
				a[b] = a[c];
				a[c] = chance;
			}
		}
		cout << a[b];
	}
	cin.get();
	return 0;
}

代码-T

#include<iostream>
using namespace std;
class Wuliao {
public:
	Wuliao(); //构造一个无参函数
	/*void Setname(char a[])
	{
		name = *a;
	}*/
	Wuliao(Wuliao &d);
	void Setage(int a);
	void Setbirthday(int a, int b, int c);
	void Showall();
	//char name[20];
	 ~Wuliao();
	 int r = 1;
private:
	int age, year, month, day;
};
Wuliao::Wuliao()
{
	age = 1; year = 2; month = 3; day = 4;
}
 Wuliao::~Wuliao()
{
	cout << r << endl;
}
Wuliao::Wuliao(Wuliao &d)
{
	age = d.age; year = d.year; month = d.month; day = d.day;
}
void Wuliao::Setage(int a)
{
	age = a;
}
void Wuliao::Setbirthday(int a, int b, int c)
{
	year = a; month = b; day = c;
}
void Wuliao::Showall()
{
	cout << "姓名:" <<"张三" << "\n" << "年龄:" << age<<"\n" << "生日(农历):" << year
		<<"."<< month << "." << day << endl;
}
int main()
{
	int a[9] = { 8,5,6,1,2,3,4,9,7 }, b, c;
	cout << "你进入了一个数组!" << "\n" << "请选择一个数字:";
	cin >> b;
	cout << "你选择的是:" << b << endl;
	for (c = 0; c < 9; c++)
	{
		if (b == a[c])
			cout << "你输入的是数组中第" << c + 1 << "个数" << endl;
	}
	Wuliao p;
	p.Setage(19);
	p.Setbirthday(1999, 9, 24);
	p.Showall();
	Wuliao y(p);
	y.Showall();
	y.r = 2;
	return 0;
}

End

~
记录留存

发布了10 篇原创文章 · 获赞 0 · 访问量 79

猜你喜欢

转载自blog.csdn.net/weixin_44228006/article/details/104094994