注册 登录
编程论坛 C++教室

求助!!各位大神帮帮小弟,谢过了!~~

dht875 发布于 2011-05-09 00:55, 535 次点击
1. “编写一个Student类,其中有id和name属性。现有要求能随时显示系统运行期间有多少个该类的对象。(提示:使用静态变量)”

这个问题到底是怎么实现的啊。。。我一直没懂题目的要求。。。

2.“编写一个类,用户使用这个类的时候只能用这个类得到一个相同的实例。(即:单例模式)”

这个问题我也不太明白他的要求。。求解释。。。

3.“编写一个Dog类,包含名字,体重,价格等信息,并且有一个成员函数用于打印这些信息,然后编写一个含有3只Dog的对象数组,遍历该数组,打印出每只Dog的信息。”

遍历该数组。。。不懂。。。

各位大神,帮帮小弟啊,求解释,求解答哇~~
5 回复
#2
hellovfp2011-05-09 13:32
嚓,又一个做作业的。
#3
棉雨2011-05-09 13:38
回复 楼主 dht875
第一题我是这么做的。
#include"iostream"
using namespace std;
class Student
{
private:
    char name[20];
    int id;
public:
    void set_info()//输入学生信息//
    {
        cout<<"please input the information of student:"<<endl;
        cin>>name;
        cin>>id;
        count++;//每输入一次,值就加一//
    }
    void out_info()//输出学生信息//
    {
        cout<<"the information of student is:"<<endl;
        cout<<name<<"  "<<id<<endl<<"the number of student is:"<<count<<endl;
    }
    static int count;//定义一个静态变量来记录学生人数//
   
};
int Student::count=0;//静态变量的初始化//
void main()
{
    int i;
    Student student[10];
    for(i=0;i<10;i++)
    {
        student[i].set_info();
        student[i].out_info();
    }
}

第三个,狗的。
#include"iostream"
using namespace std;
class Dog
{
private:
    char name[20];
    float price;
    float weight;
public:
    void Set_info()
    {
        cout<<"please input the information of dog:"<<endl;
        cin>>name;
        cin>>price;
        cin>>weight;
    }
    void out_info()
    {
        cout<<"the information of dog is:"<<endl;
        cout<<name<<" "<<price<<" "<<weight<<endl;
    }
};
void main()
{
    Dog dog[3];
    int i;
    for(i=0;i<3;i++)//用循环遍历数组,输入狗的信息//
        dog[i].Set_info();
    for(i=0;i<3;i++)//用循环遍历数组,输出狗的信息//
        dog[i].out_info();
}
您看看,我理解的对不对?
第二个单例模式,我们没学到,所以不清楚怎么写。
#4
hellovfp2011-05-09 13:49
回复 3楼 棉雨
棉雨还真是勤快,呵呵。
public class Singleton {
  private static Singleton instance = null;  
 private Singleton()
{
   //do something   
}
#5
棉雨2011-05-09 14:31
以下是引用hellovfp在2011-5-9 13:49:36的发言:

棉雨还真是勤快,呵呵。
public class Singleton {
  private static Singleton instance = null;  
 private Singleton()
{
   //do something   
}
请教一下您,这个是什么意思?
#6
linw12252011-05-09 21:59
个人觉得其实这些题目看懂书的话不难吧。
1