[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 276
  • Last Modified:

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!
0
Uzibat
Asked:
Uzibat
  • 2
  • 2
1 Solution
 
rbvoigtCommented:
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now