Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

need small help..inheritance

inheritance

  • Please log in to reply
6 replies to this topic

#1 iostream

iostream

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 03 October 2010 - 07:27 AM

hi every body ..

i do this assigment that the dr. give it to us .. and when i want to do run for the program

this message comes " cannt execute program"

...

this is my program
# include <iostream>

using namespace std;

class Rectangle
{

protected:

    float Length;

    float Width;

public:

    Rectangle ( float L=1, float W=1);

    void setLength (float L);

    void setWidth (float W);

    float getLength ();

    float getWidth ();

    float Arectangle ();

    float Crectangle ();

    void printRectangle() ;

    void ShowDim ();

    ~ Rectangle();

};

Rectangle::Rectangle( float L, float W)
{
    Length= L;

    Width= W;

}

void Rectangle::setLength(float L)
{
    Length= L;

}

void Rectangle::setWidth(float W)

{
    Width= W;
}

float Rectangle :: getLength()

{
    return Length;

}

float Rectangle:: getWidth()
{
    return Width;
}

float Rectangle::Arectangle ()
{
    return (W*L);

}

float Rectangle::Crectangle ()
{
    return (2*(W+L));

}

void Rectangle::PrintRectangle() 
{

    cout<< "LENGTH="<< L<< endl;

    cout<< "WIDTH="<<W<<endl;

    cout << "AREA="<<Arectangle ()<<endl;

    cout<< "CIRCUMFERENCE=" <<Crectangle ()<<end

}

void Rectangle :: ShowDim()
{

    cout<< "LENGTH="<< L<< endl;

    cout<< "WIDTH="<<W<<endl;
    }


Rectangle::~Rectangle()

{

    cout<< "rectangle was destroyed"<<endl;

}

class RectangleClube : public Rectangle
{

protected:

    float Height;

public:

    RectangleClube ( float L=1, float W=1, float H=1);


    void setHeight(float H);

    float getHeight ();

    float Vrectangle ();

    void printHeight() ;

    ~RectangleClube();

};

RectangleClube::RectangleClube ( float L, float W, float H)
{
    Height=H;
}

void RectangleClube::setHeight(float H)

{

Height=H;

}

void RectangleClube::getHeight()

{
    return Height;

}

float RectangleClube:: Vrectangle ()
{
    return (W*L*H);
}


void RectangleClube:: printHeight()
{

    cout<< "HEIGHT=" <<H<< endl;

    cout<< "VOLUME="<<Vrectangle ()<<endl;
}

void RectangleClube:: ShowDim ()

{
    cout<< "LENGTH="<< L<< endl;

    cout<< "WIDTH="<<W<<endl;
    
    cout<< "HEIGHT=" <<H<< endl;
}



RectangleClube::~RectangleClube()
{
    cout<<" RectangleClube was destroyed"<<endl;
}

int main()
{

    Rectangle R;

    R. setLength(2);
    R.setWidth(3);
    R. setHeight(4);

    cout << "AREA="<<Arectangle ()<<endl;

    cout<< "CIRCUMFERENCE=" <<Crectangle ()<<end

    cout<< "VOLUME="<<Vrectangle ()<<endl;

}
can you check it for me please?!:)

and tell me what is the wrong in it ..:rolleyes:

