Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > abstract base class - how to initialize variables in derived?

Reply
Thread Tools

abstract base class - how to initialize variables in derived?

 
 
ali
Guest
Posts: n/a
 
      02-27-2007
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

 
Reply With Quote
 
 
 
 
angrybaldguy@gmail.com
Guest
Posts: n/a
 
      02-27-2007
On Feb 26, 9:27 pm, "ali" <(E-Mail Removed)> wrote:
> 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) {
}
// ...

 
Reply With Quote
 
 
 
 
Paresh
Guest
Posts: n/a
 
      02-27-2007
Please refer http://msdn2.microsoft.com/en-us/lib...ee(vs.80).aspx

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
abstract base class containing class scg_ C++ 6 02-03-2009 04:50 PM
what is the difference between abstract class and pure abstract class? skishorev@yahoo.co.in C++ 4 05-17-2006 08:07 AM
About abstract class and abstract method Sameer Java 4 08-31-2005 12:59 AM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM



Advertisments