Uzibat
asked on
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.getNoOfTr acks();
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!
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,
//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.getNoOfTr
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()<<"\
}
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Aha - if I change #include "CD" to #include "CD.h", it all works.
Thanks!
Thanks!
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.
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.
ASKER
"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?