GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Sep-2007, 10:37
mortomarty mortomarty is offline
New Member
 
Join Date: Sep 2007
Posts: 2
mortomarty is on a distinguished road

Inheritance and classes


Hi, this is the problem:
A. Design a class, circleType, that can store and process the radius of the circle. You should then perform operations on the circle, such as setting the radius of the circle, printin the radius of the circle, returning the value of the radius. Also, write a test program to test various operations on the circle.


B. Every cylinder has a height and r radius of two circles. Given the height and the radius, you can determine the cylinder's area and circumference. Design a class, cylinderType, that can store the height and the radius of the cylinder. Because the radius is a data member in the class circleType and you designed a class to capture the properties of a circle in Programming Exercise a , you must derive the class cylinderType from the class circleType. You should be able to perform the usual operations on a cylinder, such as setting the height, printing the height and calculating and printing the area and volume.

=========

this is what i came until now:

CPP / C++ / C Code:
class circleType
{
private:
	float r;
	float carea;
public:
	circleType ();
	void setR (float rad)
	{
		r=rad;
	}
	float getR()
	{
		return r;
	}
	void printR ()
	{
		cout<<r;
	}
	void setcarea ()
	{
		carea = 2* 3.14 * (r*r);
	}
	~circleType();
}
class cylinderType : public circleType
{
private:
	float h;
public:
	cylinderType();
	void setH (float hight)
	{
		h=hight;
	}
	float getH()
	{
		return h;
	}
	void printH ()
	{
		cout<<h;
	}

feel free the change what ever you want..
Last edited by admin : 28-Sep-2007 at 22:30. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 28-Sep-2007, 17:00
davis
 
Posts: n/a

Re: Inheritance and classes


Note that there are some "important" reasons why I disagree with the notion of making "circleType" a base of "cylinderType" ...however, you're not likely to understand them at this juncture. Basically, a circle is a 2D (two-dimensional shape) whereas a cylinder is a 3D shape. In other words, they are not the same "kinds" of objects, yet the notion of cylinderType : public circle means that they ARE the same kinds of objects. What I've done in the code below is to aggregate (aka composition) a circle inside of the cylinder. This is, I believe, a useful design choice IF 2D objects are distinctive from 3D objects. In the case, as presented in the requirements, I would strongly lobby toward a position that accepted that cylinderType IS NOT a kind of circleType. Rather, as shown in my code, circleType (I changed the names because these are fhking stupid, IMO) is an object INSIDE of the cylinderType.

Here's some code: caveat I ran out of time to test it. There could be substantive errors with it yet. Even if you find errors, you should find something about this code that is useful.

CPP / C++ / C Code:
#include <iostream>
#include <cmath>

class Shape2D
{
public:
    virtual double getArea()const=0;
};

class Circle : public Shape2D
{
public:
    Circle() : m_radius(0)
    {
    }
    Circle( const double radius ) : m_radius(radius)
    {
    }
    bool operator==( Circle const& rhs )
    {
        bool bIsEquality = false;
        if( rhs.m_radius == m_radius )
        {
            bIsEquality = true;
        }
        return bIsEquality;
    }
    bool operator!=( Circle const& rhs )
    {
        return !(*this == rhs);
    }
    Circle& operator=( Circle const& rhs )
    {
        if( this != &rhs )
        {
            m_radius = rhs.m_radius;
        }
        return *this;
    }
    friend std::ostream& operator<<( std::ostream& os, Circle const& rhs )
    {
        os << rhs.m_radius;
        return os;
    }
    friend std::istream& operator>>( std::istream& is, Circle& rhs )
    {
        is >> rhs.m_radius;
        return is;
    }
    bool operator>( Circle const& rhs )
    {
        return(m_radius > rhs.m_radius ? true : false);
    }
    bool operator<( Circle const& rhs )
    {
        return(m_radius < rhs.m_radius ? true : false);
    }
    virtual ~Circle()
    {
    }
    double getRadius() const
    {
        return m_radius;
    }
    void setRadius( const double radius )
    {
        if( radius >= 0 )
        {
            m_radius = radius;
        }
    }
    double getArea() const
    {
        return m_radius * m_radius * M_PI;
    }
    double getCircumference() const
    {
        return 2 * m_radius * M_PI;
    }
    double getDiameter() const
    {
        return 2 * m_radius;
    }
protected:
    private:
    double m_radius;
};

class Shape3D
{
public:
    virtual double getVolume()const=0;
};

class Cylinder : public Shape3D
{
public:
    Cylinder() : m_height(0)
    {
        m_circle = new Circle();
    }
    Cylinder( const double radius, const double height )
    {
        if( height >= 0 )
        {
            m_height = height;
        }
        if( radius > 0 )
        {
            m_circle = new Circle( radius );
        }
        else
        {
            m_circle = new Circle();
        }
    }
    Cylinder( Circle const& circle ) : m_height(0)
    {
        m_circle = new Circle();
        *m_circle = circle;
    }
    Cylinder( Circle const& circle, const double height )
    {
        if( height >= 0 )
        {
            m_height = height;
        }
        m_circle = new Circle();
        *m_circle = circle;
    }
    Cylinder( Cylinder const& rhs )
    {
        m_circle = new Circle();
        *m_circle = *rhs.m_circle;
    }
    Cylinder& operator=( Cylinder const& rhs )
    {
        if( this != &rhs )
        {
            if( m_circle )
            {
                delete m_circle;
            }
            m_circle = new Circle();
            *m_circle = *rhs.m_circle;
        }
        m_height = rhs.m_height;
        return *this;
    }
    bool operator==( Cylinder const& rhs )
    {
        bool bIsEquality = false;
        if( m_height == rhs.m_height &&
            *m_circle == *rhs.m_circle )
        {
            bIsEquality = true;
        }
        return bIsEquality;
    }
    bool operator!=( Cylinder const& rhs )
    {
        return !(*this == rhs);
    }
    friend std::ostream& operator<<( std::ostream& os, Cylinder const& rhs )
    {
        os << rhs.m_height << *(rhs.m_circle);
        return os;
    }
    friend std::istream& operator>>( std::istream& is, Cylinder& rhs )
    {
        is >> rhs.m_height >> *(rhs.m_circle);
        return is;
    }
    virtual ~Cylinder()
    {
        if( m_circle )
        {
            delete m_circle;
            m_circle = 0;
        }
    }
    void setHeight( const double height )
    {
        if( height >= 0 )
        {
            m_height = height;
        }
    }
    double getHeight() const
    {
        return m_height;
    }
    double getRadius() const
    {
        return m_circle->getRadius();
    }
    void setRadius( const double radius )
    {
        m_circle->setRadius( radius );
    }
    double getArea() const
    {
        return m_circle->getArea();
    }
    double getCircumference() const
    {
        return m_circle->getCircumference();
    }
    double getDiameter() const
    {
        return m_circle->getDiameter();
    }
    double getVolume() const
    {
        return m_circle->getArea() * m_height;
    }
    void getCircle( Circle& circle ) const
    {
        circle = *m_circle;
    }
protected:
    private:
    double m_height;
    Circle* m_circle;
};


using namespace std;

void spew_circle_details( Circle const& c )
{
    cout << "Circle with radius: " << c
    << "\nhas circumference: " << c.getCircumference()
    << "\ndiameter: " << c.getDiameter()
    << "\narea: " << c.getArea()
    << endl;
}

void spew_cylinder_details( Cylinder const& c )
{
    Circle circle;
    c.getCircle( circle );
    cout << "Cylinder with volume: " << c.getVolume()
    << "\nheight: " << c.getHeight()
    << "\ncontains:\n";
    spew_circle_details( circle );
}

int main()
{
    Circle c1;
    c1.setRadius( 1.00 );
    spew_circle_details( c1 );

    Circle c2 = c1;
    c2.setRadius( 10.00 );
    if( c2 != c1 )
    {
        cout << "the circles are not equal" << endl;
    }
    c2 = c1;
    if( c2 == c1 )
    {
        cout << "the circles are equal" << endl;
    }

    cout << "Enter a new radius for the circle: ";
    cin >> c1;
    spew_circle_details( c1 );

    if( c1 > c2 )
    {
        cout << "bigger!" << endl;
    }
    if( c1 < c2 )
    {
        cout << "smaller!" << endl;
    }

    Cylinder cy1;
    cy1.setHeight( 10.0 );
    cy1.setRadius( 1.0 );

    spew_cylinder_details( cy1 );

    Cylinder cy2 = cy1;
    if( cy1 == cy2 )
    {
        cout << "equal cylinders" << endl;
    }
    cy2.setHeight( cy2.getHeight() * 2 );
    if( cy1 != cy2 )
    {
        cout << "unequal cylinders" << endl;
    }

    spew_cylinder_details( cy2 );


    return 0;
}

Output:

Code:
Circle with radius: 1 has circumference: 6.28319 diameter: 2 area: 3.14159 the circles are not equal the circles are equal Enter a new radius for the circle: 12.3 Circle with radius: 12.3 has circumference: 77.2832 diameter: 24.6 area: 475.292 bigger! Cylinder with volume: 31.4159 height: 10 contains: Circle with radius: 1 has circumference: 6.28319 diameter: 2 area: 3.14159 unequal cylinders Cylinder with volume: 3.0718e-269 height: 9.77786e-270 contains: Circle with radius: 1 has circumference: 6.28319 diameter: 2 area: 3.14159

I wish that I had more time to complete the code and fix the errors. Perhaps in the next couple of days I'll be able to provide some changes. However, this should at least demonstrate some of the notions of what I think is an appropriate implementation (less the obvious problems) of the INTENT of supporting 2D and 3D objects in code.

DaveOnTheLeftCoast...please feel free to add some comment, as I hurried through this impl in order to stay...at least closely to...my 10 minute implementation limitation.


:davis:
  #3  
Old 29-Sep-2007, 06:09
mortomarty mortomarty is offline
New Member
 
Join Date: Sep 2007
Posts: 2
mortomarty is on a distinguished road

Re: Inheritance and classes


thanks davis on the comment,, but there are few things that i dont understand,,

such as (*this) (virtual),,, anyways, i'll ask my friends what excatly the teacher wants us to do, then i'll post, because your program is longer then what she asked us to do. thanks,, i still need your generous help,, lol ,,dont go away
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheritance Suspicious Problem Peter_APIIT C++ Forum 12 15-May-2007 22:01
Need To Learn Inheritance using CLASSES CanitoCool C++ Forum 1 24-Oct-2006 00:00
Classes initiation kdsXchris C++ Forum 3 05-Jun-2006 03:07
Inheritance Project .. Qatar C++ Forum 1 28-Apr-2005 17:04
Assistance with classes... Bravebird C++ Forum 7 27-Apr-2005 13:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.