Friday , 22 November 2024

CS304 Assignments # 4 Solution Spring 2012

Assignment:

Object Model for 4th assignment:

In this assignment you have to code/implement the below said classes in running form these classes are,

  • Music Items
  • Music Category
  • Band
  • Artist
  • Album
  • Track
  • Folk Music
  • Classic Music
  • Film Music
  • POP Music

Now for implementing these classes practically in c++ you have to define classes according to the requirements given below:

  1. Implement data members and member functions for each class
  2. Implement constructor and destructor for each class
  3. Implement setters and getters functions for each class
  4. Implement different type of relations between these classes

Sample Example:

//Topic class

class topic{

// Data members of Topic class

int ID;

char  * Title;

char discription[200];

//Public interface of topic class

public:

//Default constructor

topic(){

ID=1;

Title=NULL;

}

//parametrized constructor

topic(int id, char *title){

ID=id;

Title=new char[strlen(title)+1];

strcpy(Title,title);

}

//setter function for ID

void setId() {

int id;

cout<<“\nEnter ID: “;

cin>>id;

}

void setTitle() {

char pChar[50];

cout<<endl<<“\nEnter Title: “;

cin.getline(pChar,50);

Title=new char[strlen(pChar)+1];

strcpy(Title,pChar);

}

 

void setTitle(char * title){

Title=title;

}

void setdiscription(){

cout<<“\nplease enter discription of title”<<endl;

cin.getline(discription,200);

}

int getId(){

return ID;

}

char * getTitle(){

return Title;

}


void add(){

cout<<“\n*****************************************\n”;

cout<<“\nAdding new information in add method”;

setTitle();

setdiscription();

cout<<“\n*****************************************\n”;

}

bool select(){

cout<<“Topic selection Area:”<<endl;

int select;

if (select==1)

cout<<“\nTopic is selected”<<endl;

else

cout<<“\nTopic is not selected”<<endl;

}

int search(){

cout<<“In Search Mehtod:”<<endl;

int found=0;

if (found)

return 1;

else

return 0;

}

void View() {

cout<<“Viewing the Information”;

cout<<“\n*****************************************\n”;

cout<<“\nTitle is:”<< Title ;

cout<<endl<<“\nDescription is:”<<discription[100];

cout<<“\n*****************************************\n”;

}

void print(){

cout<<“\n*****************************************\n”;

cout<<endl<<“\nPrinting the Information:”<<endl;

cout<<“Title:”<<Title<<endl<<“Discription:”<<discription;

cout<<“\n*****************************************\n”;

}

void download(){

cout<<“\n*****************************************\n”;

cout<<“\nDownloading the information:”<<endl;

cout<<“Downloading under processing, Please wait:”<<endl;

cout<<“You are downloding the following:”<<endl<<Title<<“file”;

cout<<” and the following discription”<<endl<<discription<<“File”;

cout<<“\n*****************************************\n”;

}

void remove(){

cout<<“\n*****************************************\n”;

cout<<“\nDeleting the information”<<endl;

cout<<“\n*****************************************\n”;

delete []Title;

delete [] discription;

}

~topic(){

}

};

class SubTopic: public topic {

int SubID;

char * title;

char discription[100];

public:

void setSubID(){

int subid;

cout<<“Enter ID of Sub topic:”;

cin>>subid;

}

void setdiscription(char []){

char dChar[200];

cout<<“Enter discription of Sub topic:”<<endl;

cin.getline(dChar,200,’\n’);

}

int getSubID(){

return SubID;

}

void setTitle(){

char pChar[50];

char dChar[200];

cout<<endl<<“Enter Title : “;

cin.getline(pChar,50);

cout<<endl<<“Enter Title discription:”;

cin.getline(dChar,200);

if(strlen(dChar)>=90){

cout<<“Eligible for being a topic”<<endl;

}

else

{

cout<<“Title just contains name and no description”<<endl;

}

}

~SubTopic(){

}

};

