从main()外部的函数访问和更改类实例

时间:2016-11-21 00:29:29

标签: c++ class instance

我目前正在学习C ++,而且我遇到了类似的问题,我似乎无法找到解决方案。如果有一个特定的主题我应该介绍,它将教我如何做到让我知道,但如果这是一个错误,我想帮助解决它。

我有一个控制台应用程序,当你运行时要求你输入一个电影的名称,它发布的年份和它的评级。代码会向您询问3部电影的相同问题,然后打印结果。这只是我在遵循教程时制作的代码,因此没有任何实际意义。

我创建了一个名为“Movie”的类,它保存用户的答案和创建实例的功能,但它似乎不起作用。我将使用示例粘贴代码,以便于解释。

Movie.h

#ifndef MOVIE_H
#define MOVIE_H
#include <iostream>
#include <string>

using namespace std;

class Movie
{
    private:
        string movieName, movieRating;
        int movieYear;

    public:
        Movie();
        Movie(string, int, string);
        virtual ~Movie();

        string getName();
        string getRating();
        int getYear();

        void setName(string);
        void setRating(string);
        void setYear(int);

    protected:
};

#endif // MOVIE_H

Movie.cpp

#include "Movie.h"
#include <iostream>
#include <string>
#include <ctime>

using namespace std;

Movie::Movie()
{
    movieName = "NA";
    movieYear = 1999;
    movieRating = "NA";
}

Movie::Movie(string name, int year, string rating)
{
    movieName = name;

    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    int x = (now->tm_year + 1900);
    while(year > x || year < 1889)
    {
        cout<<"Please enter a valid year: ";
        cin>>year;
    }
    movieYear = year;

    movieRating = rating;
}

Movie::~Movie()
{

}


string Movie::getName()
{
    return movieName;
}

string Movie::getRating()
{
    return movieRating;
}

int Movie::getYear()
{
     return movieYear;
}

void Movie::setName(string n)
{
    movieName = n;
}

void Movie::setRating(string r)
{
    movieRating = r;
}

void Movie::setYear(int y)
{
    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    int x = (now->tm_year + 1900);
    while(y > x || y < 1889)
    {
        cout<<"Please enter a valid year: ";
        cin>>y;
    }
    movieYear = y;

}

的main.cpp

#include <iostream>
#include <string>
#include "Movie.h"

using namespace std;

void newMovie(Movie);
void printMovies(Movie);

int main()
{
    Movie movie1("a", 1900, "a");
    Movie movie2("a", 1900, "a");
    Movie movie3("a", 1900, "a");

    newMovie(movie1);
    //newMovie(movie2);
    //newMovie(movie3);
    printMovies(movie1);
    //printMovies(movie2);
    //printMovies(movie3);
    return 0;
}

void newMovie(Movie movie)
{

    string inName, inRating;
    int inYear;
    cout<<"Enter a movie name: ";
    cin>>inName;
    movie.setName(inName);
    cout<<"Enter the year it was released: ";
    cin>>inYear;
    movie.setYear(inYear);
    cout<<"Enter the suggested rating: ";
    cin>>inRating;
    movie.setRating(inRating);
    cout<<endl<<endl;

}

void printMovies(Movie movie)
{
    cout<<"Name: "<<movie.getName()
        <<"\nYear: "<<movie.getYear()
        <<"\nRating: "<<movie.getRating()
        <<endl<<endl;
}

现在,当我运行这个时,会发生这样的情况:电影被打印成这样:

输入电影名称:test 输入发布年份:2001年 输入建议的评级:PG 姓名:a 年份:1900年 评级:a 进程返回0(0x0)执行时间:6.912秒 按任意键继续。

似乎在newMovie(电影电影)中所做的改变并没有停留。如果我通过这样的方式在中间打印:

void newMovie(Movie movie)
{

    string inName, inRating;
    int inYear;
    cout<<"Enter a movie name: ";
    cin>>inName;
    movie.setName(inName);
    cout<<"Enter the year it was released: ";
    cin>>inYear;
    movie.setYear(inYear);
    cout<<"Enter the suggested rating: ";
    cin>>inRating;
    movie.setRating(inRating);
    cout<<endl<<endl;

    cout<<movie.getName();

}

我在打印时获得正确的名称,这意味着该课程正常。唯一可以在函数内部工作,好像它是私有的,我无法弄清楚原因。

0 个答案:

没有答案