473,703 Members | 3,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OOP casting classes (using one single variable for similar types of classes)

Hi,

I think my problem deals with class casting and inheritance.

I want to deal with various Audio Formats, reading into memory for
modification, working with it (done by different classes), and writing the
result to disk afterwards.

Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
MP3FileIO and AuFileIO for the In/Out operations.
They all are inherited from the AudioFileIO class, because they all share
common methods like readSamples, writeSamples, readHeader, writeHeader.
Only the implementation is sometimes different.

Now, I want to use them in a let's say generic way:

[1]
while(there is data)
{
IN.readSamples( ); // using the classes mentioned above

DelayEffect.wor k(); // modify the memory buffer(sample data)

OUT.writeSample s(); // using the classes mentioned above
}

But due to different audio formats, the type of the IN and OUT variable
depends on the desired audio format the user specifies.
I do not want to code every time many IF-THEN-ELSE statements for checking
the desired format and for using the proper variable.

Isn't it possible to check the audio format of the incoming audio material
at first and then casting the IN and OUT variable to the proper type of
audio class?
Like:

IF the incoming file is a WAV, then IN = Type of WaveFileIO
ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO

// then using the code fragment [1], and other code fragments like [1]
Ciao, Michael
Jan 6 '06 #1
8 2126
Michael wrote:
I think my problem deals with class casting and inheritance.

I want to deal with various Audio Formats, reading into memory for
modification, working with it (done by different classes), and writing the
result to disk afterwards.

Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
MP3FileIO and AuFileIO for the In/Out operations.
They all are inherited from the AudioFileIO class, because they all share
common methods like readSamples, writeSamples, readHeader, writeHeader.
Only the implementation is sometimes different.

Now, I want to use them in a let's say generic way:

[1]
while(there is data)
{
IN.readSamples( ); // using the classes mentioned above

DelayEffect.wor k(); // modify the memory buffer(sample data)

OUT.writeSample s(); // using the classes mentioned above
}

But due to different audio formats, the type of the IN and OUT variable
depends on the desired audio format the user specifies.
I do not want to code every time many IF-THEN-ELSE statements for checking
the desired format and for using the proper variable.

Isn't it possible to check the audio format of the incoming audio material
at first and then casting the IN and OUT variable to the proper type of
audio class?
Like:

IF the incoming file is a WAV, then IN = Type of WaveFileIO
ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO

// then using the code fragment [1], and other code fragments like [1]


Your problem is in the incomplete design.

Apparently, all "Audio Formats" should derive from a common base class,
which should have the 'readSamples', 'writeSamples', and other functions
and you should be instantiating proper derived class object into a pointer
to the base class:

AudioFileIO *IN = 0;
switch (in_file_type) {
case WAVE: IN = new WaveFileIO(???) ;
break;
case AIFF: IN = new AiffFileIO(???) ;
break;
...
}

// similar for 'OUT'

Then you should use your 'IN' and 'OUT' pointers _polymorphicall y_. Read
about polymorphism, inheritance, abstract base classes, etc.. That's what
makes C++ well-suited for OOP -- class hierarchies.

Come back when you redesign your application with polymorphism in place.

V
Jan 6 '06 #2

Victor Bazarov wrote:
AudioFileIO *IN = 0;
switch (in_file_type) {
case WAVE: IN = new WaveFileIO(???) ;
break;
case AIFF: IN = new AiffFileIO(???) ;
break;
...
}


Bad idea, I think that astract factory pattern is in place......

Jan 7 '06 #3

Michael wrote:
Isn't it possible to check the audio format of the incoming audio material
at first and then casting the IN and OUT variable to the proper type of
audio class?


You might consider the "factory method" or "abstract factory" design
patterns.

Jan 7 '06 #4
q

Jan 7 '06 #5
q

Jan 7 '06 #6
On Fri, 6 Jan 2006 21:38:10 +0100, "Michael" <fe**@hrz.tu-chemnitz.de>
wrote:
Hi,

I think my problem deals with class casting and inheritance.
Virtual functions sounds like the way to go.
I want to deal with various Audio Formats, reading into memory for
modification , working with it (done by different classes), and writing the
result to disk afterwards.

Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
MP3FileIO and AuFileIO for the In/Out operations.
They all are inherited from the AudioFileIO class, because they all share
common methods like readSamples, writeSamples, readHeader, writeHeader.
Only the implementation is sometimes different.

Now, I want to use them in a let's say generic way:

[1]
while(there is data)
{
IN.readSamples( ); // using the classes mentioned above

DelayEffect.wor k(); // modify the memory buffer(sample data)

OUT.writeSample s(); // using the classes mentioned above
}

But due to different audio formats, the type of the IN and OUT variable
depends on the desired audio format the user specifies.
I do not want to code every time many IF-THEN-ELSE statements for checking
the desired format and for using the proper variable.

Isn't it possible to check the audio format of the incoming audio material
at first and then casting the IN and OUT variable to the proper type of
audio class?
Like:

IF the incoming file is a WAV, then IN = Type of WaveFileIO
ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO

// then using the code fragment [1], and other code fragments like [1]
Ciao, Michael


In addition to the Abstract Factory design pattern, the Strategy
pattern might serve you well for this.

--
Bob Hairgrove
No**********@Ho me.com
Jan 7 '06 #7
puzzlecracker wrote:
Victor Bazarov wrote:
AudioFileIO *IN = 0;
switch (in_file_type) {
case WAVE: IN = new WaveFileIO(???) ;
break;
case AIFF: IN = new AiffFileIO(???) ;
break;
...
}


