Solved

Internet Class Program???? Please HELP!!!

Posted on 1998-06-23
18
218 Views
Last Modified: 2008-02-01
PLEASE CAN ANYBODY HELP ME????? I know that this is cheating but I have never been so desperate for help. I am at the end of my course and have got completely stuck with this assignment. I have no Idea what to do and am going to fail the course if I don't hand it in. I know that this is a lot to ask and I wouldn't do it normally but I desperalty need to get the grade.

The question is :-

Numeric address for computers on the internet are composed of four parts seperated by periods, of the form xx,yy,zz,mm, where xx, yy, zz and mm are positive integers. locally, computers are usually known by a nickname as well. You are designing a program to process a list of internet addresses, identifying all pairs of computers from the same locality (ie. with matching xx and yy components). Create a class called InternetAddress with components for the four integers and a fith copmponent to store an associated nickname of at most 20 characters. Your program should extract a list of any number of addresses and nicknames from a datafile whose name is provided by the user. The first line of the file should be the number of addresses that follow. Here is a sample of the data

3
111.22.3.44 platte
555.66.7.88 wabash
11.22.5.66 green

dynamically allocate an array to hold the indicated numbers of InternetAddress objects. Fill the array and then display a list of messages identifying each pair of computers. Here is a sample message.

      Machines platte and green are on the same network.
      
include in your class definition friend operators >> and << and a SameNetwork member function that takes a second InternetAddress object as a parameter and returns true if the two addresses are on the same network and false otherwise. Follow the messages by a display of the full list of addresses and nicknames.
0
Comment
Question by:Dave091277
  • 11
  • 4
  • 2
  • +1
18 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 450 total points
Comment Utility
help coming.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I will not help you cheat.  But must institutions will allow you ask specific questions of experts (or seek the infomation from other sources)  If addition, most institutions would allow you to show your work to others for criticism and minor suggestions.  If that is within your school's guidlines, I would be happy to help you.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Assuming this is okay, why don't you post what you have done or if it is long, e-mail it to me at nietod@theshop.net.

