473,770 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance question

Hi,

Suppose we have

class CAbstractFile {

public:

virtual bool Open()=0;
virtual bool Read(long num_bytes,void *bufferp)=0;
virtual bool Write(long num_bytes,void *bufferp)=0;
virtual bool Close()=0;
};

class CDiskFile : public CAbstractFile {

private:

CString Path;
HANDLE Handle;

public:

// all implemented in CDiskFile.cpp
void SetPath(CString path);
virtual bool Open();
virtual bool Read(long num_bytes,void *bufferp);
virtual bool Write(long num_bytes,void *bufferp);
virtual bool Close();
};

class CRamFile : public CAbstractFile {

private:

void *BufferStart;
long BufferLength;
long Position;

public:

// all implemented in CRamFile.cpp
void SetRamBuffer(vo id *startp,long length);
virtual bool Open();
virtual bool Read(long num_bytes,void *bufferp);
virtual bool Write(long num_bytes,void *bufferp);
virtual bool Close();
};

Now i want to derive a new class CJpegFile that implements reading &
writing of Jpeg files.

I want that CJpegFile object can be a CDiskFile or a CRamFile,
so that we can read/write a jpeg file as well via a diskfile as well
via a ramfile.

How should this be done in good C++?

I can't find the answer, and i'm sorry if this is a stupid question.

Cheers,

Vrish

Jul 23 '05 #1
7 2148
vr*****@yahoo.c om wrote:
Suppose we have

class CAbstractFile {

public:

virtual bool Open()=0;
virtual bool Read(long num_bytes,void *bufferp)=0;
virtual bool Write(long num_bytes,void *bufferp)=0;
virtual bool Close()=0;
};

class CDiskFile : public CAbstractFile {

private:

CString Path;
HANDLE Handle;

public:

// all implemented in CDiskFile.cpp
void SetPath(CString path);
virtual bool Open();
virtual bool Read(long num_bytes,void *bufferp);
virtual bool Write(long num_bytes,void *bufferp);
virtual bool Close();
};

class CRamFile : public CAbstractFile {

private:

void *BufferStart;
long BufferLength;
long Position;

public:

// all implemented in CRamFile.cpp
void SetRamBuffer(vo id *startp,long length);
virtual bool Open();
virtual bool Read(long num_bytes,void *bufferp);
virtual bool Write(long num_bytes,void *bufferp);
virtual bool Close();
};

Now i want to derive a new class CJpegFile that implements reading &
writing of Jpeg files.

I want that CJpegFile object can be a CDiskFile or a CRamFile,
so that we can read/write a jpeg file as well via a diskfile as well
via a ramfile.

How should this be done in good C++?


Something like

class CJpegFile : public CRamFile, public CDiskFile {
...
void SetIsDisk();
void SetIsRam(); // those two should probably throw if not closed
bool Open() {
if (isDisk)
CDiskFile::Open ();
else
CRamFile::Open( );
}
// others similarly
};

However, if you don't need the same CJpegFile to be able to write to
either disk or RAM at will, you'd be much better off with two classes
CJpegDisk and CJpegRam instead of one.

V
Jul 23 '05 #2
Hi Victor,

Victor Bazarov wrote:
Something like

class CJpegFile : public CRamFile, public CDiskFile {
...
void SetIsDisk();
void SetIsRam(); // those two should probably throw if not closed
bool Open() {
if (isDisk)
CDiskFile::Open ();
else
CRamFile::Open( );
}
// others similarly
};

What a pity that we can't use the C++ polymorphism here.

I thought that's what virtual functions are for, to automatically
switch between different implementations of a (abstract) base class.
However, if you don't need the same CJpegFile to be able to write to
either disk or RAM at will, you'd be much better off with two classes
CJpegDisk and CJpegRam instead of one.


Would that mean we have 2 times the same jpeg specific code, once using
the CDiskFile functions, and once using the CRamFile functions?

Cheers,

Vrish

Jul 23 '05 #3
vr*****@yahoo.c om wrote:
Hi Victor,

Victor Bazarov wrote:
Something like

class CJpegFile : public CRamFile, public CDiskFile {
...
void SetIsDisk();
void SetIsRam(); // those two should probably throw if not closed
bool Open() {
if (isDisk)
CDiskFile::Open ();
else
CRamFile::Open( );
}
// others similarly
};

What a pity that we can't use the C++ polymorphism here.

