在main中访问类数组

时间:2014-03-15 17:01:28

标签: c++ arrays

所以我正在为大学做一个电影预订项目,但我们还没有涵盖继承。 我想在main中访问我的数组,它是在一个单独的类中。我希望在满足一个案例时访问该数组。

主要

#include <iostream>
#include <string>
#include "cinema.cpp"
#include "screen.cpp"
#include "screen.cpp"
using namespace std;

void main()
{
    int loop=1;
    string Film;
    int inputFilm;
    while(loop==1)
    {
        cout << "Please select film \n ";
        cout<<"\n\n";
        cout << " 1: Godzilla  [Screen 1] \n 2: Waking Life  [Screen 2] \n 3: Saving Private Ryan  [Screen 3] \n 4: LOTR  [Screen 4] \n 5: The Departed  [Screen 5] \n 6: Knocked Up  [Screen 6] \n";

        cout << "\n Enter a screen number";
        cin >> inputFilm;
        switch(inputFilm)
        {
        case 1: 
            Film = "Godzilla";
            cout <<"You have chosen: "+Film;
            cout << "\n";
            break;
        case 2:
            Film = "Waking Life";
            cout <<"You have chosen: "+Film;
            cout << "\n";
            break;
        case 3:
            Film = "Saving Private Ryan";
            cout <<"You have chosen: "+Film;
            cout << "\n";
            break;
        case 4:
            Film = "LOTR";
            cout <<"You have chosen: "+Film;
            cout << "\n";
            break;
        case 5:
            Film = "The Departed";
            cout <<"You have chosen: "+Film;
            cout << "\n";
            break;
        case 6:
            Film = "Knocked Up";
            cout <<"You have chosen: "+Film;
            cout << "\n";
            break;
        }
        break;
    cout << "Please eneter a seat row";
    }
    system("pause");
}

包含数组的类

#include <iostream>
#include <string>
#include "cinema.h"
using namespace std;

cinema::cinema()
{
    string ScreenArray [6];
    ScreenArray [0] = "Godzilla";
    ScreenArray [1] = "Waking Life";
    ScreenArray [2] = "Saving Private Ryan";
    ScreenArray [3] = "LOTR";
    ScreenArray [4] = "The Departed";
    ScreenArray [5] = "Knocked UP";
    address = "Golden Island Shopping Center";
}
string cinema::getAddres()const
{
    return address;
}
//int cinema::NumOfScreens()const
//{
//  return NumOfScreens;
//}

1 个答案:

答案 0 :(得分:0)

使用类可以简化操作,但是这里不需要它们(除非赋值调用它)。

Cinema构造函数中的数组是该构造函数的本地数据。

将其移至main


顺便说一下,main绝对必须具有结果类型int。 Microsoft Visual C ++允许void,他们的示例通常具有void的{​​{1}}结果类型,但这是非标准的;只有微软能够以微妙的方式提供非标准的东西才能帮助供应商锁定。除了有效地将代码限制到特定的编译器之外,它也是绝对新手的标志,一旦用main替换void,你就不再这样了。 ; - )


附录:
这是一个代码示例,用于执行我认为您到目前为止要执行的操作:

int