If you have nothing done.  I suggest you start with the class declaration itself.  Try to figure out what data members need to be stored in the class.   Try to figure out what member procedures are need and declare them (you don't have to write them yet, just try to figure out what they are and what parameters they take.)   You may want to try to write the constructor and destructor members as well.  Then let me see what you have.
0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
This is really very simple.  Ask concrete questions, post whatever you've done and we'll get you in shape in no time.  You can also contact me by mail if you wish (click on my handle to get the address)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
As Alex said this is pretty simple and shouldn't take long (100 points would have been enough) but if it does, between the two of us you have round the clock coverage so we can keep you moving until you are done or die of exhaustion, which ever comes first.
0
 
LVL 8

Expert Comment

by:trestan
Comment Utility
Here quite a lot of people are helping you. Don't worry.
Try to break down the question into pieces. Think about the procedure to fulfill the task. You may solve the problem if you keep calm. My email address can be found from my homepage.
Good luck.
0
 

Author Comment

by:Dave091277
Comment Utility
Thanks for all the help!! My main problem is that I have no idea where to start, we have had no tuition on file handling and only a tiny bit on making classes. I am trying to write the

Class InternetAddress
{
Private:
???
Public:
???

bit now. What is a constructor and destructor please??? I don't normally do this but this is the only peice of work in 4 years that has left me in such a mess. THANKS agian to all of you for the help.

0
 
LVL 11

Expert Comment

by:alexo
Comment Utility
>> Class InternetAddress
>>  {
>> Private:

All the data the object needs to hold.  "with components for the four integers and a fith copmponent to store an associated nickname of at most 20 characters" sounds like a good hint :-)

>> Public:

Whatever "interface" the object exposes to the outside world.  "a SameNetwork member function" sounds like a good hint.

>> };

Also, do not forget "friend operators >> and <<".

>> What is a constructor and destructor please
You can ignore them for now.  In general, a constructor is a method (member function) that constructs an object by initializing its data members, acquiring resources, etc.  Similarly, a destructor releases the aquired resources.  Your class can manage without a constructor for the time being and does not need a destructor at all.

I think I'm going to sign off and let Todd handle the rest of the interchange since he already locked the question and there is really not that much work.  For 450 points you better make him sweat hard!
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I AM sweating.  Its been in the 100's in Oklahoma for a week now.

Before we go on.  Have you ever written a class before?   (Do you even know what one is?)  If not Alex's comments might not make sense.   I want to get an idea of your level of understanding before we really start.  
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Dave091277
Comment Utility
I did do a class for a battery a while ago. When I had sorted that (with some help from the turor) I could do stuff with the main() bit quite easily. My problem is setting up the class. After that though I have to figure out file stuff which I have never done in C++. I have used the Battery Class I did to try the begining of this.

class InternetAddress
{
private:
      int numrecs;
      int x[100];
      int y[100];
      int z[100];
      int m[100];
      char nickname[100];
public:
      InternetAddress(Int, int, int, int, int, char);

}

InternetAddress::InternetAddress(int number, int a, int b, int c, int d, char name)
{
      int count =1;
      numrecs = number;

      do
      {
            x[count] = a;
            y[count] = b;
            z[count] = c;
            m[count] = d;
            nickname = name;
      While(count <= numrecs)
}

Would this make it so that in the MAIN() bit I could read the file one line at a time and put them into the array within the class????

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Lets not worry about main yet.   Lets get the class made first.  Then we'll use it.

First of all, inside you class you have arrays like

int x[100];

This is an array that will hold 100 values.  That isn't what you want.  You only need on x value, like

int x;

Your constructor builds on this.  It sets every item in these arrays to specified values.  you shouldn't need to do this.

Each InternetAddress object will store a single address.  If you need to store multiple addresses, you will create multiple InternetAddress objects.  Does that make sense?

another probem in the constructor is that you try to use an = to copy a string, like

nickname = name;

That doesn't work.  That won't even compile.  You have to use strcpy() to copy strings.  (or you can use a string class that defines operator =, but I suspect you haven't covered that yet.)

So lets work on these things.
1.  Make the class store just the data it needs, with no arrays for the address information.
2.  Change the constructor to set the non-array form of the address information
3.  Use strcpy() to copy the name.


0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
By the way, just so you know, the do...while loop you had will never terminate.  It does not change "count" so it will go forever.  But, you will not need it all, anyways.
0
 

Author Comment

by:Dave091277
Comment Utility
What do you mean when you say "Change the constructor to set the non-array form of the address information"???

I understand the array bit I think... Do you mean that when I create an InternetAddress item I would use something like :-

InternetAddress Address1(?????)

I used something like that in my battery program. Also do I need to set all of the values how I have???
Sorry for taking up so much of your time.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Sorry, for being unclear.  

You made the InternetAddress class store arrays of address information and you made the constructor initialize the arrays.  That is not needed, you only need one address per object (do you understand that?)  What I was saying is you need to change the object to store only one address (not arrays) and change the constructor to initialize just the one address (not arrays.)  Is that better?  One little thing.  You do need an array for the name.  Just not for the x,y,z stuff.  Okay?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I don't really understand the other stuff you are asking about.  Hopefully I answered it with my last comment.  If not try asking again.  (reworded.)
0
 

Author Comment

by:Dave091277
Comment Utility
I have had to mail you because my connection is really slow. I changed the previous code but forgot to change

nickname= name to

nickname = strcopy(name)

I don't know if that is the correct way to do it though???
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
The following was sent in an E-mail.  I just want to keep a record of it.
*****************************************************************
For a class we write we can overload operators in two ways.  As members
function or non-member functions.  For example say I write a class that
stores huge integers by storing each digit in a big array, like

Class HugeInt
{
private:
    char Digits[100];
};

I would like to do.

HugeInt H1 = 1000000000000;
int          S2 = 5;
HugeInt H2 = H1 + S2;  // get 10000000000005

If have two choices about how to write a function that adds a regullar
integer to a huge int, I could do it as a member of non-member
function.  As a member function it would look like

HugeInt HugeInt::operator +(int RhtOpp)
{
   HugeInt RetVal;  // Eventually holds value to be returned.
   RetVal = *this; // start with value on left side of "+"
   ///// somehow add on RhtOpp.
     return RetVal;
}

In this case the HugeInt on th left side of  the "+" is the object the
function got called for.  The int on the right side of the "+" is the
one passed in the parameters.  Make sense?  As a non-member function it
is written like

HugeInt operator +(HugeInt LftOpp,int RhtOpp)
{
   HugeInt RetVal;  // Eventually holds value to be returned.
   RetVal = LftOpp; // start with value on left side of "+"
   ///// somehow add on RhtOpp.
  return RetVal;
}

In this case there are two parameters.  The one on the left side of the
"+" comes first, then the one on the right side.  Big deal?  not really,
but it will be important in a bit.

When you write an overidded operator as  member function, you can
specify the type that goes on the right side only.  The type on the left
side is the class that you are writting this for.  Make sense? But when
you write a non-member function you get to specify the types on the left
and on the thr right side.

Here is the thing.  We want to write an overloaded "<<" that we can use
like

cout << HugeInt;

to print a HugeInt to an I/O stream.  Out first attempt should try to be
with a member function.  Member functions are nice because they can
access private members and the data we need to print (all those digits)
is private  So we try

ostream &HugeInt::operator <<(ostream &S) const
{
     S << Digits;
     return S;
}

That works, almost.  But it is backward.  We want

cout << HugeInt;

but that has to be used as

HugeInt << cout;

Why?  because the class we overloaded, Hugeint, goes on the left, and
the parameter class, the stream, goes on the right.  To get them on the
right sides we could try writting it for the stream class.   Like

ostream &ostream::operator <<(const HugeInt &H)

However, this is changing the ostream class.  It is adding a new
function to it.  We're not allowed to do that. Its not part of the
original class declaration.  So this means that the overloaded operator
can not be a member function.  (in this case, that is for  <<, for most
cases it can)  It needs to be a non-member like

ostream operator <<(ostream &S,const HugeInt &H)
{
    S << H.Digits;
   return S;
};

Now that works in that the stream is on the left and the huge int is on
the right.  The only problem is the line inside that accesses
H.Digits.  Digits is private.and this function is not a member function
so it can't access digits.  How do we get around that.  We declare this
function as a friend to HugeInt.  Then it can access HugeInt's private
members, like Digits.

That is why the assignment asked for overloaded "Friend" functions for
<< and >>.  They must be overlaoded because they will work on the type
you create.  They must be friend's because A) they can't be members of
your class and B) they have to access the class's private data.

Make sense?  do you think you can write the << function?  it should just
print out the contents of the IntenetAddress.  to the stream in the
parameters. Like

cout << "Numeric Address:  " << x << '.' << y << '.' << z << endl;
cout << "Name:   " << nickname;

Now the stream it will be printing to is not cout, it will be a stream
passed in the parameters.  Also there should be 4 numbers printed, but I
don't remember the 4th's name.  It didn't start with "w", I don't think,
but there's nothing after "z".  But you get the idea.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
This is what we ended up with.  This doesn't meet the requirements but may be all that gets done before the deadline.
****************************************8
 class InternetAddress
 {
 private:
                        int x;
                        int y;
                        int z;
                        int m;
                        char nickname[21];

 public:
                        InternetAddress::InternetAddress(int a, int b, int c, int d, char *name)
                        {
                                x=a;
                                y=b;
                                z=c;
                                m=d;
                                strcpy(nickname, name);
                         };

                        InternetAddress::InternetAddress()
                        {
                                x=0;
                                y=0;
                                z=0;
                                m=0;
                                strcpy(nickname, "");
                        };


                        int InternetAddress::SameNetwork(const InternetAddress &Otheraddress)
                        {
                                 return (x == Otheraddress.x)&&(y == Otheraddress.y);
                        };

friend ostream  & operator <<(ostream &S, const InternetAddress &IA);

 };

ostream & operator<< (ostream &S, const InternetAddress &IA)
{

                S<<IA.x<<IA.y<<IA.z<<IA.m<<endl;
                return S;
};


void main()
{
        InternetAddress IA1(1, 2, 3, 4, "Bill");
        InternetAddress IA2(1, 2, 4, 7, "Fred");

        if (IA1.SameNetwork(IA2))
                cout<<"Same!!";
        else
                cout<<"Not the same";



}


0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now