Question:
Need help with c++ object oriented programming.Structs and function prototypes?
System
2011-02-22 23:34:21 UTC
This is my program. This is what I have so far, but I'm stuck about halfway down where code needs to be entered in the function prototype. What else needs to be done to display the proper output?

/*Write a program that uses a structure named MovieData to store the
following information about a movie:
Title
Director
Year Released
Running time (in minutes)
Include a constructor that allows all 4 of these member data values
to be specified at the time a MovieData variable is created. The program
should create two MovieData variables and pass each one, in turn, to a
function that displays the information about the movie in a clearly
formatted manner.
*/

#include
#include
#include
#include
using namespace std;

//struct of MovieData
struct MovieData
{
string title,
director;
int year,
run_time;


// Default constructor (not used)
MovieData()
{ title = "";
director = "";
year = 2000;
run_time = 120;
}
};
// Constructor
MovieData movie1, movie2;

// Function prototype to displayMovie(MovieData)
void displayMovie(MovieData m)
{
//Enter code here
cout <<
}


int main()
{
MovieData movie1("War of the Worlds", "Byron Haskin", 1953, 88),
movie2("War of the Worlds", "Stephen Spielberg", 2005, 118);

displayMovie(movie1);
displayMovie(movie2);

return 0;
}
Four answers:
?
2011-02-23 05:47:15 UTC
Why write a default constructor if it's not used? What's the physical meaning of the "default movie"?

Write the true constructor only.



To display the output you need to output each field of the structure in some "clearly formatted manner", here's one:



#include

#include

struct MovieData

{

     std::string title, director;

     int year, run_time;

     MovieData(const std::string& t, const std::string& d, int y, int r) :

         title(t), director(d), year(y), run_time(r) {}

};

void displayMovie(MovieData m)

{

     std::cout << m.title << " (" << m.year << ") by " << m.director << " run time " << m.run_time << " min.\n";

}

int main()

{

     MovieData movie1("War of the Worlds", "Byron Haskin", 1953, 88);

     MovieData movie2("War of the Worlds", "Stephen Spielberg", 2005, 118);

     displayMovie(movie1);

     displayMovie(movie2);

}
cabriales
2016-10-27 01:19:47 UTC
certain, your pal is misguided: C++ has specific language-aspect help for class-depending merchandise-oriented programming. As already stated, it also helps quite a few different sorts of programming as well (maximum fantastically, widespread programming) Your pal's position would nicely be on the topic of a minority college of theory that a "authentic" merchandise-oriented language could impose a finished merchandise hierarchy, which C++ does no longer have, yet C++ depending its merchandise-oriented sort on Simula, the first OO language, which presented products as a "sort of a actual international". A actual merchandise isn't inevitably on the topic of each and each and every unmarried actual merchandise in the Universe.
anonymous
2011-02-22 23:39:36 UTC
Constructor

A member function with the same name as the class.

It initializes the data members of a class object.

Called automatically when an object of that class is created.

There can be many constructors accomplished through function overloading.

Specifying a return type or a return value for a constructor is a syntax error.

Default Constructor is a constructor with no parameters

The default constructor takes no parameters, whether you declare it or get it free from

the compiler

If you declare any constructor, the compiler will not provide a default constructor

Once the class has been defined, it can be used as a type in object, array, pointer or a

reference.

Time sunset, arrayoftimes[5], *ptrToTime,&dTime=sunset------- C++ is extensible



Data members may be private as they may be of no concern to class’ clients. Thus, implementation is hidden and this information hiding promotes program modifiability and simplifies the clients’ perception of a class.



Member functions are usually shorter than non-object oriented programs because the data stored in data members have ideally been validated by a constructor and/or by member functions that store new data. Because the data are already in the object, the member function calls often have no arguments or atleast few arguments than typical function calls in non object oriented languages. Thus, the calls are shorter, function definitions are shorter and function prototypes are shorter.

Attempting to initialize a data member of a class explicitly in a class definition is a syntax

error.



Destructor

A member function the same name as the class and with prefix ~ i.e. ~className( );

If no destructor is specified then the system ‘plugs-in’ one destructor.



The destructor does ‘termination housekeeping’ on each class object before the memory

for the object is reclaimed by the system.

Destructors cannot take arguments and hence, cannot be overloaded.

Destructors have no return value.



Declaring member functions inside a class definition (via their function prototypes) & defining those member functions outside the class definition separates the interface from of a class from its implementation. This promotes good Software Engineering as it hides implementation from class’ clients and also class’ need not recompile if implementation changes; provided interface remains same. Member functions declared outside in a class definition can be defined outside class definition using binary scope resolution operator ::. If member function is defined outside then use of class name and :: is mandatory as it ties function to a class and two classes may have functions with same name. However, scope of such functions is that of class only.
travelingblueeye
2011-02-22 23:38:15 UTC
Your 'int main' only specifies one instance, but it looks like two instances should be created.



MAIN is the code that is supposed to run when you get everything right, you need to re-evaluate your code structure.



Jone Te is not wrong, but he didn't post anything useful.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...