Solved

polymorphism

Posted on 2004-04-01
9
265 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MSVCR80.dll crash 2 141
returning a dereferenced pts in C++ 10 157
thread-safe code in c++ 2 115
sorting efficency of sorting algorithm 30 121
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

766 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