C++实验---构造函数与析构函数练习

构造函数与析构函数练习

Description

有一个类,其属性由一个整数和一个实数确定,请定义这个类,使其能够在main函数中使用时,产生满足题目所要求的输出。

Input

一个整数,一个实数。

Output

见样例。

Sample Input

1 2.56

Sample Output

A new object of i=1 and d=2.56 is constructed.
A new object of i=0 and d=0 is constructed.
The object of i=0 and d=0 is deconstructed.
The object of i=1 and d=2.56 is deconstructed.

题目给定的主函数

int main()
{
    
    
    int a;
    double b;
    cin>>a>>b;
    myClass m1(a,b),m2;
    return 0;
}

code:

#include<iostream>

using namespace std;

class myClass{
    
    
	int a;
	double b;
public:
	myClass(int aa,double bb){
    
    
		a=aa;
		b=bb;
		cout<<"A new object of i="<<a<<" and d="<<b<<" is constructed."<<endl;
	}
	
	myClass(){
    
    
		a=0;
		b=0;
		cout<<"A new object of i="<<a<<" and d="<<b<<" is constructed."<<endl;
	}
	
	~myClass(){
    
    
		cout<<"The object of i="<<a<<" and d="<<b<<" is deconstructed."<<endl;
	}

}; 


int main()
{
    
    
    int a;
    double b;
    cin>>a>>b;
    myClass m1(a,b),m2;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/timelessx_x/article/details/115411652