473,511 Members | 16,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

I have to create an abstract class "shape" with one pure virtual function print, tha

4 New Member
I have to create an abstract class "shape" with one pure virtual function print, that outputs the name of the shape. Derive class circle and rectangle from the shape. In the main function create an array that can hold any type of the derived classes. Put one circle and one rectangle into this array. Print the names of the objects. Delete objects (that means that you have to have a destructor . You do not have to put any code in the destructor, just cout name of the class, that it belongs to.

can somebody please fix these codes for me i would really appreciate it. please guys i need ur help... thanks

Expand|Select|Wrap|Line Numbers
  1.  #include<iostream>
  2. using namespace std;
  3.  
  4. class Rectangle {
  5. public:
  6. Rectangle (double,double);
  7. Rectangle (double,double,double,double);
  8. bool check_square(double, double);
  9. void show_check_sqaure_result(bool);
  10. private:
  11. double x,y;           
  12. double length,width;
  13. };
  14.  
  15. class Circle {
  16. public:
  17. Circle (Rectangle);
  18. };
  19.  
  20. Rectangle::Rectangle(double l,double w)
  21. {
  22. length = l;
  23. width = w;
  24. cout<<"The area is "<<length*width<<" unit sqaure."<<endl;
  25. check_square(l,w);
  26. }
  27.  
  28. Rectangle::Rectangle(double x0,double y0,double x1, double y1)
  29. {
  30. double a = (x1>x0) ? (x1-x0):(x0-x1);
  31. double b = (y1>y0) ? (y1-y0):(y0-y1);
  32. cout<<"The area is "<<a*b<<" unit sqaure."<<endl;
  33. check_square(a,b);
  34. }
  35.  
  36. bool Rectangle::check_square(double a, double b)
  37. {
  38. double c;
  39. c = a-b;
  40. show_check_sqaure_result(c);
  41. }
  42.  
  43. void Rectangle::show_check_sqaure_result(bool k)
  44. {
  45. if (k==0)
  46. cout<<"It is a square.";
  47. else
  48. cout<<"It is NOT a square.";
  49. cout<<endl;
  50. }
  51.  
  52. Circle::Circle(Rectangle)
  53. {
  54. cout<<"The area of respective circle is"<<endl;
  55. }
  56.  
  57. int main()
  58. {
  59. Rectangle A(4.6,10, 3, 9);
  60. Circle B(A);
  61. Rectangle C(1,1);
  62. Circle D(C);
  63. system("pause");
  64. }
Apr 12 '10 #1
3 3222
newb16
687 Contributor
What is the point of passing Rectangle as an argument to Circle's constructor and ignoring it there?
And why is square of the shape calculation there, if it's not in the requirements?
Apr 12 '10 #2
manontheedge
175 New Member
what you have posted looks like random, incoherant code from the Internet that is no where close to being anything that resembles your question. I recommend you break down the question, like this ...

1. You need to create an abstract class "shape"
--- what makes a class abstract?

2. It needs a virtual function - print()
--- what's a virtual function?

3. Derive class Circle and Rectangle from Shape.
--- how do you derive a class?

4. Create an array that will hold any type of derived class.
--- what kind of array will it be?

5. Print the names of the objects.
--- how do you print the names?

You need to have a good understanding of classes and then inheritence. But, you need to break the question down if it's not making sense.
Apr 12 '10 #3
jkmyoung
2,057 Recognized Expert Top Contributor
I think you want a different constructor for the circle, and different methods for a circle. One thing which would define a circle would be it's radius.

So have a radius variable inside the circle shape, and force the constructor to pass a radius in, not a rectangle.
Apr 12 '10 #4

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

Similar topics

11
4649
by: Tim Cadell | last post by:
I found an odd situation that probably isn't doing what is intended. class A { function A() { // normal constructor for class1 } function B() { // a general purpose method }
0
641
by: Bob | last post by:
Has anyone tried (and succeded?) in specifying the appropriate connect string for a hierarchical dataset using MyODBC? It's where you specify MSDATASHAPE as your "Provider" but then need to...
4
4195
by: WittyGuy | last post by:
Hi all, Though I know the concepts of both abstract class & virtual function (like derived class pointer pointing to base class...then calling the function with the pointer...), what is the real...
11
4329
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
6
1719
by: romayankin | last post by:
Can someone explain what am I doing wrong? Header: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ class CMyBase { private: CMyBase(const CMyBase& obj); /* Disable copy constuctor */ void...
8
2065
by: al pacino | last post by:
hi, i want to know how does c++ compiler prohibits the creation of instance of an abstract class? i think it must be checking on the vtable entry if there is an entry with zero(indicting...
8
8337
by: mfabricius | last post by:
Hi, I am probably trying to do something really stupid. There is a class shape: class Shape { public: Shape();
1
5320
by: Stodge | last post by:
Yet another SWIG question (YASQ!). I'm having a problem with using an abstract base class. When generating the Python bindings, SWIG thinks that all the concrete classes that derive from this...
2
1339
by: Angus | last post by:
Hello I want to use polymorphic feature. Eg if I had a base Shape class and a derived Circle class I could do things like this: Shape myShape; MyCircle* pCircle = &myShape;...
3
1967
by: Magnus.Moraberg | last post by:
Lets say I have the following - class Shape { ... virtual void Plot(); PrintInfo(); } class Circle : public Shape
0
7138
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...
0
7355
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
7423
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...
0
7510
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...
1
5066
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
3225
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
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.