大家好我正在尝试做功课,而且我一直坚持使用一些构造函数。我应该这样做:
到目前为止,我已经完成了这项工作:
// 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;
}
我知道我没有第二个构造函数,这是我的问题。我不知道如何宣布它;我一直在读我的书,但我遇到了很多麻烦,因为它没有说什么。如果你有人能引导我一点,那就太好了
答案 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;
}