Bad idea, I think that astract factory pattern is in place......


And inside that "abstract factory", what are you going to do? And if
that factory is abstract, how do you instantiate it? Through another
abstract factory? Think before you post your "bad idea" replies.
Jan 7 '06 #8

"Michael" <fe**@hrz.tu-chemnitz.de> wrote in message
news:dp******** **@anderson.hrz .tu-chemnitz.de...
Hi,

I think my problem deals with class casting and inheritance.

I want to deal with various Audio Formats, reading into memory for
modification, working with it (done by different classes), and writing the
result to disk afterwards.

Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and
MP3FileIO and AuFileIO for the In/Out operations.
They all are inherited from the AudioFileIO class, because they all share
common methods like readSamples, writeSamples, readHeader, writeHeader.
Only the implementation is sometimes different.

Now, I want to use them in a let's say generic way:

[1]
while(there is data)
{
IN.readSamples( ); // using the classes mentioned above

DelayEffect.wor k(); // modify the memory buffer(sample data)

OUT.writeSample s(); // using the classes mentioned above
}

But due to different audio formats, the type of the IN and OUT variable
depends on the desired audio format the user specifies.
I do not want to code every time many IF-THEN-ELSE statements for checking
the desired format and for using the proper variable.
What, exactly, do you mean "the type of the IN and OUT variable depends on
the desired audio format the user specifies." Why isn't the IN and OUT
variables members of the audio formats themselves? Why aren't they just
functions? Why do they depend on the type of the desired audio format?
Where do IN and OUT get their data to work on?

It sounds like you just need polymorphism, but it could also be that you
need RTTI. I don't understand what you are trying to do in your code
snippet. You don't specify what type IN and OUT can be nor why you need
them.

I'm sure there's many ways to do what you want, but I don't understand what
it is you're trying to do.

Isn't it possible to check the audio format of the incoming audio material
at first and then casting the IN and OUT variable to the proper type of
audio class?
Like:

IF the incoming file is a WAV, then IN = Type of WaveFileIO
ELSE IF incoming file is a AIF, then IN = Type of AiffFileIO
Using RTTI you can check the type of a polymorphic variable. But is this
what you are really needing to do? I'm just not sure.

In case you are, though, it would something like this:

if ( typeid( MyVar ) == typeid( MyClass ) )

so, for instance, if your polymorphic variable was stored in the variable
format it would be:

if ( typeid( Format ) == typeid( WaveFileIO ) )

although it might be a differernt if Format is a pointer. I *think* this
syntax is what you need (not positive)

if ( typeid( Format ) == typeid( WaveFileIO* ) )

// then using the code fragment [1], and other code fragments like [1]
Ciao, Michael

Jan 8 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
8736
by: vrillusions | last post by:
I've been using functions since I first started using php, but I've been hearing more and more about classes, which I never really looked at that much. I understand the whole OO programming aspect, but are there any advantages to having classes as opposed to just functions? I've heard that the classes are better because they only load the functions they need to, where with an include file of functions, the whole page gets loaded, even if...
7
5947
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries the idea: static int counter = 0; // this variable can be changed only at runtime... template <typename T> struct want_to_be_counted;
19
1745
by: Ramesh Tharma | last post by:
Hi, Is any one knows what's wrong with the following code, I was told that it will compile and run but it will crash for some values. Assume that variables are initilized. char* c; long* lg;
4
1381
by: mharness | last post by:
Hello All, I've defined a number of classes that currently only include public variable declarations. I've been reluctant to add subroutines and functions for fear of taking a performance hit when I pass the class as an argument to another function and in a one case, store the class to a session variable. Right now I have all of my functions and subroutines in a single class that's becoming a bit unwieldy. What I'd like to do is move...
14
1665
by: Shannon Richards | last post by:
Hello: I am confused about casting... Option Strict = ON Example1: --------------- When I say something like this... Dim lo_Foo = New Foo Dim lo_Obj as Object = lo_Foo
2
2166
by: Enrique Bustamante | last post by:
Casting arrays that works on watch and command window but not in code. My application is casting arrays in a way it should work. To test if I was doing something invalid, I wrote a test code that has similar structure of the classes in my application. The test worked fine, the casting I want to do must work. I compared the structure of the test with the application to ensure they where similar (inheriting twice, base class implements...
6
1456
by: Spoon | last post by:
Hello everyone, I'm writing code where I can receive two kinds of packets. The first 12 octets are common to both packet types, i.e. they have the same semantics. The 1st type has 4 more octets in the header, then the payload. The 2nd type has 16 more octets in the header, then the payload. I was thinking I'd write a base class with two derived classes:
5
5926
by: brekehan | last post by:
I've always been a little sketchy on the differences between static, dynamic, and reinterpret casting. I am looking to clean up the following block by using C++ casting instead of the C style casting. from what I am reading, I should use reinterpret cast in this situation, is that correct? Why does static and dynamic casting fail me? // please excuse the windows types, it is necessary in this code, // but the question remains C++ related
5
2192
by: Ronald Raygun | last post by:
If I have the following class heirarchy: class A{ protected $m_type; function type(){return $this->m_type;} } class B extends A{} class C extends B{}
0
8659
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9240
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7846
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6585
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5920
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4417
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2426
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.