473,383 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Problems with the execution of class hierarchy

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.
Jul 22 '05 #1
1 1966
> 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

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stefan Seefeld | last post by:
hi there, I'v run into a little problem for which I only found an ugly workaround, so I'd like to know whether people are aware of better ways to achieve this... I'm defining a class 'Class'...
13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
2
by: Matt | last post by:
Hello, I would like to generate what I call an "overall class hierarchy" in UML automatically derived from my C++ code source. An example of what I seek: ...
21
by: Mark Broadbent | last post by:
Consider the following statements //------- Item i = Basket.Items; //indexer is used to return instance of class Item Basket.Items.Remove(); //method on class item is fired item i = new...
0
by: Alex Clark | last post by:
Hi all, Apologies for the cross-post but I can't determine if this is a VS .NET problem or a VB.NET language issue. I'm using .NET 1.1 SP1, VS 2003 EA, VB.NET. I'm coding a custom component...
1
by: Alex Clark | last post by:
Hi all, Apologies for the cross-post but I can't determine if this is a VS .NET problem or a VB.NET language issue. I'm using .NET 1.1 SP1, VS 2003 EA, VB.NET. I'm coding a custom component...
3
by: kk_oop | last post by:
Hi. I have base class Base and derived class Derived. My derived class constructor is dependent upon attributes set in the base class constructor. Is this okay? Does ANSI C++ guarantee that a...
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.