Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 7th, 2006, 05:15 AM
sunny
Guest
 
Posts: n/a
Default 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<iostream>
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<<endl;
}
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<<endl;
}
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(argc);
square->Area(3);
square->Desc();
}
else
{
Quad *rectangle=mcreator.Create(argc);
rectangle->Area(3,4);
rectangle->Desc();
}
return 0;

}

  #2  
Old December 7th, 2006, 05:15 AM
Ivan Vecerina
Guest
 
Posts: n/a
Default Re: What is wrong in this code ?

"sunny" <kavuri.rajasekhar@gmail.comwrote in message
news:1165468474.467729.16660@16g2000cwy.googlegrou ps.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<iostream>
: 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=mcreator.Create(argc);
: square->Area(3);
: square->Desc();
: }
: else
: {
: Quad *rectangle=mcreator.Create(argc);
: 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

  #3  
Old December 7th, 2006, 05:25 AM
Allen
Guest
 
Posts: n/a
Default Re: What is wrong in this code ?

You cannot use base class pointer to call overloaded subclass member
function.

#include<iostream>
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<<endl;
}

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<<endl;
}

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_cast<Square*>(mcreator.Create(2));
square->Area(3);
square->Desc();
}
else
{
Rectangle *rectangle =
reinterpret_cast<Rectangle*>(mcreator.Create(1));
rectangle->Area(3,4);
rectangle->Desc();
}

return 0;
}

  #4  
Old December 7th, 2006, 05:25 AM
Allen
Guest
 
Posts: n/a
Default Re: What is wrong in this code ?

Or in overlapped version. It can do.


#include<iostream>
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<<endl;
}

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<<endl;
}

void 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;
}

};

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;
}

  #5  
Old December 7th, 2006, 12:35 PM
sototh@poczta.fm
Guest
 
Posts: n/a
Default Re: What is wrong in this code ?

Allen napisal(a):
Quote:
Square *square = reinterpret_cast<Square*>(mcreator.Create(2));
It is VERY BAD, UNSAFE practice to use reinterpret_cast 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(int 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<Square*>(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<Rectangle*>(mcreator.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<Square*>(quad)->Area(3);

} else {
dynamic_cast<Rectangle*>(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;
}

  #6  
Old December 8th, 2006, 02:55 AM
I V
Guest
 
Posts: n/a
Default Re: What is wrong in this code ?

On Wed, 06 Dec 2006 21:14:34 -0800, sunny wrote:
Quote:
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).
Quote:
class Quad
{
public:
void Area() ;
void Desc() ;
Here, you are declaring that Quad has functions Area and Desc, both taking
no arguments.
Quote:
};
>
class Square : public Quad
{
public:
using Quad:Area;
void Area(int x)
{
cout<<"Area of square is = "<<x*x<<endl;
}
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;
}



 

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