Solved

polymorphism

Posted on 2004-04-01
9
262 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
Comment Utility
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
Comment Utility
Example:
class Circle:public Shape
{
    Point center;
    mutable double radius;// Declare this variable mutable
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 30

Expert Comment

by:Axter
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 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

8 Experts available now in Live!

Get 1:1 Help Now