Edited by WingedPanther, 03 October 2010 - 08:57 AM.
add code tags (the # button)

  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 October 2010 - 08:58 AM

Does it not compile, or does execution fail?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 iostream

iostream

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 03 October 2010 - 09:01 AM

zero error !
  • 0

#4 iostream

iostream

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 03 October 2010 - 09:04 AM

may be cause of the height is not on the first class .. i'll check it !!
  • 0

#5 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 03 October 2010 - 09:22 AM

I fixed some errors. Here's the code.
# include <iostream>

using namespace std;

class Rectangle {

  protected:
    float Length;
    float Width;

  public:

    Rectangle ( float L=1, float W=1);
    void setLength (float L);
    void setWidth (float W);
    float getLength ();
    float getWidth ();
    float Arectangle ();
    float Crectangle ();
    void printRectangle() ;
    void ShowDim ();
    ~ Rectangle();
};

Rectangle::Rectangle( float L, float W) {
  Length= L;
  Width= W;
}

void Rectangle::setLength(float L) {
  Length= L;
}

void Rectangle::setWidth(float W) {
  Width= W;
}

float Rectangle :: getLength() {
  return Length;
}

float Rectangle:: getWidth() {
  return Width;
}

float Rectangle::Arectangle () {
  return (Width*Length);
}

float Rectangle::Crectangle () {
  return (2*(Width+Length));
}

void Rectangle::printRectangle() {
  cout<< "LENGTH="<< Length<< endl;
  cout<< "WIDTH="<<Width<<endl;
  cout << "AREA="<<Arectangle ()<<endl;
  cout<< "CIRCUMFERENCE=" <<Crectangle ()<<endl;
}

void Rectangle :: ShowDim() {
  cout<< "LENGTH="<< Length<< endl;
  cout<< "WIDTH="<<Width<<endl;
}


Rectangle::~Rectangle() {
  cout<< "rectangle was destroyed"<<endl;
}

class RectangleClube : public Rectangle {

  protected:
    float Height;

  public:
    RectangleClube ( float L=1, float W=1, float H=1);
    void setHeight(float H);
    float getHeight ();
    float Vrectangle ();
    void printHeight() ;
    ~RectangleClube();
};

RectangleClube::RectangleClube ( float L, float W, float H) {
  Height=H;
}

void RectangleClube::setHeight(float H) {
  Height=H;
}

float RectangleClube::getHeight() {
  return Height;
}

float RectangleClube:: Vrectangle () {
  return (Width*Length*Height);
}


void RectangleClube:: printHeight() {
cout<< "HEIGHT=" <<Height<< endl;
cout<< "VOLUME="<<Vrectangle ()<<endl;
}

void RectangleClube:: ShowDim () {
  cout<< "LENGTH="<< Length<< endl;
  cout<< "WIDTH="<<Width<<endl;
  cout<< "HEIGHT=" <<Height<< endl;
}

RectangleClube::~RectangleClube() {
  cout<<" RectangleClube was destroyed"<<endl;
}

int main() {

  Rectangle R;

  R.setLength(2);
  R.setWidth(3);
  R.setHeight(4);

  cout << "AREA="<<Arectangle ()<<endl;
  cout<< "CIRCUMFERENCE=" <<Crectangle ()<<endl;
  cout<< "VOLUME="<<Vrectangle ()<<endl;
}
Only errors now are that Rectangle doesn't have setHeight() method since it's a base class and you the last 3 lines in main function, you're calling them without object.
  • 0

The roots of education are bitter, but the fruit is sweet.


#6 iostream

iostream

    CC Newcomer

  • Just Joined
  • PipPip
  • 10 posts

Posted 03 October 2010 - 10:15 AM

i changed the name of the class from Rectangle to RectangleClube in the main which has a height

emmm there is few errors now !

i don't know what r they!
  • 0

#7 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts
  • Location:::1
  • Programming Language:C++, Python

Posted 03 October 2010 - 11:51 AM

cout << "AREA="<<Arectangle ()<<endl;
cout<< "CIRCUMFERENCE=" <<Crectangle ()<<endl;
cout<< "VOLUME="<<Vrectangle ()<<endl;
You are calling these, Arectangle(), Crectangle(), Vrectangle(), without objects. Object is instance of a class, should be something like this
int main() {
    RectangleClube r;  // r is object, instance of class RectangleClube
    cout << "AREA="                  << r.Arectangle() << endl;
    cout << "CIRCUMFERENCE=" << r.Crectangle() << endl;
    cout << "VOLUME="             << r.Vrectangle() << endl;
}

  • 0

The roots of education are bitter, but the fruit is sweet.






Also tagged with one or more of these keywords: inheritance