Solved

polymorphism

Posted on 2004-04-01
9
263 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 25 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 25 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now