Hi guys I am trying to do my homework and I've been stuck with some constructors. I am supposed to do this:
Write a program that uses a class named MovieData to store the
following information about a movie:
Title, Director, Year Released, Running Time (in minutes).
Include a constructor that allows all four of these member data
values to be specified at the time a MovieData variable is created.
The program should create two MovieData variables.
Have a method that displays the information about the movie in a clearly formatted manner.
Now include two additional attributes that hold the movie's production costs and first-year revenues.
Add a second constructor so that all six member values can be
specified when a MovieData variable is created.
Copy the method that displays the movie data to create a second method that displays the title, director, release year, running time, and first year's profit or loss.
So far I have this done:
// 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;
}
I know that I don't have my second constructor, and this is my problem. I have no idea how to declare it; I've been reading my book, but I am having a lot of trouble, because it does not say anything about it. It'd be great if you someone could guide me a little bit
You already have one constructor. Simply do the exact same thing with the other constructor, just with different parameters, like the requirements say to.
And you need to change your displayMovieInfo() standalone function into a method of the class, like the requirements say to.
Try this:
// 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;
}
Related
#include<iostream>
#include<string>
using namespace std;
class Item{
private:
string type;
string abbrv;
string uID;
int aircraft;
double weight;
string destination;
public:
void print(){
cout << "ULD: " << type << endl;
cout << "Abbreviation: " << abbrv << endl;
cout << "ULD-ID: " << uID << endl;
cout << "Aircraft: " << aircraft << endl;
cout << "Weight: " << weight << " Kilograms" << endl;
cout << "Destination: " << destination << endl;
}
friend void kilotopound(Item);
};
void kilotopound(Item I){
cout << "Weight in Pounds: " << I.weight * 2.2 << " LBS " << endl;
}
int main(){
Item I;
I.type = "Container";
I.uID = "AYK68943IB";
I.abbrv = "AYK";
I.aircraft = 737;
I.weight = 1654;
I.destination = "PDX";
I.print();
kilotopound(I);
return 0;
}
Starting on line 31 I'm getting the error 'std::__cxxll::string Item::type' is private within this context
I'm basically trying to make the data private from this code
class Item{
public:
string type;
string abbrv;
string uID;
int aircraft;
double weight;
string destination;
void print(){
cout << "ULD: " << type << endl;
cout << "Abbreviation: " << abbrv << endl;
cout << "ULD-ID: " << uID << endl;
cout << "Aircraft: " << aircraft << endl;
cout << "Weight: " << weight << " Kilograms" << endl;
cout << "Destination: " << destination << endl;
}
friend void kilotopound(Item);
};
void kilotopound(Item I){
cout << "Weight in Pounds: " << I.weight * 2.2 << " LBS " << endl;
}
int main(){
Item I;
I.type = "Container";
I.uID = "AYK68943IB";
I.abbrv = "AYK";
I.aircraft = 737;
I.weight = 1654;
I.destination = "PDX";
I.print();
kilotopound(I);
return 0;
}
Any help would be greatly appreciated, I'm just sort of lost on how I can resolve the error. Thanks!
Also I need to be able to copy and output the copied data once again if anyone can help with that as well, with private data too. Thanks again!
#include<iostream>
#include<string>
using namespace std;
class Item{
private:
string type;
string abbrv;
string uID;
int aircraft;
double weight;
string destination;
public:
Item(string t, string a, string u, int aC, double w, string d){
type = t;
abbrv = a;
uID = u;
aircraft = aC;
weight = w;
destination = d;
}
void print() {
cout << "ULD: " << type << endl;
cout << "Abbreviation: " << abbrv << endl;
cout << "ULD-ID: " << uID << endl;
cout << "Aircraft: " << aircraft << endl;
cout << "Weight: " << weight << " Kilograms" << endl;
cout << "Destination: " << destination << endl;
}
friend void kilotopound(Item);
};
void kilotopound(Item I){
cout << "Weight in Pounds: " << I.weight * 2.2 << " LBS " << endl;
}
int main(){
Item I ("Container", "AYK68943IB", "AYK", 737, 1654, "PDX");
I.print();
kilotopound(I);
return 0;
}
I have been trying to search the array for a specific car (array) based on the choice between: Make, Model, Year or number of Cylinders and display the items retrieved by the search on the screen from here.
My data in the text file presented here was passed already into arrays and printed as:
Volkswagen
Jetta
2016
4
Jeep
Wrangler
2008
6
Toyota
Corolla
1999
4
Chevy
Malibu
2018
6
Chevy
Malibu
2014
6
BMW
320i
2014
6
Jeep
GrandCharokeeLaredo
2006
6
Ford
Fusion
2012
4
Chevy
Equinox
2015
4
Volkswagen
Jetta
1998
4
My problem is that I do not know how to transfer from a user input (ex: "Volkswagen") from a menu and display the arrays with that characteristic.
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <math.h>
using namespace std;
class Cars
{
private:
// Private variables of cars
string make;
string model;
int year;
int cylinders;
public:
// Function within the objects
void setMake(string m) { make = m; }
string getMake() { return make; }
void setModel(string mo) { model = mo; }
string getModel() { return model; }
void setYear(int y) { year = y; }
int getYear() { return year; }
void setcylinders(int c) { cylinders = c; }
int getCylinders() { return cylinders; }
};
int main()
{
//Opening the file
ifstream inputFile;
inputFile.open("cars.txt");
// Variables for car characteristics
string make;
string model;
int year;
int cylinders;
string inMa;
string inMo;
int inYe;
int inCy;
// Object array
Cars car[10];
cout << " Display of available automobiles: " << endl;
cout << endl;
// Reading from file and passing into array
for (int x = 0; x < 10; x++)
if (inputFile >> make >> model >> year >> cylinders)
{
car[x].setMake(make);
car[x].setModel(model);
car[x].setYear(year);
car[x].setcylinders(cylinders);
cout << right << setw(7) << " Car #" << x << " " << endl;
cout << "Maker: " << make << endl;
cout << "Model: " << model << endl;
cout << "Year made: " << year << endl;
cout << "Number of cylinders: " << cylinders << endl;
cout << endl;
}
inputFile.close();
int selection;
cout << " Selection of automobiles:" << endl;
cout << " Select an option to describe your car/cars. " << endl;
cout << " ----------------------------" << endl;
cout << " 1. Maker of Car: " << endl;
cout << " 2. Model of Car: " << endl;
cout << " 3. Year of the Car made: " << endl;
cout << " 4. Number of cylinders: " << endl;
cin >> selection;
if (selection == 1)
{
cout << " Who is the maker of the car? " << endl;
// Need help displaying array that contain a specific variable
// Ex: All arrays that contain "Jeep"
}
else if (selection ==2)
{
cout << " What is the model of the car? " << endl;
// Need help displaying array that contain a specific variable
}
else if (selection ==3)
{
cout << " What was the year the car was made? " << endl;
// Need help displaying array that contain a specific variable
}
else if (selection ==4)
{
cout << " How many cylinders does the car have?" << endl;
// Need help displaying array that contain a specific variable
}
else
{
cout << "Enter a valid response" << endl;
}
system("pause");
return 0;
}
How can I match arrays with the user input and output it the user based on the object arrays? I am not worried if the user misinputs the variable during "cin".
So whenever I post the following code I get the wrong responses,
The class member variables do get updated with my setters, but for some reason the final cost is incorrect. I am not allowed to change the main.cpp codes
I am thinking that it might be something with the setters but I can't figure out how to force the setters to invoke the constructor so the setTotalCost() can get invoked
main.cpp
#include <iostream>
#include <iomanip>
#include "Inventory.h"
using namespace std;
int main()
{
cout << fixed
<< showpoint
<< setprecision(2);
// Demonstrate the default constructor
Inventory stockItem1;
cout << "\nDemonstrating the default constructor...\n";
cout << "Item number: " << stockItem1.getItemNumber() << endl;
cout << "Quantity : " << stockItem1.getQuantity() << endl;
cout << "Cost : " << stockItem1.getCost() << endl;
cout << "Total Cost : " << stockItem1.getTotalCost() << endl;
// Now demonstrate the overloaded constructor
Inventory stockItem2(124, 12, 84.95);
cout << "\nDemonstrating the overloaded constructor...\n";
cout << "Item number: " << stockItem2.getItemNumber() << endl;
cout << "Quantity : " << stockItem2.getQuantity() << endl;
cout << "Cost : " << stockItem2.getCost() << endl;
cout << "Total Cost : " << stockItem2.getTotalCost() << endl;
// Now demonstrate the member "set" functions
stockItem2.setItemNumber(243);
stockItem2.setQuantity(50);
stockItem2.setCost(9.50);
cout << "\nDemonstrating the \"set\" functions...\n";
cout << "Item number: " << stockItem2.getItemNumber() << endl;
cout << "Quantity : " << stockItem2.getQuantity() << endl;
cout << "Cost : " << stockItem2.getCost() << endl;
cout << "Total Cost : " << stockItem2.getTotalCost() << endl;
// Now demonstrate the input validation functions
cout << "\nDemonstrating the input validation functions...\n";
stockItem2.setItemNumber(-1);
stockItem2.setQuantity(-1);
stockItem2.setCost(-1);
cout << "\nItem number: " << stockItem2.getItemNumber() << endl;
cout << "Quantity : " << stockItem2.getQuantity() << endl;
cout << "Cost : " << stockItem2.getCost() << endl;
cout << "Total Cost : " << stockItem2.getTotalCost() << endl;
return 0;
}
Inventory.h
//Header File
#include<iostream>
#ifndef INVENTORY_H
#define INVENTORY_H
class Inventory
{
private:
int itemNumber;
int quantity;
double cost;
double totalCost;
public:
// default constructor, setting all values to 0
Inventory();
Inventory(int, int, double);
void setItemNumber(int );
void setQuantity(int );
void setCost(double );
void setTotalCost();
int getItemNumber();
int getQuantity();
double getCost();
double getTotalCost();
};
#endif //PROGRAM6_INVENTORY_H
Inventory.cpp
#include <iostream>
#include "Inventory.h"
using namespace std;
Inventory :: Inventory()
{
itemNumber = 0;
quantity = 0;
cost = 0;
totalCost = 0;
}
Inventory ::Inventory(int itemNumber, int quantity, double cost)
{
setItemNumber(itemNumber);
setQuantity(quantity);
setCost(cost);
setTotalCost();
}
void Inventory ::setItemNumber(int theItemNumber)
{
if (theItemNumber > 0)
{
itemNumber = theItemNumber;
}
else
{
cout << "You entered " << theItemNumber << " as your item number. Only positive numbers are accepted"<<endl;
}
}
void Inventory ::setQuantity(int quantityOfItems)
{
cout << "Setting quantity to " << quantityOfItems << endl;
if (quantityOfItems > 0)
{
quantity = quantityOfItems;
}
else
{
cout << "You entered " << quantityOfItems << " as your item quantity. Only positive numbers are accepted"
<< endl;
}
}
void Inventory ::setCost(double costOfItems)
{
if (costOfItems > 0)
{
cost = costOfItems;
}
else
{
cout << "You entered " << costOfItems << " item cost. Only positive numbers are accepted" << endl;
exit(0);
}
}
void Inventory ::setTotalCost()
{
int itemCount = getQuantity();
double itemCost = getCost();
totalCost = itemCost * itemCount;
}
int Inventory ::getItemNumber()
{
return itemNumber;
}
int Inventory ::getQuantity()
{
return quantity;
}
double Inventory ::getCost()
{
return cost;
}
double Inventory ::getTotalCost()
{
return totalCost;
}
I get the following output:
Demonstrating the default constructor...
Item number: 0
Quantity : 0
Cost : 0.00
Total Cost : 0.00
Demonstrating the overloaded constructor...
Item number: 124
Quantity : 12
Cost : 84.95
Total Cost : 1019.40
Demonstrating the "set" functions...
Item number: 243
Quantity : 50
Cost : 9.50
Total Cost : 1019.40
Demonstrating the input validation functions...
You entered -1 as your item number. Only positive numbers are accepted
You entered -1 as your item quantity. Only positive numbers are accepted
You entered -1.00 item cost. Only positive numbers are accepted
Process finished with exit code 0
for what I see, you do not call setTotalCost() after change the other variable. It will not happen automatically.
Hope it helps.
So, you need to make it so getTotalCost() returns the correct value without a setTotalCost() being explicitly called after the inputs to that value are updated. There are two ways that you can do this - you can have the setters for the inputs call that setter, or you can not store the value totalCost at all and recalculate every time you call getTotalCost().
Hi im creating a cinema ticket operator as a self short project but i am a bit stuck on how to make the results of what the user inputs show up at the end summed up like on a receipt. Thanks in advance.
This is my code :
#include <iostream>
#include <string>
using namespace std;
int selectStaff();
int selectDate();
int selectMovie();
int printReceipt(int a);
string name, date;
int main()
{
int movie;
selectStaff();
date = selectDate();
movie = selectMovie();
printReceipt(movie);
return 0;
}
int printReceipt(int a)
{
if (a == 1) {
cout << "Deadpool" << endl;
}
else {
cout << "Goosebumps" << endl;
}
return 0;
}
int selectStaff() {
cout << "Welcome to FILM Screen Cinema" << endl;
cout << endl;
cout << "ENTER STAFF/OPERATOR'S NAME: " << endl;
cin >> name;
return 0;
}
int selectDate() {
cout << endl;
cout << "ENTER DATE:";
cin >> date;
return 0;
}
int selectMovie() {
int movie;
cout << endl;
cout << "CHOOSE A MOVIE THAT IS SCREENING TODAY:" << endl;
cout << endl;
cout << "Press 1 for first option & 2 for second option" << endl;
cout << endl;
cout << "[1] Deadpool" << endl << "[2] Goosebumps" << endl;
cin >> movie;
return 0;
}
I want all the required input to be displayed at the bottom so when it prints, the user can view the summary. I also need a closing message that says "thank you"
You have all of the variables lined up to do this neatly and simply. Just edit your print receipt function to look something like:
Int printreceipt(int a) {
string title;
if (a==1) {
title = "Deadpool";
} else {
title == "Goosebumps";
}
cout << "Cashier: "; cout << name << endl << endl; //say the operator's name and add a line
cout << "Date: " << date << endl; //say the date
cout << "Movie: " << title; //say the title of the movie
cout << endl << endl; //add a line
cout << "Thank you for choosing this cinema! Enjoy!"; //Thank you message :D
}
To give you results similar to:
Cashier: Guy
Date: May 5, 2017
Movie: Deadpool
Thank you for choosing this cinema! Enjoy!
I need this program to create a new HotDogStand object that is able to track how many hot dogs are sold by each stand individually and all together, and I cannot figure out how to make my static method work to find the total number of hot dogs sold between all stands. Can someone point me in the right direction please?
#include <iostream>
using namespace std;
class HotDogStand
{
public:
HotDogStand(int id, int hds);
void justSold();
int getNumSold();
int getID();
int getTotalSold();
private:
int idNum;
int hotDogsSold;
static int totalSold;
};
HotDogStand::HotDogStand(int id, int hds)
{
idNum = id;
hotDogsSold = hds;
return;
}
void HotDogStand::justSold()
{
hotDogsSold++;
return;
}
int HotDogStand::getNumSold()
{
return hotDogsSold;
}
int HotDogStand::getID()
{
return idNum;
}
int HotDogStand::getTotalSold()
{
totalSold = 0;
totalSold += hotDogsSold;
}
int main()
{
HotDogStand s1(1, 0), s2(2, 0), s3(3, 0);
s1.justSold();
s2.justSold();
s1.justSold();
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << "." << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << "." << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << "." << endl;
cout << "Total sold = " << s1.getTotalSold() << endl;
cout << endl;
s3.justSold();
s1.justSold();
cout << "Stand " << s1.getID() << " sold " << s1.getNumSold() << "." << endl;
cout << "Stand " << s2.getID() << " sold " << s2.getNumSold() << "." << endl;
cout << "Stand " << s3.getID() << " sold " << s3.getNumSold() << "." << endl;
cout << "Total sold = " << s1.getTotalSold() << endl;
}
Globally (outside of the class), you have to define the static variable:
int HotDogStand::totalSold = 0;
Change
void HotDogStand::justSold()
{
hotDogsSold++;
totalSold++; // increment here
return;
}
And
int HotDogStand::getTotalSold()
{
return totalSold; // just return value
}