Crash course in C++, definitions/explanation

This is a discussion on Crash course in C++, definitions/explanation within the C++ Programming forums, part of the General Programming Boards category; No laughs, please . I'm attempting to learn this stuff, but I don't understand some of the definitions. The below ...

  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
    21,893
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 06: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, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21