Class vs Structs
The only difference between a
class
and astruct
in C++ is thatstructs
have defaultpublic
members and bases and classes have defaultprivate
members and bases. Both classes and structs can have a mixture ofpublic
,protected
andprivate
members, can use inheritance, and can have member functions.
I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with
private
data and member functions.
// example about structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct movies_t {
string title;
int year;
} mine, yours;
void printmovie (movies_t movie);
int main ()
{
string mystr;
mine.title = "2001 A Space Odyssey";
mine.year = 1968;
cout << "Enter title: ";
getline (cin,yours.title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> yours.year;
cout << "My favorite movie is:\n ";
printmovie (mine);
cout << "And yours is:\n ";
printmovie (yours);
return 0;
}