Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 27th, 2007, 05:35 AM
ali
Guest
 
Posts: n/a
Default abstract base class - how to initialize variables in derived?

Hi,

I have a question with regards to abstract classes (understanding that
I cannot instantiate an abstract class).

If my Abstract base class has:

private:
int width;
int height;

How do I initialize/give values to these from my derived class? I
tried adding public methods to the base class, such as setWidth() and
setHeight(), and use them in the constructor of the derived class. Is
that a good solution? Is there any other method I can use?

I know that I cannot access base::height and base::width from the
derived class as they are private variables.

Thanks,

Ali

  #2  
Old February 27th, 2007, 05:45 AM
angrybaldguy@gmail.com
Guest
 
Posts: n/a
Default Re: abstract base class - how to initialize variables in derived?

On Feb 26, 9:27 pm, "ali" <aliasger.jaf...@gmail.comwrote:
Quote:
Hi,
>
I have a question with regards to abstract classes (understanding that
I cannot instantiate an abstract class).
>
If my Abstract base class has:
>
private:
int width;
int height;
>
How do I initialize/give values to these from my derived class? I
tried adding public methods to the base class, such as setWidth() and
setHeight(), and use them in the constructor of the derived class. Is
that a good solution? Is there any other method I can use?
Use the base class' constructor.

class ABC {
//...
protected: // or public: if appropriate
ABC (int w, int h):
width (w), height (h) {
}

private:
int width;
int height;
};

class Concrete : public ABC {
public:
Concrete ():
ABC(0, 0) {
}
// ...

  #3  
Old February 27th, 2007, 07:25 AM
Paresh
Guest
 
Posts: n/a
Default Re: abstract base class - how to initialize variables in derived?

Please refer http://msdn2.microsoft.com/en-us/lib...ee(vs.80).aspx

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles