Problems with the execution of class hierarchy

P

Piotre Ugrumov

I had wrote this class hierarchy
Quadrilateral->Trapezoid->Parallelogram->Rectangle->Square.
I had compiled the classes without error, but when I start a test program
the values of the variables of the object are non inizialized.
Here I write The class Trapezoid(The only classe that run perfectly) and the
class Parallelogram.
How Can I resolve this problem?
Thanks.


#pragma once
#include "quadrilateral.h"

class Trapezoide :
public Quadrilateral
{
public:
Trapezoide(int=0, int=0, int=0, int=0, int=0, int=0, int=0, int=0);
double getHeight()const;
double getBaseM()const;
double getBasem()const;
double getPerimeter()const;
double getArea()const;
void print();
~Trapezoide(void);
private:
int y1, y2;
double area, perimetrr, h, baseM, basem;
protected:
double l1, l2;
};



#include "StdAfx.h"
#include ".\trapezoide.h"
#include <cmath>
#include <iostream>
using namespace std;

Trapezoide::Trapezoide(int xa, int ya, int xb, int yb, int xc, int yc, int
xd, int yd)
{
y1=ya;
y2=yb;
y2=y1;
Quadrilateral::setVertex(xa, y1, xb, y2, xc, yc, xd, yd);
if(yc>=yd)
h=yc-y2;
else
h=yd-y2;
if(h<0)
h=h*(-1);
baseM=xb-xa;
if(baseM<0)
baseM=baseM*(-1);

basem=sqrt(static_cast<double>((xc-xd)*(xc-xd))+static_cast<double>((yc-yd)*
(yc-yd)));

l1=sqrt(static_cast<double>((xc-xb)*(xc-xb))+static_cast<double>((yc-y2)*(yc
-y2)));

l2=sqrt(static_cast<double>((xa-xd)*(xa-xd))+static_cast<double>((y1-yd)*(y1
-yd)));
}
double Trapezoide::getHeight()const{
return h;
}

double Trapezoide::getBaseM()const{
return baseM;
}

double Trapezoide::getBasem()const{
return basem;
}


double Trapezoide::getPerimeter()const{
return l1+l2+baseM+basem;
}

double Trapezoide::getArea()const{
return (baseM+basem)*h/2;
}
void Trapezoide::print(){
Quadrilateral::print();
cout<<"Base 1: "<<baseM<<", Base 2: "<<basem<<", Height: "<<h<<endl;
cout<<"Perimeter: "<<getPerimeter()<<", Area: "<<getArea()<<endl;
}

Trapezoide::~Trapezoide(void)
{
}




#pragma once
#include "trapezoide.h"

class Parallelogram :
public Trapezoide
{
public:
Parallelogram(int=0, int=0, int=0, int=0, int=0, int=0, int=0, int=0);
double getBase() const;
void print();
~Parallelogram(void);
};



#include "StdAfx.h"
#include ".\parallelogram.h"
#include <cmath>
#include <iostream>
using namespace std;

Parallelogram::parallelogram(int xa, int ya, int xb, int yb, int xc, int yc,
int xd, int yd)
{
yd=yc;
if(abs(xd-xa)!=abs(xd-xc))
xd=xb+abs(xd-xa);
Trapezoide::Trapezoide(xa, ya, xb, yb, xc, yc, xd, yd);
}

double Parallelogram::getBase()const{
return Trapezoide::getBaseM();
}

void Parallelogram::print(){
Quadrilateral::print();
cout<<"Base: "<<getBase()<<", Height: "<<Trapezoide::getHeight()<<endl;
cout<<"Lato: "<<Trapezoide::l1<<endl;
cout<<"Perimeter: "<<Trapezoide::getPerimeter()<<", Area:
"<<Trapezoide::getArea()<<endl;
}
Parallelogram::~Parallelogram(void)
{
}


#include "stdafx.h"
#include "Trapezoide.h"
#include "Parallelogram.h"

int main()
{
Trapezoide t(5,4,7,8,9,8,2,8);
Parallelogram primo(1, 1, 4, 1, 4, 4, 1, 4);
t.print();
primo.print();
return 0;
}
T is printed correctly, PRIMO has al the values equal to 0.
 
R

Ralf

Parallelogram::parallelogram(int xa, int ya, int xb, int yb, int xc, int
yc,
int xd, int yd)
{
yd=yc;
if(abs(xd-xa)!=abs(xd-xc))
xd=xb+abs(xd-xa);
Trapezoide::Trapezoide(xa, ya, xb, yb, xc, yc, xd, yd);
}

I think, you have some leaks in C++ syntax. Try to write a correct
initialization list:

Parallelogram::parallelogram(int xa, int ya, int xb, int yb, int xc, int yc,
int xd, int yd)
: Trapeziode(xa, ya, xb, yb, xc, yc, xd, yd)
{
}

But also think about, if you class hierarchy is correct. Is an parallelogram
realy a special form of a trapeziode?
It is not. This makes the code complicated and difficult to understand.


Ralf

www.oo-fabrik.de
www.oop-trainer.de
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top