c++-初学者一个关于类的问题

问题描述

初学者一个关于类的问题

编译,链接都没有问题.没有得到想要结果,我找不出错在哪,请大家帮看看

头文件

# include <string>
using namespace std;
# include <iostream>
# include <vector>
class Animal
{
public:
    Animal (const char*n): name(n) {}
    virtual ~Animal() {}
    virtual void read ()=0;
protected:
    string name;
};

class Panda:public Animal
{
    friend class Zoo;
public:
    Panda (const char*n): Animal(n) {}
    void read ()
    {
        cout<<"name "<<name<<"     "
            <<"speices panda"<<endl;
    }

};

class Tiger:public Animal
{
    friend class Zoo;
public:
Tiger (const char*n): Animal(n) {}
    void read ()
    {
        cout<<"name "<<name<<"     "
            <<"speices tiger"<<endl;
    }
};

class Monkey:public Animal
{
    friend class Zoo;
public:
Monkey (const char*n): Animal(n) {}
    void read ()
    {
        cout<<"name "<<name<<"     "
            <<"speices monkey"<<endl;
    }
};

class Zoo
{
public:
    Zoo(): contain(0) {}
    Zoo(int c): contain(c), room(c){}
    void ListAnimals();
    int Accept(Animal*);
    void Release (int);
    ~Zoo();
private:
    int contain;
    vector<Animal*> room;

};
==================================================
# include "Animal.h"
using namespace std;

int Zoo::Accept(Animal* one)
{
    vector<Animal*>::size_type i=0;
    Animal* record =one;
    room.push_back(record);
    i = room.size();
    return i;
}
void Zoo::Release(int p)
{
    room[p] = 0;
}
void Zoo::ListAnimals()
{
    int count = 0;
    for(int i=0;i<contain;++i)
    {
        if(room[i] != 0)
        {
            room[i]->read;
            cout<<"position "<<i<<endl;
            ++count;
        }

    }
    cout<<"toatl animals are "<<count<<endl;
}

Zoo::~Zoo()
{
    for(int j=0;j<contain;++j)
    {
        if(room[j]!=0)
        {
            delete room[j];
        }
    }
}
# include "Animal.h"
using namespace std;

int main()
{
   Panda p[1]={Panda("Rudy")};
   Tiger t[2]={Tiger("Sam"),Tiger("Princess")};
   Monkey m[3]={Monkey("Nancy"),Monkey("CaroLine"),Monkey("Bob")};

   Zoo Z(10);               //初始化动物园对象,其最多可饲养10只动物,缺省饲养了0只动物
   for(int i=0;i<2;i++)
   {
       Z.Accept(&t[i]);     //向动物园移入要饲养的动物,并返回该动物所在的位置
       Z.Accept(&m[i]);
   }
   int position=Z.Accept(&p[1]);
   Z.ListAnimals();         //列出动物园当前饲养了多少只动物, 每只动物所在的位置及其种类和姓名
   Z.Release(position);     //从动物园移走指定位置的动物
   Z.ListAnimals();
   Z.Accept(&m[2]);
   Z.ListAnimals();
   return 0;
}

解决方案

倒数第7行应该是p[0]

解决方案二:

#if 1

include

include

include

using namespace std;

class Animal
{
public:
Animal(const char*n) : name(n) {}
virtual ~Animal() {}
virtual void read() = 0;
protected:
string name;
};

class Panda :public Animal
{
friend class Zoo;
public:
Panda(const char*n) : Animal(n) {}
void read()
{
cout << "name " << name << " "
<< "speices panda" << endl;
}

};

class Tiger :public Animal
{
friend class Zoo;
public:
Tiger(const char*n) : Animal(n) {}
void read()
{
cout << "name " << name << " "
<< "speices tiger" << endl;
}
};

class Monkey :public Animal
{
friend class Zoo;
public:
Monkey(const char*n) : Animal(n) {}
void read()
{
cout << "name " << name << " "
<< "speices monkey" << endl;
}
};

class Zoo
{
public:
Zoo() : contain(0) {}
Zoo(int c) : contain(c), room(c){}
void ListAnimals();
int Accept(Animal*);
void Release(int);
~Zoo();
private:
int contain;
vector room;

};

