473,466 Members | 4,835 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

new classname() showing error

39 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2.     class derived;   //forward declaration
  3.  
  4.      class base {
  5.  
  6.           public :
  7.                 base *getobject()
  8.                   {
  9.                           if(/*condition*/)
  10.                               return new derived();
  11.                        else
  12.                           /* do nothing */
  13.                     }
  14.                virtual void paint() {} =0;
  15.             };
  16.  
  17.     class derived  :  public base
  18.         {
  19.              public:
  20.                  derived(){  }
  21.                    void paint()
  22.                     {
  23.                        cout<<"in derived";
  24.                      }
  25.               };
  26.  
  27.    int main()
  28.     {
  29.             base  *b,j,*k;
  30.            b=&j;
  31.            k = b->getobject();
  32.            k->paint();
  33. }
  34.  
  35.  

please tell me whats the cause of error
Sep 22 '07 #1
3 1246
Meetee
931 Recognized Expert Moderator Contributor
Expand|Select|Wrap|Line Numbers
  1.  
  2.  virtual void paint() {} =0;
  3.  

the cause of error
Here virtual function declared is a pure virtual function. So it cannot be defined in derived class.
Sep 22 '07 #2
pntkiran
16 New Member
Hi Diwakar,

you are using forward declaration, So can't access derived class function through base class object(remove getobject function), modify virtual void paint() function ..(remove =0) and assign k = new derived ();.

You will get the result.
Cheers.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     class derived;   //forward declaration
  3.  
  4.      class base {
  5.  
  6.           public :
  7.                 base *getobject()
  8.                   {
  9.                           if(/*condition*/)
  10.                               return new derived();
  11.                        else
  12.                           /* do nothing */
  13.                     }
  14.                virtual void paint() {} =0;
  15.             };
  16.  
  17.     class derived  :  public base
  18.         {
  19.              public:
  20.                  derived(){  }
  21.                    void paint()
  22.                     {
  23.                        cout<<"in derived";
  24.                      }
  25.               };
  26.  
  27.    int main()
  28.     {
  29.             base  *b,j,*k;
  30.            b=&j;
  31.            k = b->getobject();
  32.            k->paint();
  33. }
  34.  
  35.  

please tell me whats the cause of error
Sep 22 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
The actual problem is failing to substitute a derived object for a base object.

Your main shoud be:
Expand|Select|Wrap|Line Numbers
  1. base* ptr = new derived;
  2. ptr->paint();   //calls derived::paint()
  3.  
That is, when you have virtual functions you are designing the interface to be inthe base class but you will actually create derived objects and use them as base objects. This is fundamental polymorphism.

The pure virtual function only means that you can't call the method using an object of the class that declares the pure virtual function.

Since the base class defines the interface, the methods the user is call should be public and none of these should eb private. Instead, the base public methods call base private virtual methods and it is these that are overriden in the derived class.

Most likely, the overiding method can be private in the derived class.

Your base::getobject() that returns a base* is bad design. Methods like this belong in a Factory class and not your base class. It is the Factory that creates the derived object and returns its address as a base*.
Sep 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Joshua Beall | last post by:
Hi All, I have been trying to dynamically call a static member function, as follows: $className = 'MyClass'; $methodName = 'MyMethod' $result = $className::$methodName(); However, I get a...
1
by: Peter King | last post by:
if you assign multiple classes in order to keep your classes generic e.g ..classA { position.absolute; left:5 top:5 height:200 width:800 } ..classB { background-color: red} ..classC {...
4
by: Ben R. | last post by:
Between ClassName and Inherits, which attribute is set to specify the class that a page uses? I would think that would be inherits. Further, the description for ClassName is: Specifies the class...
2
by: bmgz | last post by:
I have written a script that highlights a table row when the appropriate checkbox is checked. Using element.style is a bit messy and doesn't really fulfil my needs.. I want to just be able to...
5
by: http://www.douglassdavis.com | last post by:
Why is it that I can do this: $obj= new $classname(); But not this: $classname::astaticfunc() I would like to call a static function when I only have the class name in a variable and the...
1
by: Rene Sørensen | last post by:
I'm trying to make things alot easyer for my self, by useing a formatet error report, but i need some info to do that, like etc... MessageBox.Show( "Error In File: " + GetFileNAme() + " \n" +...
1
by: Vicenç Masanas | last post by:
I had a problem with some webforms and user controls. From time to time when developing a new form or user control and testing I got the following error: Could not load type 'xml.WebForm1' I...
2
by: benjamin | last post by:
I'm new at JS and DOM, so please bear with me... Here's what I'm trying to do: My code is such as this: *** <div class="post" id="123"> lorem ipsum <div class="tag1">hello</div> </div>
10
by: sp | last post by:
I create an xml file in a text editor: <?xml version="1.0" encoding="utf-8"?> <elts> <elt id="1" class="c1">content1</elt> <elt id="2" class="c1">content2</elt> </elts> Then I load the file...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
1
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...
0
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.