I thought that's what virtual functions are for, to automatically
switch between different implementations of a (abstract) base class.

However, if you don't need the same CJpegFile to be able to write to
either disk or RAM at will, you'd be much better off with two classes
CJpegDisk and CJpegRam instead of one.

Would that mean we have 2 times the same jpeg specific code, once using
the CDiskFile functions, and once using the CRamFile functions?


Definitely not. What you need to do is abstract the jpeg specific code
and take it out of 'CJpegDisk' and 'CJpegRam' into 'CJpegGeneric'. Then
your 'CJpegDisk' would be something like

class CJpegDisk : public CDiskFile, public CJpegGeneric {

and all things from here.

Of course, you'd be much better off if you do all this with "policies",
see "Modern C++ Design" by A. Alexandrescu.

V
Jul 23 '05 #4
vr*****@yahoo.c om wrote:
Suppose we have

class CAbstractFile { ...
class CDiskFile : public CAbstractFile { ...
class CRamFile : public CAbstractFile { ...

Now i want to derive a new class CJpegFile that implements reading &
writing of Jpeg files.

I want that CJpegFile object can be a CDiskFile or a CRamFile,
so that we can read/write a jpeg file as well via a diskfile as well
via a ramfile.


There are several ways of doing this. You need to think about how you
want to use the files.

I don't think that the methods of JpegFile include Read and Write.
Probably what you want to do with a JpegFile is things like Open,
Display, Print, ConvertToPng etc. So JpegFile is not a subclass of
AbstractFile. But it is *implemented* using an AbstractFile. So you
could have

class JpegFile {
private:
AbstractFile* file;

public:
JpegFile(char* fn): file(new DiskFile(fn) {...}
// Add a constructor to DiskFile that takes a filename

JpegFile(void* address): file(new RamFile(address ) {...}
// Add a constructor to RamFile

~JpegFile() {delete file;}
// AbstractFile needs a virtual destructor

void print();
....
};
Does this help?

--Phil.
Jul 23 '05 #5
vr*****@yahoo.c om wrote:
Hi,

Suppose we have

class CAbstractFile {
};

class CDiskFile : public CAbstractFile {
};

class CRamFile : public CAbstractFile {
};

Now i want to derive a new class CJpegFile that implements reading &
writing of Jpeg files.

I want that CJpegFile object can be a CDiskFile or a CRamFile,
so that we can read/write a jpeg file as well via a diskfile as well
via a ramfile.

How should this be done in good C++?


Personally, I think this may be more of a design issue than a language
issue. It seems to me that the hierarchy you've built is a hierarchy of
file *systems*. JPEG is a file *format*; maybe it doesn't belong in
this hierarchy. How about something like:

class CJpegProcessor
{
return_type Read(CAbstractF ile &, CImage &);
return_type Write(CAbstract File &, CImage &);
};

where CImage is some image class that you've defined to store the image
data? CJpegProcessor "knows" what a JPEG file looks like and contains,
but it doesn't need to know anything about the vagaries of writing data
to disk vs. RAM vs. (say) flash, the way the classes in your hierarchy do.

--
Mike Smith
Jul 23 '05 #6
I think you and Phil are essentially saying the same thing.

And i agree with you that it's much better to do it this way, i.e. give
CJpegFile a pointer/reference to a CAbstractFile,
so it can use that to read/write a file, whatever type that is.

Thanks for your enlightening help guys!

Cheers,

Vrish

Jul 23 '05 #7
> Of course, you'd be much better off if you do all this with "policies",
see "Modern C++ Design" by A. Alexandrescu.


Interesting link, thanks Victor.

Cheers,

Vrish

Jul 23 '05 #8

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

Similar topics

1
3751
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
2
2203
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
4
12822
by: Dave Theese | last post by:
Hello all, The example below demonstrates proper conformance to the C++ standard. However, I'm having a hard time getting my brain around which language rules make this proper... The error below *should* happen, but my question to the community is *why* does it happen? Any answer will be appreciated, but a section and paragraph number from the C++ Standard would be especially appreciated.
8
7830
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
22
23384
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete examples?
45
6367
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
6
2100
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
5
2473
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
1841
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to make a list class that will store the objects. My question is how do I go about creating the list class...I am assuming it should be a standalone class that uses an arraylist to store the objects. If I go that route how do I instantiate the...
0
9432
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
10232
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
10059
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...
0
8891
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
7420
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
6682
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
5313
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...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3578
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.