473,669 Members | 2,415 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is wrong in this code ?

hai
i am not able to overload a member function of base class in derived
calss.what is the wrong thning i am doing here in the following
program.
# include<iostrea m>
using namespace std;

class Quad
{
public:
void Area() ;
void Desc() ;
};

class Square : public Quad
{
public:
using Quad:Area;
void Area(int x)
{
cout<<"Area of square is = "<<x*x<<end l;
}
void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};
class Rectangle : public Quad
{
public:
using Quad:Area;
void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<end l;
}
void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}
};

class Creator
{
public:
Quad* Creator::Create (int id)
{
if(id==2)
return new Square;
else
return new Rectangle;
}
};
int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Quad *square=mcreato r.Create(argc);
square->Area(3);
square->Desc();
}
else
{
Quad *rectangle=mcre ator.Create(arg c);
rectangle->Area(3,4);
rectangle->Desc();
}
return 0;

}

Dec 7 '06 #1
5 1472
"sunny" <ka************ ***@gmail.comwr ote in message
news:11******** *************@1 6g2000cwy.googl egroups.com...
: i am not able to overload a member function of base class in derived
: calss.what is the wrong thning i am doing here in the following
: program.
In C++, you need to explicitly state that a function can
be overriden in a subclass.
(you wanted to say *override*, overload has a different meaning).

: # include<iostrea m>
: using namespace std;
:
: class Quad
: {
: public:
: void Area() ;
: void Desc() ;
You should write:
virtual void Area() =0;
virtual void Desc() =0;

Further adding =0 tells the compiler that the member function
is *abstract* (=not implemented in this class) in addition
to being virtual.

: };
: class Square : public Quad
: {
...... [ok code] .....
: class Creator
: {
: public:
: Quad* Creator::Create (int id)
: {
: if(id==2)
: return new Square;
: else
: return new Rectangle;
: }
NB: make sure to learn to use std::auto_ptr
to avoid forgetting to delete the objects
that are created with new.

: };
: int main(int argc, char* argv[])
: {
: Creator mcreator;
: if (argc<=2)
: {
: Quad *square=mcreato r.Create(argc);
: square->Area(3);
: square->Desc();
: }
: else
: {
: Quad *rectangle=mcre ator.Create(arg c);
: rectangle->Area(3,4);
: rectangle->Desc();
: }
: return 0;
:
: }

hth --Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <http://www.brainbench.com

Dec 7 '06 #2
You cannot use base class pointer to call overloaded subclass member
function.

#include<iostre am>
using namespace std;

class Quad
{
public:
void Area();
void Desc();
};

class Square : public Quad
{
public:
Square() {};

void Area(int x)
{
cout<<"Area of square is = "<<x*x<<end l;
}

void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};

class Rectangle : public Quad
{
public:
Rectangle() {};

void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<end l;
}

void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}

};

class Creator
{
public:
Quad* Creator::Create (int id)
{
if (id==2)
return new Square;
else
return new Rectangle;
}
};

int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Square *square = reinterpret_cas t<Square*>(mcre ator.Create(2)) ;
square->Area(3);
square->Desc();
}
else
{
Rectangle *rectangle =
reinterpret_cas t<Rectangle*>(m creator.Create( 1));
rectangle->Area(3,4);
rectangle->Desc();
}

return 0;
}

Dec 7 '06 #3
Or in overlapped version. It can do.
#include<iostre am>
using namespace std;

class Quad
{
public:
virtual void Area(int x) {};
virtual void Area(int x, int y) {};
virtual void Desc() {};
};

class Square : public Quad
{
public:
Square() {};

void Area()
{
cout<<"Area of square is = 0"<<endl;
}

void Area(int x)
{
cout<<"Area of square is = "<<x*x<<end l;
}

void Desc()
{
cout<<"This Derived class Square from Base Class Quad"<<endl;
}
};

class Rectangle : public Quad
{
public:
Rectangle() {};

void Area()
{
cout<<"Area of Rectangle is = 0"<<endl;
}

void Area(int x)
{
cout<<"Area of Rectangle is = "<<x*x<<end l;
}

void Area(int x, int y)
{
cout<<"Area of Rectangle is = "<<x*y<<end l;
}

void Desc()
{
cout<<"This Derived class Rectangle from Base Class Quad"<<endl;
}

};

class Creator
{
public:
Quad* Creator::Create (int id)
{
if (id==2)
return new Square;
else
return new Rectangle;
}
};

int main(int argc, char* argv[])
{
Creator mcreator;
if (argc<=2)
{
Quad *square = mcreator.Create (2);
square->Area(3);
square->Desc();
}
else
{
Quad *rectangle = mcreator.Create (1);
rectangle->Area(3,4);
rectangle->Desc();
}

return 0;
}

