OOP concepts

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I am trying to implement the following program, using OOP concepts,
but I am a bit confused.

The program is to accept audio files of type mp3(version 1 and 2) and
wma. I need to store the artist of the song. This is implemented in
the audio class as public. In class Audio, I have a method which
checks the type and returns an int according to type.

I implemented two other classes(WMA and MP3) which inherit Audio.
Then implemented another two classes (V1 and V2) and inherit MP3.

The artist is to be identified in the classes V1, V2 and WMA.

A sample code is the following:

Class Audio
{
public string artist;
private string path;

public Audio(string filePath)
{
this.path = filePath;
}

public virtual int checkType()
{
if (wma) // not correct syntax
return 1;
else
return 0;
}

public string getArtist()
{
return artist;
}
} // end class


class MP3 : Audio
{
private Audio myAudio;

public MP3(Audio x)
{
this.Audio = x;
}

public override int checkType()
{
// checking from file what type is and returning data
}

}

class V1 : MP3 // V2 and WMA similar
{
private MP3 myMP3;

public MP3(MP3 x)
{
this.myMP3 = x;
}

public void setArtist()
{
// is to be set here by reading from file
// stored in the artist object found in class Audio
}
}

Now I was trying to acess as following, but not working

Audio audio = new Audio(filePath);
int x = audio.getType(); // assume returned for MP3
MP3 mp3 = new MP3(audio);
int x = MP3.getType(); // assume returned for V1
V1 v1 = new V1(mp3);
v1.setArtist();

Now I was trying to get the artist by
audio.getArist();
but surely it wont work.

Is there another way of how to implement it other than getting the
artist from v1.getArtist();


I hope someone understands my problem


Thanks in Advance
 
Hi!

Xarky wrote:

[...snip...]
The program is to accept audio files of type mp3(version 1 and 2) and
wma. I need to store the artist of the song. This is implemented in
the audio class as public. In class Audio, I have a method which
checks the type and returns an int according to type.

I implemented two other classes(WMA and MP3) which inherit Audio.
Then implemented another two classes (V1 and V2) and inherit MP3.

The artist is to be identified in the classes V1, V2 and WMA.

So you added an "artist" property to the Audio class. That's o.k.
A sample code is the following:

Class Audio
{
public string artist;
private string path;

public Audio(string filePath)
{
this.path = filePath;
}

public virtual int checkType()
{
if (wma) // not correct syntax
return 1;
else
return 0;
}

public string getArtist()
{
return artist;
}
} // end class


class MP3 : Audio
{
private Audio myAudio;

Why do you need this Audio attribute in here ?
If you'd really need a constructor with a Audio as an Argument, you can take
the argument's filePath and store it in MP3's filePath...
public MP3(Audio x)
{
this.Audio = x;
}

like this:

public MP3(Audio x)
{
this.path = x path;
}

But you can also override the Audio constructor:

public MP3(string filePath) : base(filePath)
{
}

public override int checkType()
{
// checking from file what type is and returning data
}

Why would you want to check it here ? Let V1 return a value and V2 another
one...
}

class V1 : MP3 // V2 and WMA similar
{
private MP3 myMP3;

Same as for MP3 class applies here: Why do you need a type MP3 attribute ?
public MP3(MP3 x)

This is incorrect. Must be

public V1(MP3 x)

But you will not need it...
{
this.myMP3 = x;
}

Just create a constructor like

public V1(string filePath) : base(filePath)
{
this.setArtist();
}
public void setArtist()
{
// is to be set here by reading from file
// stored in the artist object found in class Audio
}
}

Now I was trying to acess as following, but not working

Audio audio = new Audio(filePath);
int x = audio.getType(); // assume returned for MP3

Why do you create an Audio instance ? As far as I can see, you do not need
it at all...
MP3 mp3 = new MP3(audio);
int x = MP3.getType(); // assume returned for V1

Why do you create an MP3 instance ? As fas as I can see, you do not need it
at all...
V1 v1 = new V1(mp3);
v1.setArtist();

Just do

V1 myMusic = new V1(filePath);

Your new instance will use filePath as it's audio file and initialize the
artist based upon that file...

[...snip...]
Is there another way of how to implement it other than getting the
artist from v1.getArtist();

What's wrong in getting the Artist from your V1 instance ?


I hope someone understands my problem

I hope I did; if I didn't, just say so ;-)
 
The problem is that Audio class determines what type of music is.

If I do
V1 x = new V1(filePath);
filePath, may result to a WMA or a V2.
 
xarky said:
The problem is that Audio class determines what type of music is.

The superclass is not supposed to know anything of its subclasses, so you'd
have a problem here.
If I do
V1 x = new V1(filePath);
filePath, may result to a WMA or a V2.

You'll have to know what kind of object to create prior to the creation. You
might consider creating a factory class being responsible for determining
the instance to create:

class AudioFactory
{
public AudioFactory()
{
}

public Audio createAudio(string filePath)
{
// check the file, extract information about Artist and Type
// create an instance of the appropriate Audio subclass
// return the proper class
}
}

You might think of creating some kind of AudioInterface as well.
 
Back
Top