?
Solved

polymorphism

Posted on 2004-04-01
9
Medium Priority
?
268 Views
Last Modified: 2010-04-01
hi guys,

should be easy for you guys...Standard Polymorphism...

Class Shape - Base Class

#ifndef SHAPE_H
#define SHAPE_H

class Shape{

public:
      virtual void read() const = 0;
      virtual void print() const = 0;

};
#endif

Class Circle - Derived Class

#ifndef CIRCLE_H
#define CIRCLE_H

#include "Shape.h"
#include "Point.h"


class Circle:public Shape
{
      Point center;
      double radius;
            
public:

      Circle();
      void setCircle(int x, int y, double r)const;
      int getX()const;
      int getY()const;
      double getRadius()const;
      virtual void read()const;
      virtual void print()const;
};
#endif

Implementation of virtual void read()const

void Circle::read()const
{
      int x;
      int y;
      int rad;
      cout<<"Entering a circle x y radius: ";
      cin>>x;
      cin>>y;
                cin>>rad;
}

Once I have the input form the user, I need to push rad into radius which is a public data member...I can't figure out how to this...It won't let me do this within the function definition because it's a const member function...Also, I would like to set the center of the circle with the use of the Point object center that was defined as public but it gives me errors when I try to do center.setX(x) (setX is a member function of class Point)...Here, I will inlcude the .h and cpp file for the class point...


#ifndef POINT_H
#define POINT_H

#include <iostream>
#include "Shape.h"


class Point
{

int xCoor;
int yCoor;

public:
      
      Point();
      Point(int x, int y);
      void setX(int x);
      void setY(int y);
      int getX()const;
      int getY()const;

};
#endif


#include <iostream>
#include "Point.h"


Point::Point(int x, int y)
{
      xCoor = x;
      yCoor = y;
}

void Point::setX(int x)
{
      xCoor = x;
}

void Point::setY(int y)
{
      yCoor = y;
}

int Point::getX()const
{
      return xCoor;
}

int Point::getY()const
{
      return yCoor;
}

Can anyone help plz??

Thanks ahead

Mike

0
Comment
Question by:finalfreak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
9 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 10737569
If the function is const, then it can not modify data members unless the data members are mutable
0
 
LVL 30

Expert Comment

by:Axter
ID: 10737576
Example:
class Circle:public Shape
{
    Point center;
    mutable double radius;// Declare this variable mutable
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10737577
A const method cannot modify the state of an object. If you need to modify the object, the method cannot be const. A good example for a const function is getX()- you are only retrieving information from the object, and not modifying it.

Why are you using const for read()?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 10737592
However, I recommend that you either make your function a non-const function, or that you not modify the variable.

Although declaring a variable mutable allows you to modify it in a const function, it should not be done unless you have a very good reason for by passing the constant logic.
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 10737619
>>A const method cannot modify the state of an object.

It can if the data member is mutable.  Although I don't recommend it, it can be done and it's part of the C++ standard.

From the C++ standards:
************************************************************
7.1.1 Storage class specifiers
9.  The mutable specifier on a class data member nullifies a const specifier applied to the containing class
object and permits modification of the mutable class member even though the rest of the object is const
************************************************************
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10737620
Axter is right with both that mutable will allow you to modify the variable, but also that you should not do it: If you use a class, and you see that a method is declared as const, you usually don't read the whole implementation of the method (and often you don't even have access to it), and you may miss the fact that one member variable is declared mutable. This means that you rely on the fact that the object does not get modified when you call the method.

In your case, it's pretty obvious that the object will be modified, because that's the whole purpose of the read() method. This means that you are creating a conflict between the expectation of what the method does, and how it's declared.

0
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 100 total points
ID: 10737627
I should have written "...should not modify..." I don't like the mutable modifier, and I haven't had a reason to use it in a long time.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question