面向对象程序设计(C++)
来源:百度知道 编辑:UC知道 时间:2024/06/18 10:10:24
要求:根据不同的类可设置相应的构造函数、析构函数,以及设置、显示相关属性等相关行为。每个类至少定义4个成员函数。
(4)定义DOG类。(可设有年龄、体重、颜色等属性)
(4)定义DOG类。(可设有年龄、体重、颜色等属性)
#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