How to create separate class file?

Hi

I'm just starting to learn C++ from various web sources but have come a bit unstuck. I have the below code which works fine but I'd ideally like to separate the CD class code into a separate file and then call it from within my code.

How do I go about this? Do I need to create a header file or separate source code file? And how do I refer to the class from within my main code?




#include <iostream>
#include <string>

using namespace std;

class CD
{
      public:
            CD();    // Default Constructor
            CD(string a, string t, int n, int y):artist(a), title(t), noOfTracks(n), year(y) {} //Constructor with values

            void setArtist(string newArtist)
            {
                  artist = newArtist;
            }
            string getArtist()
            {
                  return artist;
            }
            
            void setTitle(string newTitle)
            {
                  title = newTitle;
            }
            string getTitle()
            {
                  return title;
            }

            void setNoOfTracks(int newNoOfTracks)
            {
                  noOfTracks = newNoOfTracks;
            }
            int getNoOfTracks()
            {
                  return noOfTracks;
            }
            
            void setYear(int newYear)
            {
                  year = newYear;
            }
            int getYear()
            {
                  return year;
            }

      private:
            string artist;
            string title;
            int noOfTracks;
            int year;
};

void main()
{
      //define input holders
      string inputArtist;
      string inputTitle;
      int inputNoOfTracks;
      int inputYear;

      //get input for first class
      cout<<"Favourite album \n \n";

      cout<<"\n Please enter name of artist: ";
      getline(cin, inputArtist);

      cout<<"\n Please enter title of album : ";
      getline(cin, inputTitle);

      cout<<"\n Please enter number of tracks : ";
      cin>>inputNoOfTracks;

      cout<<"\n Please enter year of album : ";
      cin>>inputYear;
      cin.ignore(); //need to find out why we need this one!!!

      //create class with values
      CD myFavouriteCD(inputArtist, inputTitle, inputNoOfTracks, inputYear);

      //get input for second class
      cout<<"\n \n Worst album \n \n";

      cout<<"\n Please enter name of artist: ";
      getline(cin, inputArtist);

      cout<<"\n Please enter title of album : ";
      getline(cin, inputTitle);

      cout<<"\n Please enter number of tracks : ";
      cin>>inputNoOfTracks;

      cout<<"\n Please enter year of album : ";
      cin>>inputYear;

      //create class with values
      CD myWorstCD(inputArtist, inputTitle, inputNoOfTracks, inputYear);

      //print out values of first class
      cout<<"\n \n Favourite album";

      cout<<"\n Artist : "<<myFavouriteCD.getArtist();
      cout<<"\n Title : "<<myFavouriteCD.getTitle();
      cout<<"\n Number of tracks : "<<myFavouriteCD.getNoOfTracks();
      cout<<"\n Year : "<<myFavouriteCD.getYear();

      //print out values of second class
      cout<<"\n \n Worst album";

      cout<<"\n Artist : "<<myWorstCD.getArtist();
      cout<<"\n Title : "<<myWorstCD.getTitle();
      cout<<"\n Number of tracks : "<<myWorstCD.getNoOfTracks();
      cout<<"\n Year : "<<myWorstCD.getYear()<<"\n";
}



Thanks!
UzibatAsked:
Who is Participating?
 
rbvoigtConnect With a Mentor Commented:
Create a header file CD with the following code

#include <string>

using namespace std;

class CD
{
     public:
          CD();    // Default Constructor
//snip
          int year;
};

Now find and replace string with std::string and kill the using namespace line.  It's evil to import namespaces in a header file.

To use in your application, use
#include "CD"
along with your other includes.
0
 
UzibatAuthor Commented:
Done exactly what you said but now I'm getting the following error when I compile...

"Cannot open include file: 'CD': No such file or directory"

Also, how comes you use speechmarks for including the CD header file and not <> as standard?
0
 
UzibatAuthor Commented:
Aha - if I change #include "CD" to #include "CD.h", it all works.

Thanks!


0
 
rbvoigtCommented:
Quotes are used for user header files, the compiler will look in the current directory first.  Angle brackets cause the compiler to look first in the system include directory.

Actually, there's a big movement to get rid of the .h on header files in C++.
<iostream.h> changed to <iostream>
<stdio.h> changed to <cstdio>
That's why I said to name the header CD rather than CD.h.  It doesn't matter though, as long as the #include and filename match.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.