/****************** Assignment No. 4 : Solution **********************\

#include <cstdlib>
#include <iostream>

using namespace std;

 

class Track
{
char* trackName;
int trackID;
char *lyrics;
public:
Track(){} // default constructor
Track(char* _tn, int tID, char* _lyrics) //parameterized constructor
{
setTrackName(_tn);
setTrackID(tID);
setLyrics(_lyrics);
}//end parameterized constructor
void setTrackName(char *tn) // setter function
{
if(tn != NULL)
{
trackName = new char[strlen(tn) + 1];
strcpy(trackName, tn);
}
else
trackName = NULL;
}
void setTrackID(int n) // setter function
{
if(n > 0 && n < 99999)
trackID = n;
else
trackID = -1;
}
void setLyrics(char *lyrcs) // setter function
{
if(lyrcs != NULL)
{
lyrics = new char[strlen(lyrcs) + 1];
strcpy(lyrics, lyrcs);
}
else
lyrics = NULL;
}
Track(const Track & rhs) // copy constructor
{
if(rhs.trackName != NULL)
{
trackName = new char[strlen(rhs.trackName) + 1];
strcpy(trackName,rhs.trackName);
}
else
trackName = NULL;
trackID = rhs.trackID;
if(rhs.lyrics != NULL)
{
lyrics = new char[strlen(rhs.lyrics) + 1];
strcpy(lyrics,rhs.lyrics);
}
else
lyrics = NULL;
}// end copy constructor
~Track() // destructor
{
if(trackName != NULL)
delete[] trackName;
if(lyrics != NULL)
delete[] lyrics;
}// end destructor
void playTrack()
{
cout<<“playTrack() method is invoked.”<<endl;
}
void stopTrack()
{
cout<<“stopTrack() method is invoked.”<<endl;
}
void pauseTrack()
{
cout<<“pauseTrack() method is invoked.”<<endl;
}
void downloadTrack()
{
cout<<“downloadTrack() method is invoked.”<<endl;
}
char* getTrackName()const //getter function
{
return trackName;
}
int getTrackID()const //getter function
{
return trackID;
}
char* getLyrics() //getter function
{
return lyrics;
}
}; // end class Track

 

class Album
{
int albumID;
char* albumTitle;
int year;
Track t; //Composition
public:
Album(){}// default constructor
Album(int aID, char* aT, int tn, int yr, char* tr) // parameterized constructor
{
setAlbumID(aID);
setAlbumTitle(aT);
t.setTrackID(tn); // use of Composition
setYear(yr);
t.setTrackName(tr); // use of Composition
}//end constructor
Album(const Album & rhs)//copy constructor
{
albumID = rhs.albumID;
if(rhs.albumTitle != NULL)
{
albumTitle = new char[strlen(rhs.albumTitle) + 1];
strcpy(albumTitle, rhs.albumTitle);
}
else
albumTitle = NULL;
t.setTrackID(rhs.t.getTrackID());
year = rhs.year;
t.setTrackName(rhs.t.getTrackName());
}//end copy constructor
~Album() // destructor
{
if(albumTitle != NULL)
delete[] albumTitle;
}//end destructor
void setAlbumID(int n) // setter function
{
if(n>0)
albumID = n;
else
albumID = -1;
}
void setAlbumTitle(char* a) // setter function
{
if(a != NULL)
{
albumTitle = new char[strlen(a) + 1];
strcpy(albumTitle, a);
}
else
albumTitle = NULL;
}
void setTrackNumber(int n) // setter function
{
t.setTrackID(n);
}
void setYear(int n) // setter function
{
if(n>0)
year = n;
else
year = -1;
}
char* getTrack()const
{
t.getTrackName(); //used composition
}
void viewTrack()
{
cout<<“viewTrack() method is invoked.”<<endl;
}
void viewTrackNumber()
{
cout<<t.getTrackID();
}
void viewYear()
{
cout<<year<<endl;
}
int getAlbumID()
{
return albumID;
}
char* getTitle()const
{
return albumTitle;
}
}; // end class Album

class Artist
{
char* artistName;
Album al; //composition
Track tr; //composition
public:
Artist(){} // default constructor
Artist(char* _artistName, char* _albums, char* _tracks)// parameterized constructor
{
setArtistName(_artistName);
al.setAlbumTitle(_albums);
tr.setTrackName(_tracks);
}
Artist(const Artist & rhs)// copy constructor
{
if(rhs.artistName != NULL)
{
artistName = new char[strlen(rhs.artistName) + 1];
strcpy(artistName, rhs.artistName);
}
al.setAlbumTitle(rhs.al.getTitle());
tr.setTrackName(rhs.tr.getTrackName());
}// end copy constructor
void setArtistName(char *an) // setter function
{
if(an != NULL)
{
artistName = new char[strlen(an) + 1];
strcpy(artistName, an);
}
else
artistName = NULL;
}
char* getArtistName()const
{
return artistName;
}
~Artist()//destructor
{
if(artistName != NULL)
delete[] artistName;
}//end destructor
Album * getAlbum(int albumID)
{
return &al;
}
Track * getTrack(char * trackName)
{
return &tr;
}
}; // end class Artist


class Band
{
char* bandDetails;
char* bandName;
Artist ar;//composition
public:
Band() //Default Constructor
{}
Band(char *_bandDetails, char *_bandName, char *_artist)//parameterized Constructor
{
setBandDetails(_bandDetails);
setBandName(_bandName);
ar.setArtistName(_artist);
}
void setBandDetails(char *_bandDetails) // setter function
{
if(_bandDetails != NULL)
{
bandDetails = new char[strlen(_bandDetails) + 1];
strcpy(bandDetails, _bandDetails);
}
else
bandDetails = NULL;

}
void setBandName(char *_bandName) // setter function
{
if(_bandName != NULL)
{
bandName = new char[strlen(_bandName) + 1];
strcpy(bandName, _bandName);
}
else
bandName = NULL;
}
char* getBandDetails() // getter function
{
return bandDetails;
}
char* getBandName() // getter function
{
return bandName;
}

Band * getBand(char * bandName) // getter function
{
return this;
}
Artist * getArtist(char * artistName) // getter function
{
return &ar;
}
~Band() // Destructor
{
if(bandDetails != NULL)
{
delete[] bandDetails;
}
if(bandName != NULL)
{
delete[] bandName;
}

}//end destructor
Band(const Band & rhs) // copy constructor
{
if(rhs.bandDetails != NULL)
{
bandDetails = new char[strlen(rhs.bandDetails) + 1];
strcpy(bandDetails, rhs.bandDetails);
}
if(rhs.bandName != NULL)
{
bandName = new char[strlen(rhs.bandName) + 1];
strcpy(bandName, rhs.bandName);
}
ar.setArtistName(rhs.ar.getArtistName());

}// end copy constructor
};//end class Band

 

 

 

class MusicCategory
{
protected:
Artist* _artist; //Aggregation relationship
Band* _band; //Aggregation relationship
public:
MusicCategory()//Default Constructor
{
_artist = NULL;
_band = NULL;
}
MusicCategory(char* a, char* b) //parameterized constructor
{
_artist = new Artist();
_artist->setArtistName(a);
_band = new Band(“”,b,a);

//_band->setBandName(b);

}
MusicCategory(const MusicCategory & mc)//copy constructor of MusicCategory
{
_artist->setArtistName(mc._artist->getArtistName());
_band->setBandName(mc._band->getBandName());
}//end copy constructor of MusicCategory
~MusicCategory()//destructor
{
delete _artist;
delete _band;
}
Artist * getArtist(char * artistName)const
{
return _band->getArtist(artistName);
}
Album * getAlbum(int albumID)const
{
return _artist->getAlbum(albumID);
}
Track * getTrack(char * trackID)const
{
return _artist->getTrack(trackID);
}
Band * getBand(char * bandName)const
{
return _band->getBand(bandName);
}

};//end class MusicCategory

class MusicItems
{
MusicCategory mc;// Composition relationship
public:
MusicItems(const MusicCategory & gmc) //Class Constructor
{
cout<<“\n*****************************************\n”;
cout<<“get all Music Items from DB into mc”;
cout<<“\n*****************************************\n”;

mc = gmc;
}
Album * getAlbum(int albumID)
{
Album * tempAlbum = mc.getAlbum(albumID);
return tempAlbum;
}
Artist * getArtist(char * artistName)
{
Artist * tempArtist = mc.getArtist(artistName);
return tempArtist;
}
Track * getTrack(char * trackID)
{
Track * tempTrack = mc.getTrack(trackID);
return tempTrack;
}
Band * getBand(char * bandName)
{
Band * tempBand = mc.getBand(bandName);
return tempBand;
}
};//end class MusicItems

class FolkMusic : public MusicCategory
{
public:
FolkMusic(char* ar, char *bd) : MusicCategory(ar, bd){} //constructor

};//end FolkMusic
class FilmMusic : public MusicCategory
{
public:
FilmMusic(char* ar, char *bd) : MusicCategory(ar, bd){} //constructor

};//end FilmMusic
class POPMusic : public MusicCategory
{
public:
POPMusic(char* ar, char *bd) : MusicCategory(ar, bd){} //constructor

};//end POPMusic
class ClassicMusic : public MusicCategory
{
public:
ClassicMusic(char* ar, char *bd) : MusicCategory(ar, bd){} //constructor

};//end ClassicMusic
int main(int argc, char *argv[])
{
system(“PAUSE”);
return EXIT_SUCCESS;
}

Check Also

CS304 Unsolved Final Term Paper Spring 2010

Question No: 1    ( Marks: 1 )    – Please choose one  Classes like TwoDimensionalShape and …

Leave a Reply

Your email address will not be published. Required fields are marked *

*