![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programmer
|
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
|
|
|
|
|
|
#12 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#13 |
|
Programmer
|
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. ![]() |
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#15 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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 mainnow 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 mainlooks 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. |
|
|
|
|
|
#16 |
|
Newbie
Join Date: Sep 2005
Posts: 11
Rep Power: 0
![]() |
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++. |
|
|
|
|
|
#17 |
|
Expert Programmer
|
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.
|
|
|
|
|
|
#18 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#19 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#20 |
|
Newbie
Join Date: Sep 2005
Posts: 11
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|