Solved

polymorphism

Posted on 2004-04-01
9
264 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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.

770 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