入门c++问题,与类有关

来源:百度知道 编辑:UC知道 时间:2024/06/17 13:24:24
声明一个dog类,包含age,weight等属性以及关于这些属性的函数。要求使用构造函数、析构函数和构造拷贝函数。并使用这个类创建一段主程序。

谢谢各位~

#include<iostream>
using namespace std;
class dog{
private:
int age;
double weight;
public:
dog(int a,double w){
age=a;weight=w;
}//Constructor;
dog(dog &t){
age=t.age ;weight=t.weight ;
}//Copy Constructor;
~dog(){
cout<<"Destructor is called"<<endl;
}//Destructor;
void setdog(int a,double w){
age=a;weight=w;
}
void prt(){
cout<<"the dog's age is: "<<age<<endl;
cout<<"the dog's weight is: "<<weight<<endl;
cout<<endl;
}
};//definiton the CLASS of dog;
void main(){
dog d(6,12.6);
d.prt ();
d.setdog (9,25.32);
dog d1(d);
d.prt();
return;
}
这是最简单的了,我也是初学C++;很愿意共同交流;
353880028是我的QQ