Member Avatar for terrah

ok, so I have to do this problem to solve :
Create a class Road that contains different stop points. A point is characterized by
latitude, longitude, altitude.
a. Validate the correctness of the values
b. Create an array of Roads and display it
c. Display the longest Road
d. Display the Road with minimum, maximum stop points.
e. Display all Roads that contain a certain stop point

Here is my partial solution... can you please tell me whats wrong with my destcutors or constructors?

ROAD.cpp
    #include <iostream>
#include <cmath>
#include <cstring>
#include "road.h"
const double PI = 3.14159265;
using namespace std;

double road::ConvertToRadians(double d)
{
    double radians = (d / 360) * (2.0 * PI);
    return radians;
}

double road::sum_point(road road, int max_size)
{
int R = 6371, i = 1, s = 0;     // km
double dLat, dLon, lat1, lat2,lon1, lon2, a, c, d;
while ( i <= max_size )
{
    lat1 = road.x[i]->lat;
    lon1 = road.x[i]->lon;
    i++;
    lat2 = road.x[i]->lat;
    lon2 = road.x[i]->lon;

    dLat= ConvertToRadians(lat2-lat1);
    dLon= ConvertToRadians(lon2-lon1);
    a = sin(dLat/2) * sin(dLat/2) +
        sin(dLon/2) * sin(dLon/2) *
        cos(lat1) * cos(lat2);
    c = 2 * atan2(sqrt(a), sqrt(1-a));
    d = R * c;
    s+= d;
}
return s;

}

double road::calc_point(point x, point y)
{
double dLat, dLon, lat1, lat2,lon1, lon2, a, c, d;
int R = 6371;
lat1 = x.lat;
lat2 = y.lat;
lon1 = x.lon;
lon2 = y.lon;
dLat= ConvertToRadians(lat2-lat1);
dLon= ConvertToRadians(lon2-lon1);
a = sin(dLat/2) * sin(dLat/2) +
    sin(dLon/2) * sin(dLon/2) *
    cos(lat1) * cos(lat2);
c = 2 * atan2(sqrt(a), sqrt(1-a));
d = R * c;
cout<<d;
return 0;
}
/*
road::road()
{
pointSize = 0;
}
*/
road::road(char *s, point *p, int max_size)
{
cout<<"CONSTRUCTOR ROAD: \n";
if(s != NULL)
    strcpy(this->name, s);
this->pointSize = max_size;
for(int i = 0; i < this->pointSize; i++)
    this->x[i] = new point(p[i]);
cout<<this;
}

road::~road()
{
if(this->name != NULL)
    delete this->name;
if(this->pointSize != 0)
{
    for(int i =0; i <this->pointSize; i++)
        delete this->x[i];
}
}

void road::display()
{
int i = 0;
if(this->name != NULL)
    cout<<"ROAD NAME: "<<this->name<<endl;
cout<<"POINTS: "<<endl;
for(i = 0; i < this->pointSize; i++)
{
    cout<<"POINT "<<i+1<<":"<<endl;
    cout<<"LAT: "<<this->x[i]->lat<<" ";
    cout<<"LON: "<<this->x[i]->lon<<" ";
    cout<<"ALT: "<<this->x[i]->alt<<endl;
}
}
road.h
#include "point.h"
#ifndef ROAD_H_INCLUDED
#define ROAD_H_INCLUDED

class road
{
char *name;
point *x[10];
int pointSize;
public:
    road(char*, point *, int);
    ~road();
    road(const road&);
    void display();
    void display_certain(double, double, double = 0);
    double max_road(road );
    double min_road(road );
    double calc_point(point , point );
    double sum_point(road, int );
    double ConvertToRadians(double );
};
#endif // ROAD_H_INCLUDED
point.h
#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED

class point
{

public:
    double lat, lon, alt;
    point(double = 0.0, double = 0.0, double = 0.0);
    point(const point&p);
    ~point();
    void display();
private:
    double validatepoint(double);
};

#endif // POINT_H_INCLUDED
point.cpp
#include "point.h"
#include <iostream>
using namespace std;
point::point(double lat, double lon, double alt)
{
this->lat = validatepoint(lat);
this->lon = validatepoint(lon);
this->alt = validatepoint(alt);
cout<<"CONSTRUCTOR POINT"<<this<<" ";
cout<<endl;
}
point::point(const point&p)
{
    this->lat = p.lat;
    this->lon = p.lon;
    this->alt = p.alt;
    cout<<"copy constructor POINT"<<this<<" ";
    display();
    cout << endl;
}
point::~point()
{
    cout << "destructor POINT " << this << " ";
    display();
    cout << endl;
}

void point::display()
{
    cout << "[" << this->lat << "," << this->lon << "," << this->alt << "]";
    cout << endl;
}

double point::validatepoint(double p){
    if ( -300<=p && p<=300){
    return p;
   }
    return 0;
}

and then in the end I try to create an obj, but it errors after it compiles:

main.cpp
#include <iostream>
#include "road.h"
using namespace std;
int main()
{
point coords(2,3,5);
coords.display();
road r("lala",&coords,);
return 0;
}

so please, can someone help me ? its an emmergency ! thank you!

The first thing I noticed is that this->name is an unallocated pointer (line 67, road.cpp). You have to allocate memory for it before you can call strcpy() in the second constructor.

line 7, main.cpp: missing the third parameter to the constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.