int Zoo::Accept(Animal* one)
{
vector::size_type i = 0;
Animal* record = one;
room.push_back(record);
i = room.size();
return i;
}
void Zoo::Release(int p)
{
/*你用的vector,他是c++标准模版库的类,它提供了自己的迭代器*/
//room[p] = 0;
int i = 0;
for (vector::iterator iter = room.begin(); iter != room.end() && i++ <= p; iter++)
{
*iter = 0;
}

}
void Zoo::ListAnimals()
{
int count = 0;
for (int i = 0; i
{
if (room[i] != 0)
{
room[i]->read();
cout << "position " << i << endl;
++count;
}

}
cout << "toatl animals are " << count << endl;

}

Zoo::~Zoo()
{
for (int j = 0; j<contain; ++j)
{
if (room[j] != 0)
{
delete room[j];
}
}
}

int main()
{
Panda p[1] = { Panda("Rudy") };
Tiger t[2] = { Tiger("Sam"), Tiger("Princess") };
Monkey m[3] = { Monkey("Nancy"), Monkey("CaroLine"), Monkey("Bob") };

Zoo Z(10);               //初始化动物园对象,其最多可饲养10只动物,缺省饲养了0只动物
for (int i = 0; i<2; i++)
{
    Z.Accept(&t[i]);     //向动物园移入要饲养的动物,并返回该动物所在的位置
    Z.Accept(&m[i]);
}
int position = Z.Accept(&p[1]);
Z.ListAnimals();         //列出动物园当前饲养了多少只动物, 每只动物所在的位置及其种类和姓名
Z.Release(position);     //从动物园移走指定位置的动物
Z.ListAnimals();
Z.Accept(&m[2]);
Z.ListAnimals();
return 0;

}
#endif

时间: 2024-08-07 14:48:01

c++-初学者一个关于类的问题的相关文章

c++基础-【初学者跪地求助】一个关于类中vector类型调用的问题。

问题描述 [初学者跪地求助]一个关于类中vector类型调用的问题. 一个项目管理的程序,现在在写输出项目中任务列表的功能. getTaskList()函数中似乎总是无法调用project类中的 任务列表vector 程序运行就会崩溃 C++初学者求帮助啊! 数据已经由initData()函数初始化 有两个包含任务列表的容器 还有一个包含项目列表的容器 #include #include #include using namespace std; class Time { public: Tim

如何编写一个ASP类

前几天大佛写了"ASP设计模式",可能有些初学者或者刚刚接触ASP的朋友不一定完全看得明白,偶就整理了一下编写一个ASP类的方法,大部分是从网上找来的.希望对朋友们有帮助. <ASP设计模式>(作者 我佛山人): 首先ASP的类是由事件和方法(它们就是构成类的成员了)构成的,如果大家还没有接触过,可以先看看下面的说明:在 Class 块中,成员通过相应的声明语句被声明为 Private(私有成员,只能在类内部调用) 或 Public(公有成员,可以在类内外部调用) .被声明

怎样从一个DLL中导出一个C++类

原文作者:Alex Blekhman    翻译:朱金灿   原文来源: http://www.codeproject.com/KB/cpp/howto_export_cpp_classes.aspx 译文来源:http://blog.csdn.net/clever101     C++语言毕竟能和Windows DLLs能够和平共处.     介绍          自从Windows的开始阶段动态链接库(DLL)就是Windows平台的一个组成部分.动态链接库允许在一个独立的模块中封装一系列

如何编写一个ASP类_ASP CLASS类

前几天大佛写了"ASP设计模式",可能有些初学者或者刚刚接触ASP的朋友不一定完全看得明白,偶就整理了一下编写一个ASP类的方法,大部分是从网上找来的.希望对朋友们有帮助.  <ASP设计模式>(作者 我佛山人): dispbbs.asp?boardID=20&ID=247879 首先ASP的类是由事件和方法(它们就是构成类的成员了)构成的,如果大家还没有接触过,可以先看看下面的说明: 在 Class 块中,成员通过相应的声明语句被声明为 Private(私有成员,

一个Ajax类

ajax 一个Ajax类function Ajax(url,recvT,stringS,resultF) {    this.url = url;    this.stringS = stringS;    this.xmlHttp = this.createXMLHttpRequest();    if (this.xmlHttp == null) {        alert("erro");        return;    }    var objxml = this.xml

jsp或者说JAVA倒底有多快?这里有一个计时类,可以帮你的忙。同时支持JAVA和JSP。内有例子。

js //作者:sonymusic//原载于豆腐技术站(www.asp888.net)package sony.utils; import java.util.*;import javax.servlet.jsp.*;/*** 一个计时类.* 创建日期:(2000-11-6 13:09:38)* 作者:SonyMusic(sonymusic@china.com)*/public class Timing{private ArrayList nameArray=new ArrayList();pr

Flash制作的一个3D类效果

刚从Macromedia官方网站上淘到一个3D类,不知道以前发过没有.觉得还不错,就发上来了.有兴趣的朋友可以下载看看. 点击这里下载源文件 自己写了一篇教程,对自学有一些帮助 Macromedia 3Dclasses1.添加场景SceneName:Scene= new Scene();属性:clip:MovieClip 绘制物体的影片剪辑f:Number 焦距比数,过小会使物体变形,默认为300(最自然)quaternion:Quaternion 四元数,默认为0,0,0,1(处理旋转用)方法

在定义一个js类的时候,为什么要设置该类的prototype属性为它所要继承的类的

在定义一个js类的时候,为什么要设置该类的prototype属性为它所要继承的类的实例对象 在写JavaScript类定义的时候,大家很可能都写过下面的代码: function A() {} function B() {} B.prototype = new A() 上面这样写是为了让instanceof语句能起作用.举个例子: 1.不重写子类的prototype属性 b = new B(); b instanceof B //return true b instanceof A // retu

使用libzplay库封装一个音频类

装载请说明原地址,谢谢~~      前两天我已经封装好一个duilib中使用的webkit内核的浏览器控件和一个基于vlc的用于播放视频的视频控件,这两个控件可以分别用在放酷狗播放器的乐库功能和MV功能上,也可以用于其他duilib项目,说起来做仿酷狗程序,但是至今我虽然把仿酷狗的主界面做好了,但是还没有播放音乐的功能,所以今天就再封装一个音频类.    我以前并不怎么使用音频和视频的功能,所以对常用的视频库和视频库不太了解,而我肯定不会使用系统的win32控件或者MFC里面的类,因为多数系统