Thread: Crash course in C++, definitions/explanation

  1. #1
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065

    Crash course in C++, definitions/explanation

    No laughs, please . I'm attempting to learn this stuff, but I don't understand some of the definitions. The below code was copied off of daniweb from 2004. I have a "test" that I'm taking where they don't expect me to be able to write C++ code, but I don't want to look completely brain dead either. The question goes something like make a class that does linked lists. I searched and found this.

    Anywho, I want to follow this code so that I'll understand it, I'd rather not copy it directly. Please correct me where I'm wrong.

    First, the <strong class="highlight". . . I have a vague understanding of what strong and weak does: strong means that we want to associate this class more closely (that'd be, we inherit from this class more than "???normal???"), where weak would me we have a loose association with the parent class, but we will probably implement most of the methods ourselves.

    Second, the "highlight" class is a standard class defined in iostream?

    From here, I think I understand the rest of the code. Basically, we define a "structure" that has elements we cannot touch from the OUTSIDE world, first and mySize. The public elements of the "structure" are data and next, which the user would presumably pass to us in some of the public methods.

    Code:
    #ifndef LINKEDLIST                                                                                                    
    #define LINKEDLIST                                                                                                    
    #include<iostream.h>                                                                                                  
    
    template<typename DataType>
    <strong class="highlight">class</strong> LinkedList
    {                                                  
            private:                                   
                    <strong class="highlight">class</strong> Node
                    {                                            
                    public:          // Makes it accessible (public) by <strong class="highlight">class</strong> Node
                            DataType data;                                                                           
                            Node *next;                                                                              
                    };                                                                                               
    
                    Node *first;
                    int mySize; 
    
                    LinkedList::deleteLL()
                    {                     
                            Node *tempP=first, *disposeP;
                            while (tempP!=0)             
                            {                            
                                    disposeP=tempP;      
                                    tempP=tempP->next;   
                                    delete disposeP;     
                            }                            
                            return;                      
                    }
    Past this point, there is another public{ } set that has what I believe to be a constructor
    Code:
                            LinkedList::LinkedList()
                            {                       
                                    mySize=0;       
                                    first=0;        
                            }
    the destructor (??? which is _REQUIRED_ for every class???)
    Code:
                            LinkedList::~LinkedList()
                            {                        
                                    deleteLL();      
                            }
    and then the externally called methods (more comments below)
    Code:
                            LinkedList& LinkedList::operator=(const LinkedList& s)
                            {                                                     
                                    if (this!=&s)                                 
                                    {                                             
                                            deleteLL();                           
                                            mySize=s.mySize;                      
                                            first=copyLL(s.first)                 
                                    }                                             
                                    return *this;                                 
                            }                                                     
                                    bool LinkedList::empty()                      
                                    {                                             
                                            return (first==0);                    
                                    }                                             
                                    void LinkedList::insert(DataType item, unsigned pos)
                                    {                                                   
                                            if (pos>mySize+1)                           
                                            {                                           
                                                    cout<<"Illegal position to insert:"<<pos<<endl;
                                                    return;                                        
                                            }                                                      
                                            mySize++                                               
                                                    Node* newNode=newNode;                         
                                            newNode->data=item;                                    
                                            Node* prev=first;                                      
                                            for (int i=1, i<pos, i++)                              
                                                    prev=prev->next;                               
                                            newNode->next=prev->next;                              
                                            prev->next=newNode;                                    
                                    }                                                              
                                    void LinkedList::delete (dataType item)                        
                                    {                                                              
                                            mySize--;                                              
                                            Node* tempP=first;                                     
                                            Node* prev=0;                                          
                                            while (tempP!=0 && tempP->data!=item)                  
                                            {                                                      
                                                    prev=tempP;                                    
                                                    tempP=tempP->next;                             
                                            }                                                      
                                            if (tempP!=0 && tempP->data==item)                     
                                            {                                                      
                                                    prev->next=tempP->next;
                                                    delete tempP;
                                            }
                                                    else
                                                            cout<<"Item to delete not found"<<endl;
                                                    return;
                                    }
    
                                    int LinkedList::locate(DataType item)
                                    {
                                            int position=0;
                                            Node* ptr=first;
                                            while(ptr->data<item &&ptr!=0)
                                            {
                                                    position++;
                                                    ptr=ptr->next;
                                            }
                                            return position
                                    }
    
                                                    void LinkedList::traverse()
                                                    {
                                                            Node *temp=first;
                                                            while (tempP!=0)
                                                            {
                                                                    Process(tempP->data);
                                                                    tempP=tempP->next;
                                                            }
                                                            return;
                                                    }
                                    };
    This is most confusing to me. From what I understand about C++, this would be the portion that overloads the "=" operator, but then I get into the weeds and I just don't understand. Ah, wait, I see, there is some funky indention in that code. Is it standard to indent the other methods from the overloading methods?

    So, to use one of these methods, one would simply call something like:

    Code:
    LinkedList node = new LinkedList;
    
    node.insert(someitem, 0);
    Or, am I way off.

    Any help would be appreciated!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kennedy
    First, the <strong class="highlight". . . I have a vague understanding of what strong and weak does: strong means that we want to associate this class more closely (that'd be, we inherit from this class more than "???normal???"), where weak would me we have a loose association with the parent class, but we will probably implement most of the methods ourselves.
    It looks like you had the misfortune of coming upon code that is poorly formatted to the extent that the HTML tags became mixed up with the C++ code

    You should discard the "<strong class="highlight">" and "</strong>" as they are not part of C++.

    Furthermore, the inclusion of the pre-standard header <iostream.h> means that we may need to be wary of other pre-standard/non-standard stuff lying around.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Kennedy View Post
    No laughs, please . I'm attempting to learn this stuff, but I don't understand some of the definitions. The below code was copied off of daniweb from 2004. I have a "test" that I'm taking where they don't expect me to be able to write C++ code, but I don't want to look completely brain dead either. The question goes something like make a class that does linked lists. I searched and found this.

    Anywho, I want to follow this code so that I'll understand it, I'd rather not copy it directly. Please correct me where I'm wrong.

    First, the <strong class="highlight". . . I have a vague understanding of what strong and weak does: strong means that we want to associate this class more closely (that'd be, we inherit from this class more than "???normal???"), where weak would me we have a loose association with the parent class, but we will probably implement most of the methods ourselves.

    Second, the "highlight" class is a standard class defined in iostream?

    From here, I think I understand the rest of the code. Basically, we define a "structure" that has elements we cannot touch from the OUTSIDE world, first and mySize. The public elements of the "structure" are data and next, which the user would presumably pass to us in some of the public methods.

    Code:
    #ifndef LINKEDLIST                                                                                                    
    #define LINKEDLIST                                                                                                    
    #include<iostream.h>                                                                                                  
    
    template<typename DataType>
    <strong class="highlight">class</strong> LinkedList
    {                                                  
            private:                                   
                    <strong class="highlight">class</strong> Node
                    {                                            
                    public:          // Makes it accessible (public) by <strong class="highlight">class</strong> Node
                            DataType data;                                                                           
                            Node *next;                                                                              
                    };                                                                                               
    
                    Node *first;
                    int mySize; 
    
                    LinkedList::deleteLL()
                    {                     
                            Node *tempP=first, *disposeP;
                            while (tempP!=0)             
                            {                            
                                    disposeP=tempP;      
                                    tempP=tempP->next;   
                                    delete disposeP;     
                            }                            
                            return;                      
                    }
    Past this point, there is another public{ } set that has what I believe to be a constructor
    Code:
                            LinkedList::LinkedList()
                            {                       
                                    mySize=0;       
                                    first=0;        
                            }
    the destructor (??? which is _REQUIRED_ for every class???)
    Code:
                            LinkedList::~LinkedList()
                            {                        
                                    deleteLL();      
                            }
    and then the externally called methods (more comments below)
    Code:
                            LinkedList& LinkedList::operator=(const LinkedList& s)
                            {                                                     
                                    if (this!=&s)                                 
                                    {                                             
                                            deleteLL();                           
                                            mySize=s.mySize;                      
                                            first=copyLL(s.first)                 
                                    }                                             
                                    return *this;                                 
                            }                                                     
                                    bool LinkedList::empty()                      
                                    {                                             
                                            return (first==0);                    
                                    }                                             
                                    void LinkedList::insert(DataType item, unsigned pos)
                                    {                                                   
                                            if (pos>mySize+1)                           
                                            {                                           
                                                    cout<<"Illegal position to insert:"<<pos<<endl;
                                                    return;                                        
                                            }                                                      
                                            mySize++                                               
                                                    Node* newNode=newNode;                         
                                            newNode->data=item;                                    
                                            Node* prev=first;                                      
                                            for (int i=1, i<pos, i++)                              
                                                    prev=prev->next;                               
                                            newNode->next=prev->next;                              
                                            prev->next=newNode;                                    
                                    }                                                              
                                    void LinkedList::delete (dataType item)                        
                                    {                                                              
                                            mySize--;                                              
                                            Node* tempP=first;                                     
                                            Node* prev=0;                                          
                                            while (tempP!=0 && tempP->data!=item)                  
                                            {                                                      
                                                    prev=tempP;                                    
                                                    tempP=tempP->next;                             
                                            }                                                      
                                            if (tempP!=0 && tempP->data==item)                     
                                            {                                                      
                                                    prev->next=tempP->next;
                                                    delete tempP;
                                            }
                                                    else
                                                            cout<<"Item to delete not found"<<endl;
                                                    return;
                                    }
    
                                    int LinkedList::locate(DataType item)
                                    {
                                            int position=0;
                                            Node* ptr=first;
                                            while(ptr->data<item &&ptr!=0)
                                            {
                                                    position++;
                                                    ptr=ptr->next;
                                            }
                                            return position
                                    }
    
                                                    void LinkedList::traverse()
                                                    {
                                                            Node *temp=first;
                                                            while (tempP!=0)
                                                            {
                                                                    Process(tempP->data);
                                                                    tempP=tempP->next;
                                                            }
                                                            return;
                                                    }
                                    };
    This is most confusing to me. From what I understand about C++, this would be the portion that overloads the "=" operator, but then I get into the weeds and I just don't understand. Ah, wait, I see, there is some funky indention in that code. Is it standard to indent the other methods from the overloading methods?

    So, to use one of these methods, one would simply call something like:

    Code:
    LinkedList node = new LinkedList;
    
    node.insert(someitem, 0);
    Or, am I way off.

    Any help would be appreciated!
    This is a really poor implementation, IMO. Personally, I'd recommend reading the code in your C++ header files. It might take a while to take it all in, and even seem a bit overwhelming at times, but the end result is so much more rewarding - it just gives you a much better feel for the way in which "real" code is written.

    Good luck!
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  2. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  3. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM
  4. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM
  5. Crash after crash!
    By Dual-Catfish in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2002, 09:32 AM