c ++使用第二个构造函数

时间:2015-10-08 03:50:03

标签: c++

大家好我正在尝试做功课,而且我一直坚持使用一些构造函数。我应该这样做:

  • 编写一个程序,该程序使用名为MovieData的类来存储 以下有关电影的信息:
  • 标题,导演,发行年份,运行时间(以分钟为单位)。
  • 包含允许所有这四个成员数据的构造函数 在创建MovieData变量时指定的值。
  • 程序应该创建两个MovieData变量。
  • 有一种方法可以以清晰的格式显示有关电影的信息。
  • 现在包含两个额外的属性,用于保存电影的制作成本和第一年的收入。
  • 添加第二个构造函数,以便所有六个成员值都可以 在创建MovieData变量时指定。
  • 复制显示电影数据的方法,以创建第二种方法,显示标题,导演,发行年份,运行时间和第一年的利润或损失。

到目前为止,我已经完成了这项工作:

// ch7movie
// By Kevin Mok

#include <iostream>
#include <string>

using namespace std;

class MovieData

{

    public:
        string title;
        string director;
        int yearReleased;
        int runningTime;
        double productionCost;
        double firstYearRevenue;

        MovieData(string titl, string dir, int yr, int rT, double pC, double fyR); // constructor

};

//Defining constructor

MovieData::MovieData(string titl, string dir, int yr, int rT, double pC, double fyR)
{
    title = titl;
    director = dir;
    yearReleased = yr;
    runningTime = rT;
    productionCost = pC;
    firstYearRevenue = fyR;
}

//Function of movie info
void displayMovieInfo(MovieData movieName)
{
    cout << "----------------------------" << endl;
    cout << "Title: " << movieName.title << endl;
    cout << "Director: " << movieName.director << endl;
    cout << "Year Released: " << movieName.yearReleased << endl;
    cout << "Running Time: " << movieName.runningTime << " minutes" << endl;
    cout << "Production Cost: $" << movieName.productionCost << endl;
    cout << "First Year's Profit': $" << movieName.firstYearRevenue - movieName.productionCost << endl;
    cout << "----------------------------\n" << endl;
}

int main()
{
    MovieData movie1("C++ in English", "JohnCna", 2010, 120, 36500, 50000);
    MovieData movie2("Great Programmer", "RKO", 2013, 130, 36500, 100000);

    displayMovieInfo(movie1);
    displayMovieInfo(movie2);

    return 0;

}

我知道我没有第二个构造函数,这是我的问题。我不知道如何宣布它;我一直在读我的书,但我遇到了很多麻烦,因为它没有说什么。如果你有人能引导我一点,那就太好了

1 个答案:

答案 0 :(得分:0)

您已经有一个构造函数。只需使用其他构造函数完成相同的操作,只需使用不同的参数,例如要求说明。

您需要将displayMovieInfo()独立函数更改为类的方法,就像要求说的那样。

试试这个:

// ch7movie
// By Kevin Mok

#include <iostream>
#include <iomanip> 
#include <string>

using namespace std;

class MovieData
{    
public:
    string title;
    string director;
    int yearReleased;
    int runningTime;
    double productionCost;
    double firstYearRevenue;

    // constructors
    MovieData(string titl, string dir, int yr, int rT);
    MovieData(string titl, string dir, int yr, int rT, double pC, double fyR);

    void displayMovieInfo();
    void displayMovieInfoAndCosts();
};

//Defining constructors

MovieData::MovieData(string titl, string dir, int yr, int rT)
{
    title = titl;
    director = dir;
    yearReleased = yr;
    runningTime = rT;
    productionCost = 0.0;
    firstYearRevenue = 0.0;
}

MovieData::MovieData(string titl, string dir, int yr, int rT, double pC, double fyR)
{
    title = titl;
    director = dir;
    yearReleased = yr;
    runningTime = rT;
    productionCost = pC;
    firstYearRevenue = fyR;
}

//Methods to display movie info

void MovieData::displayMovieInfo()
{
    cout << "----------------------------" << endl;
    cout << "Title: " << title << endl;
    cout << "Director: " << director << endl;
    cout << "Year Released: " << yearReleased << endl;
    cout << "Running Time: " << runningTime << " minutes" << endl;
    cout << "----------------------------\n" << endl;
}

void MovieData::displayMovieInfoAndCosts()
{
    cout << "----------------------------" << endl;
    cout << "Title: " << title << endl;
    cout << "Director: " << director << endl;
    cout << "Year Released: " << yearReleased << endl;
    cout << "Running Time: " << runningTime << " minutes" << endl;
    cout << "Production Cost: $" << setprecision(2) << productionCost << endl;
    cout << "First Year's Profit': $" << setprecision(2) << firstYearRevenue - productionCost << endl;
    cout << "----------------------------\n" << endl;
}

int main()
{
    MovieData movie1("C++ in English", "JohnCna", 2010, 120, 36500, 50000);
    MovieData movie2("Great Programmer", "RKO", 2013, 130, 36500, 100000);

    movie1.displayMovieInfo();
    movie1.displayMovieInfoAndCosts();

    movie2.displayMovieInfo();
    movie2.displayMovieInfoAndCosts();

    return 0;    
}