面向对象程序设计(C++)
要求:根据不同的类可设置相应的构造函数、析构函数,以及设置、显示相关属性等相关行为。每个类至少定义4个成员函数。(4)定义DOG类。(可设有年龄、体重、颜色等属性)...
要求:根据不同的类可设置相应的构造函数、析构函数,以及设置、显示相关属性等相关行为。每个类至少定义4个成员函数。
(4)定义DOG类。(可设有年龄、体重、颜色等属性) 展开
(4)定义DOG类。(可设有年龄、体重、颜色等属性) 展开
1个回答
展开全部
#include <iostream>
#include <string>
using namespace std;
class CDog
{
int m_nAge;
float m_fWeight;
string m_strColor;
string m_strName;
public:
CDog(int Age, float Weight, string str1, string str2)
{
m_nAge = Age;
m_fWeight = Weight;
m_strColor = str1;
m_strName = str2;
}
CDog()
{
m_nAge = 2;
m_fWeight = 10;
m_strColor = "黑色";
m_strName = "旺才";
}
void show()
{
cout<<"狗名:"<<m_strName
<<"\n年龄:"<<m_nAge
<<"\n体重:"<<m_fWeight
<<"\n颜色:"<<m_strColor<<endl;
}
void Set(int Age, float Weight, string str1, string str2)
{
m_nAge = Age;
m_fWeight = Weight;
m_strColor = str1;
m_strName = str2;
}
~CDog()
{
}
};
int main()
{
CDog dog//(写参数);
// dog.Set(写参数);
dog.show();
return 0;
}
#include <string>
using namespace std;
class CDog
{
int m_nAge;
float m_fWeight;
string m_strColor;
string m_strName;
public:
CDog(int Age, float Weight, string str1, string str2)
{
m_nAge = Age;
m_fWeight = Weight;
m_strColor = str1;
m_strName = str2;
}
CDog()
{
m_nAge = 2;
m_fWeight = 10;
m_strColor = "黑色";
m_strName = "旺才";
}
void show()
{
cout<<"狗名:"<<m_strName
<<"\n年龄:"<<m_nAge
<<"\n体重:"<<m_fWeight
<<"\n颜色:"<<m_strColor<<endl;
}
void Set(int Age, float Weight, string str1, string str2)
{
m_nAge = Age;
m_fWeight = Weight;
m_strColor = str1;
m_strName = str2;
}
~CDog()
{
}
};
int main()
{
CDog dog//(写参数);
// dog.Set(写参数);
dog.show();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询