Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 13th, 2005, 6:01 PM   #11
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 3 n00b is on a distinguished road
Send a message via MSN to n00b
DaWei's article seems full of neat information about these pointers. I'll have a look into it. but now it's time to go to bed...i'm sure i'll have some more questions tomorrow when i continue studyin
n00b is offline   Reply With Quote
Old Sep 13th, 2005, 6:02 PM   #12
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
If you type "A", you will fall into that message. No outer loop or subsequent code is shown. After the "A", the stream is broken and will no longer work, input won't be accepted, the call will just fall right through. If the program's done, fine. If not, you have to clear the error. If you don't test, you don't know the error's there, because the user that typed the "A" normally isn't you. Just add "if (!cin.good ()) ....go take measures...." type of thangy and you're home free.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 13th, 2005, 6:09 PM   #13
n00b
Programmer
 
Join Date: Sep 2005
Posts: 69
Rep Power: 3 n00b is on a distinguished road
Send a message via MSN to n00b
well...if (!cin.good... would be too much for me right now.
but it explains the use of "new" and "delete", that's the thing that's important for me right now.
n00b is offline   Reply With Quote
Old Sep 13th, 2005, 6:28 PM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Just personal opinion, but good code is never too much for you. Just salt that little fact away and save yourself at least one trip back here. Maybe we'll have it covered in a FAQ by the time someone breaks your program with a keystroke or two.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 16th, 2005, 6:42 PM   #15
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
just as limits are the underpinning of calculus, pointers are the underpinning of C/C++. originally in C, you had to use pointers to do a LOT of things. C++ hid some of that implementation but pointers are still key. this is where the learning curve on the language skyrockets. pointers are central to speed among many other things...check this code out.

simple program

#include <iostream>
#include <conio.h>
using namespace std;
           

int main()
{
    //  variable for animal type
    int choice;
    //  variable for quitting
    char quit;
    
    do
    {
        cout<<"let's make animal sounds!"<<endl;
        cout<<"1.\tdog\n2.\tchicken\n3.\tcow"<<endl;
        cout<<"4.\tcat\n5.\tquit"<<endl;
        cin>>choice;
    
        switch (choice)
        {
               case 1:
                    cout<<"woof!"<<endl;
                    break;
                
               case 2:
                    cout<<"cluck!"<<endl;
                    break;
                
               case 3:
                    cout<<"moo!"<<endl;
                    break;
                    
               case 4:
                    cout<<"meow!"<<endl;
                    break;      
                
               default:
                    cout<<"wanna quit do ya?"<<endl;
                    cin>>quit;
                    break;
        }           
    
    }while (quit != 'y');
    
    //pause program
    getch();
    
    return 0;
}//  end main

now THIS example uses pointers and a technique called "polymorphism" to determine the program flow at runtime as opposed to compile time.

#include <iostream>
#include <conio.h>
using namespace std;

//  base class
class animal
{
      public:
             //want to show when constuctor/destructor are called
             animal(){cout<<"constructor called..."<<endl;};
             virtual ~animal(){cout<<"\ndestructor called..."<<endl;};
             virtual void speak(){cout<<"\nanimal sound!\n"<<endl;}
             
};

//derived classes follow...

class dog : public animal
{
      public:
             void speak(){cout<<"\nwoof!\n"<<endl;}
};
                     
class chicken : public animal 
{
      public:
             void speak(){cout<<"\ncluck!\n"<<endl;} 
};

class cow : public animal
{
      public:
             void speak(){cout<<"\nmoo!\n"<<endl;}
};

class cat : public animal
{
      public:
             void speak(){cout<<"\nmeow!\n"<<endl;}
};  
//end of classes

           

int main()
{
    //  pointer to base class
    animal * base;
    //  variable for animal type
    int choice;
    //  variable for quitting
    char quit;
    
    do
    {
        cout<<"let's use polymorphism to make animal sounds!"<<endl;
        cout<<"1.\tdog\n2.\tchicken\n3.\tcow"<<endl;
        cout<<"4.\tcat\n5.\tbase animal"<<endl;
        cin>>choice;
    
        switch (choice)
        {
               case 1:
                    base = new dog;
                    break;
                
               case 2:
                    base = new chicken;
                    break;
                
               case 3:
                    base = new cow;
                    break;
                    
               case 4:
                    base = new cat;
                    break;      
                
               default:
                    base = new animal;
                    break;
        }           
    
        //  call appropriate "speak" function
        //  using virtual functions   
        base->speak();
    
        //  quitting condition test
        cout<<"wanna quit?"<<endl;
        cin>>quit;
          
        if (quit == 'y')
        {
                 //  free memory of animal objects
                 delete base;
        }
    
    }while (quit != 'y');
    
    //pause program
    getch();
    
    return 0;
}//  end main

looks like pointers still are a bunch of bullshit right? not really, in simple apps like these the use of pointers is contraindicated, but implementing these techniques in full-scale applications will render the shortest code and the fastest results. from an earlier post i assume you are just beginning and just worry about screen i/o and loops, if/then, etc before worrying about pointers.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Sep 18th, 2005, 8:55 PM   #16
meverha1
Newbie
 
Join Date: Sep 2005
Posts: 11
Rep Power: 0 meverha1 is on a distinguished road
I'm curious, when do you think someone learning C++ should learn about pointers? I ask because I'm using "C++ Primer Plus" which introduces the concept in the 4th Chapter. However, my alternative text "Ivor Horton's Beginning C++ " doesn't introduce them until much later (after Loops, decisions, functions, etc..).

I prefer "C++ Primer Plus" and use the other book as back-up reading but the concept of pointers seems a bit "deep" for someone just learning C++.
meverha1 is offline   Reply With Quote
Old Sep 19th, 2005, 12:57 AM   #17
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Well i think it would be good to get pointers "under your belt". Because they are used frequently in C++ and you will definitley need to know how to use them. So why not learn them sooner than later?

Also I've heard C++ Primer Plus is a very good book, so maybe it is best to go along with what it says
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Sep 19th, 2005, 5:30 AM   #18
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
It's better to carry around a little black book than to try to haul all your paramours around in a little red wagon. Take my word for it. They don't get along. That's all pointers are: indirect references.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 20th, 2005, 1:41 AM   #19
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
does the text get super-in-depth right away with them? a lot of books will say "hey, this what a pointer is!!!" with pictures of cubby-holes representing memory and all that shit. then they're like, "see you in six chapters! (when they actually explain why the hell you would ever want to use one".

sometimes it's nice just to be presented with the basics of an idea and let your brain soak in it for awhile, and then come back and get a little more in-depth. the concept of a pointer isn't that difficult, it's usually the implementation of them (in useful and non-redundant ways).

int x = 1;
int * px = &x;
cout<<*px<<endl;

is stupid.

(this may be a crappy example but...)

remote controllers are great. imagine if every time you wanted to change the channel you had to lug your fat ass off the couch and go get a new tv that was on the right channel...that would be gay. but now you have a remote and can access all that stuff from a distance. it's a way to "point" to various elements of your program and get them to do things.

that's just one shitty example, but there are countless applications for which pointers are the best way to go.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Sep 20th, 2005, 3:01 PM   #20
meverha1
Newbie
 
Join Date: Sep 2005
Posts: 11
Rep Power: 0 meverha1 is on a distinguished road
They get pretty in-depth with creating pointers. i.e. pointers, pointers & arrays, pointers & structures, etc...

The problem is that because it's early in the book the author can't offer a compelling reason (with examples) for using them. Sure, he can say it's better to use "new" with a pointer to create an array but he can't offer a good code example to support it because we simply don't know enough C++ for a complicated example to make sense.

I think he would have been better off introducing the concept of a pointer and then addressing it later in the book.
meverha1 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC