编写C++程序:声明一个动物类animal,再由此派生出狗类Dog和猫类Cat。

作者&投稿:采郑 (若有异议请与网页底部的电邮联系)
编写程序:声明一个哺乳动物类Mammal,再由此派生出狗类Dog和猫类Cat。~

#include
using namespace std;
enum myColor{BLACK ,WHITE};
class Mammal{
public :
//constructors
Mammal();
~Mammal();
//acessors
int GetAge() const{return itsAge;}
void SetAge(int age){itsAge=age;}
int GetWeight (int weight){itsWeight=weight;}
//Others
void Speak() const{cout<<"Mammal sound!
";}
protected:
int itsAge;
int itsWeight;
};
class Dog:public Mammal
{public:

Dog();
~Dog();
myColor GetColor()const{return itsColor;}
void SetColor(myColor color){itsColor=color;}
void WagTail(){cout<<"Tail wagging...
";}
private:
myColor itsColor;
};
Mammal::Mammal():
itsAge(1),
itsWeight(5)
{
cout<<"Mammal constructor...
";
}
Mammal::~Mammal()
{
cout<<"Mammal destructor...
";
};
Dog::Dog()
{
cout<<"Dog destuctor...
";
}
Dog::~Dog()
{
Dog Jack;
Jack.Speak();
Jack.WagTail();
cout<<"Jack is"<<Jack.GetAge()<<"year old
";
}
void main()
{
Dog dog1;
}

1、首先在C语言中,static const与const static修饰变量的效果一样。

2、属于类,不属于对象,如下图所示。

3、程序的运行过程中只有一个副本,如下图所示。

4、不能在构造函数中初始化,如下图所示。

5、最后基本类型中被static const修饰的浮点型变量不能在声明时初始化,其它类型可以,建议统一在类体外初始化,避免出错。

#include <iostream>
#include <string>
using namespace std;

class animal
{
public:
animal(string n){ name = n; };
string GetName(){ return name; };
virtual float GetWeight()= 0;
private :
string name;

};

class dog :public animal
{
public:
dog(float w, string n) :animal(n){ weight = w; };
float GetWeight() { return weight; };
private:
float weight;
};

class cat : public animal
{
public:
cat(float w, string n) :animal(n){ weight = w; };
float GetWeight() { return weight; };
private:
float weight;
};
void main()
{
dog d(2,"dog1");
cat c(1, "cat1");
cout << d.GetName() << ":" << d.GetWeight() << "KG" << endl;
cout << c.GetName() << ":" << c.GetWeight() << "KG" << endl;
}


int main()吧


如何使用Visual Studio 2010编写一个C语言程序
第四步:如下图所示,自动加载新建的项目(由于之前有存在HelloWorld的项目,所以就改名为HelloWorld2),右击项目选择【添加】→【新建项】,进入第五步,如图所示。第五步:如下图所示,在左栏中单击【Visual C++】,在中间栏选择【C++文件】,在下面的名称栏里填写C语言程序的名称(注意:不要忘记加...

c程序代码写好后,怎么保存到电脑里?什么格式?
具体如下:1、第一步,打开C-Free5.0创建一个新的空白页,然后编写C语言的基本格式,注意格式缩进,见下图,然后进入下一步。2、其次,完成上述步骤后,使用“ FILE”进行声明,如下图所示,然后进入下一步。3、接着,完成上述步骤后,使用fp = fopen(“ c:\\\\ czb_one.txt”,“ wb”)...

怎么在linux中编写c语言程序
图中的代码为:现在回到终端操作,输入命令 gcc helloworld.c ,按下回车键进行编译,稍等片刻编译完成,请看图。如果不是如图所示结果也不要着急,检查下代码是否一样。上面使用gcc命令把helloworld.c文件转换为了名为a.out的可执行文件 输入命令 .\/a.out 就可以运行程序了,请看运行结果图。

这个c语言程序怎么写?大小写字母转换,原程序如下
include<stdio.h>#include<string.h>\/\/声明一个能转换英文字符大小写的函数。\/\/如果传入其它字符,例如空格,该函数返回原字符。char reverse(char ch);main(){ char ch;int i=0;\/\/读取一行字符串ch=getchar();while(ch!='#') \/\/如果还有字符{ \/\/那么就将该字符传给reverse函数去处理,并...

C语言关键词学习总结?
C++提供一种提高效率的方法,即在编译时将函数调用处用函数体替换,类似于C语言中的宏展开。这种在函数调用处直接嵌入函数体的函数称为内联函数(inlinefunction),又称内嵌函数或内置函数。 优点:内联函数可以有效避免函数调用的开销,程序执行效率更高。 缺点:如果被声明为内联函数的函数体非常大,则编译器编译后程序...

用C语言写一个删除学生信息的程序。(要求有删除的函数)。
用C语言写一个删除学生信息的程序。(要求有删除的函数)。  我来答 首页 用户 认证用户 帮帮团 认证团队 合伙人 热推榜单 企业 媒体 ...\/\/函数声明,本程序共10个子函数,每个函数对应一个操作 void student_scanf(int n); void student_printf(int n); int student_find_name(int n);...

怎么用visual studio2015写c语言
在选择新建项:6、在弹出的页面中,系统会默认后缀是c++文件的cpp后缀,所以直接命名为“.c”后缀就行了,接着再点”完成“。7、这样一个C文件就建好了,新建一个程序打印一句话,按下crtl+F5编译运行:8、弹出窗口中会打印出刚才设置的文字,就证明可以用c语言写程序了:...

c程序的每行中只能写一条语句
c语言定义语句的规则如下:1、c程序与main函数的位置无关,从main函数执行,由编译器处理。2、c程序的写法是自由的,一行可以写几个句子,一个句子也可以写多行。 但是为了提高程序的可读性,应该养成良好的写作习惯。3、语句和数据声明为分号(; )结束。4、一个c程序由一个或多个源文件组成,一个...

c语言头文件怎么写呀?
C++\/C程序的头文件以“.h”为后缀。以下是假设名称为graphics.h的头文件:ifndef GRAPHICS_H\/\/作用:防止graphics.h被重复引用 define GRAPHICS_H include<...>\/\/引用标准库的头文件 ...include"..."\/\/引用非标准库的头文件 ...void Function1(...);\/\/全局函数声明 ...inline();\/\/inline...

c语言怎么编写子程序?
C语言 函数定义 出现在 main() 函数 以后的,则要在 main() 函数 以前 加写 函数原型声明。例如:计算10个数的平均值,最大值,最小值子程序:include<stdio.h> include<stdlib.h> void f(int n, int *a, int *a_max, int *a_min, float *a_mean);\/\/ 函数原型声明 void main(){...

望城县17647186213: C++ 定义一个Animal类,私有成员有年龄、身高、体重 -
糜竖快胃: 你的构造函数不必要那么麻烦,直接这样就行了: Animal(float a1=0 , float w1=0 , float h1=0 ):a(a1),w(w1),h1(h){}; 如果你强行要在函数外定义的话,就别加上默认参数了

望城县17647186213: c++ 多态问题 定义一个基类Animal -
糜竖快胃: class Animal { protected:std::string name;int weight; public:// 虚的析构函数是必要的virtual ~Animal(){} // who 就用不到虚函数了吧...std::string who() const{char buf[30];memset(buf,0,30);sprintf(buf,"%d",weight);return name + "...

望城县17647186213: C++面象对象程序设计.
糜竖快胃: #include&lt;iostream&gt; #include&lt;string&gt; using namespace std; class Animal { string mingzi; string zhonglei; public: Animal(){mingzi="";zhonglei="";} Animal(string &amp;a,string &amp;b){mingzi=a;zhonglei=b;} void Identify(){cout&lt;&lt;mingzi&lt;&lt;"\'s de zhonglei shi"&lt;&lt;zhonglei&lt;&lt;endl;} };

望城县17647186213: 设计一个c++的动物的类 -
糜竖快胃: 自己修改一下:#include using namespace std; class Animal { public: Animal(const char *n); virtual ~Animal(); virtual void Eat(); virtual void Sleep(); const char *GetName() const { return name; } void SetName(const char *n); private: char *name; }; ...

望城县17647186213: 跪求C++答案 -
糜竖快胃: 32题答案:#include template max5(T *a); main() { int a[5]; double b[5]; int i; cout>a[i]; max5(a); cout>b[i]; max5(b); } template max5(T *a) { T max=a[0]; for(int i=1;imax) max=a[i]; cout using namespace std; int add(int,int); double add(double,double); int main() { cout

望城县17647186213: C++编程题目:定义一个CAnimal类,该类有数据成员name.用来表示canimal的名字,类型是字符串···详细见 -
糜竖快胃: ..我老师上课举的例子 , 我给你找出来 class CAnimal { string m_sName; int m_nCallCount; public: CAnimal(string _name) { m_sName = _name; m_nCallCount = 1; } void SetCount(int _count) { m_nCallCount = _count; } void Call() { cout << "我是...

望城县17647186213: c++编程题,急!!!!!!! -
糜竖快胃: #include using namespace std;class Animal{public: int Age; int Weight;public: void Walk(){cout...

望城县17647186213: C++课程设计 -
糜竖快胃: #include class animal //动物基类 { public: animal() { cout<<"This is animal class\n"; } }; class mammal:public animal //哺乳动物类 { public: mammal() { cout<<"This is mammal class\n"; } }; class bird:public animal //鸟类 { public: bird() { cout<<"...

望城县17647186213: C++声明一个Cat类,包含name,color等属性以及对这些属性的操作的方法 急求 -
糜竖快胃: class Cat { char name[256]; //定义名字 int color; //定义颜色char* GetName(){ return name; }void SetName(char* chName){ strcpy(name,chName); } };类中有名字 颜色,保存字符串也可以用标准库的string,其他方法和属性都类似.希望对你有帮助

望城县17647186213: c++编程问题
糜竖快胃: #include<iostream> using namespace std; class Animal {public:void eat(); //吃东西的动作string speak(); //叫喊double run(); //跑步的动作void ml(Animal& ) //性行为动作 private:string type; //什么动物int years; //年龄char ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网