Dec 7 '06 #4
Allen napisal(a):
Square *square = reinterpret_cas t<Square*>(mcre ator.Create(2)) ;
It is VERY BAD, UNSAFE practice to use reinterpret_cas t here! Proper
way is to use dynamic_cast.

My proposal is:

class Quad
{
public:
void Area(); // not needed, it should be dropped out
virtual void Desc() = 0; // every derived class will have to
// implement this function
};
/*-------- Square ----------*/

class Square : public Quad
{
public:
void Area(int x);
void Desc();
};

void Square::Area(in t x)
{
cout << "Area of square is = " << x*x << endl;
}

void Square::Desc()
{
cout << "This Derived class Square from Base Class Quad" << endl;
}
/*------ Rectangle ---------*/

class Rectangle : public Quad
{
public:
void Area(int x, int y);
void Desc();
};

void Rectangle::Area (int x, int y)
{
cout << "Area of Rectangle is = " << x*y << endl;
}

void Desc()
{
cout << "This Derived class Rectangle from Base Class Quad" << endl;
}
/*-------- Creator -----------*/

class Creator
{
public:
Quad* Create(const int id);
};

Quad* Creator::Create (const int id)
{
if (id == 2)
return new Square();
else
return new Rectangle();
}
/*--------- Let's use this stuff -------*/

int main(int argc, char* argv[])
{
Creator mcreator;

if (argc <= 2) {
// below we use dynamic_cast. It checks if Square is really
// a subclass of this Quad object and then does casting
Square* square = dynamic_cast<Sq uare*>(mcreator .Create(argc));
square->Area(3);
square->Desc();

// do not forget to do some cleanups, to avoid memory leaks
delete square;

} else {
Rectangle* rectangle =
dynamic_cast<Re ctangle*>(mcrea tor.Create(argc ));
rectangle->Area(3,4);
rectangle->Desc();

delete rectangle;
}

return 0;
}
/*----- Another, more compact and safe version --------*/

int main(int argc, char* argv[])
{
Creator mcreator;

Quad* quad = mcreator.Create (argc);

if (argc <= 2) {
dynamic_cast<Sq uare*>(quad)->Area(3);

} else {
dynamic_cast<Re ctangle*>(quad)->Area(3,4);
}

quad->Desc(); // it uses Desc() version of Square or Rectangle,
// according to type of created object,
// beacause Quad.Desc() is virtual

delete quad;

return 0;
}

Dec 7 '06 #5
I V
On Wed, 06 Dec 2006 21:14:34 -0800, sunny wrote:
hai
i am not able to overload a member function of base class in derived
calss.what is the wrong thning i am doing here in the following
program.
As another poster said, what you want to do here is _override_ (provide a
function with the same interface as another function), not
_overload_ (provide a function with the same name, but a different
interface).
class Quad
{
public:
void Area() ;
void Desc() ;
Here, you are declaring that Quad has functions Area and Desc, both taking
no arguments.
};

class Square : public Quad
{
public:
using Quad:Area;
void Area(int x)
{
cout<<"Area of square is = "<<x*x<<end l;
}
Here, you are saying Square has a function Area taking one argument. Note
that this function is _completely unrelated_ to the function "Area" in the
Quad class. Likewise, the function Area in the Rectangle class, taking two
arguments, is unrelated to the Area functions of either Quad or Square.

What you need to do is give Square and Rectangle functions called Area
that take no arguments; then, they will have the same interface as Quad
(you also need, as another poster pointed out, to find out how to use the
"virtual" keyword).

To get you started - this is a possible main function you could use.

int main()
{
Creator c;

Quad* quad = c.Create(2);
quad->Desc();
quad->Area();

delete quad;

quad = c.Create(4,5);
quad->Desc();
quad->Area();

delete quad;
}

Dec 8 '06 #6

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

Similar topics

125
14709
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
72
5836
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for example, between a C/C++ programmers with a few weeks of experience and a C/C++ programmer with years of experience. You don't really need to understand the subtle details or use the obscure features of either language
121
10031
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
51
13349
by: WindAndWaves | last post by:
Can anyone tell me what is wrong with the goto command. I noticed it is one of those NEVER USE. I can understand that it may lead to confusing code, but I often use it like this: is this wrong????? Function x select case z
46
4198
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
13
5036
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
1
1461
by: GS | last post by:
I got a combobox box that I load at load time. the Item and vales ended up in reverse order of each other, what went wrong? the database table has the following row code value ebay http://www.ebay.com google http://www.google.com yahoo http://www.yahoo.com However in the drop down list displayed value used ebay http://www.yahoo.com
98
4571
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
9
2118
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
20
2808
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
0
8383
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
8894
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
8803
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
8587
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
8658
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6210
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
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2029
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.