Sams Teach Yourself in 24 hours.....

This is a discussion on Sams Teach Yourself in 24 hours..... within the C++ Programming forums, part of the General Programming Boards category; I don't think I like the flow of this book. I was reading a CHAPTER on pointers and it went ...

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Sams Teach Yourself in 24 hours.....

    I don't think I like the flow of this book. I was reading a CHAPTER on pointers and it went along describing and explaining it. The next chapter was about references. And it was more less saying that using pointers can lead to memory leaks and that it is not a good idea to use pointers. Another thing, it teaches the goto() fucntions and THEN tells you not to ever use it. Things like these are what I think makes this book a poor read. I think I should just cough up the money and get the Sams Teach Yourself in 21 Days. I don't think I like Liberty's books but I haven't tried any others. I have read some reviews though. Seeing as he wrote,"Teach yourself C++ in 20 minutes" just goes to show.... .

    A question:
    If pointers are such a bad thing then why do a lot of people use them? Also, I see pointers used a lot more than references. From reading Liberty he explaines it as references work a lot better they are safer and easier to use and explain.
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    I think I should just cough up the money and get the Sams Teach Yourself in 21 Days
    Link removed by mod
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  3. #3
    1479
    Join Date
    Aug 2003
    Posts
    253
    Thanks for the link. It will come in handy when I start using V C++. It will complement the book I bought for that,"Practical Visual C++ 6". Thanks again though.
    Knowledge is power and I want it all

    -0RealityFusion0-

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ALL Sam's books are horribly written. Jesse Liberty should be put in jail for all the pain and suffering he has caused beginning C++ programmers with his trashy C++ "novels".

    Either get the Dietel C++ book or "Ivor Horton's Beginning C++".

    A question:
    If pointers are such a bad thing then why do a lot of people use them? Also, I see pointers used a lot more than references. From reading Liberty he explaines it as references work a lot better they are safer and easier to use and explain.


    1)For polymorphism
    2)Dynamic allocation of memory(this is where memory leaks are possible).
    Last edited by 7stud; 08-21-2003 at 03:06 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    1)For polymorphism
    Isn't that just a fancy term for function overloading? And what does that have to do with pointers? Anyway, what about:

    3) Efficiency

    And RealityFusion, go pick yourself up a better book.
    Last edited by funkydude9; 08-21-2003 at 03:19 PM.
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    56
    Day 2 of teach yourself ansi bastardization in 21 days has this piece of code

    Code:
    #include <iostream.h>
    int Add (int x, int y)
    {
    
    	cout << In Add(), received  << x <<  and  << y  << \n;
    return (x+y);
    }
    
    void main()
    {
    	cout << Im in main()!\n;
    	int a, b, c;
    	cout << Enter two numbers: ;
    	cin >> a;
    	cin >> b;
    	
                    cout << \nCalling Add()\n;
    	c=Add(a,b);
    	
                    cout << \nBack in main().\n;
    	cout << c was set to  << c;
    	cout << \nExiting...\n\n;
    }
    When trying to compile it, there are all sorts of errors. I moved on to another book after seeing that.

    I mean if at day 2 they are presenting such sloppy and non-functional code, what will happen in the next 18 days?

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    I don't have the "24 Hours" book...

    But, I do have "Teach Yourself C++ in 21 Days", by Jesse Liberty. I found this book EXCELLENT as a beginning self-learning book. It is clearly written and easy to understand. I also like the structure provided by the questions/exercises - answers/solutions.

    I have a few more advanced (and more complete) books, but when I need to look something up, I look in "21 Days" first, because if the answer is in there, it will be easy to find and easy to understand.

    [EDIT]
    PROBLEMS WITH THE SAMPLE CODE:

    1- I do recall having to make some corrections per the errata info on the web page.

    2- I couldn't print to the printer from a Windows console using his examples. This is an OS issue.

    3- He does show some common non-ANSI functions (from conio.h?), but these are clearly identified.

    4- At least once, I had to use the downloaded code, because I couldn't find the typo I had made when entering the code!
    Last edited by DougDbug; 08-21-2003 at 04:29 PM.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    161
    > Isn't that just a fancy term for function overloading? And what does that have to do with pointers?



    Code:
    class Base
    {
      virtual int func(int i)
      {
        return i + 10;
      }
    };
    
    class Derived: public Base
    {
      int func(int i)
      {
        return i + 100;
      }
    };
    
    
    int main()
    {
      Base* p = new Derived;
      p->func(5); 
      delete p;
      return 0;
    }
    The line
    Code:
    p->func(5);
    will call func() in Derived (not in Base) even though p of type Base*.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Day 2 of teach yourself ansi bastardization in 21 days has this piece of code
    Obviously an old version, the version I have does not use the old .h headers.

    And I found it to be a fairly good tutorial and an even better reference...It covers a little of most basic C++, so it makes a great starting point.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571

    Re: Sams Teach Yourself in 24 hours.....

    Originally posted by RealityFusion

    A question:
    If pointers are such a bad thing then why do a lot of people use them? Also, I see pointers used a lot more than references. From reading Liberty he explaines it as references work a lot better they are safer and easier to use and explain.
    Pointers aren't BAD per se. They're easy to misuse. There are ways to use pointers more intelligently, like using pointer wrapper classes, such as std::auto_ptr or boost::shared_ptr. The major reasons people use pointers is that pointers can be used for dynamically allocated memory, and they aren't constrained to pointing at only one object. You can make the pointer before the object itself.

    Pointers and dynamic memory allocation, by themselves, are often problematic. It's very useful, but unless you take care, it's not very safe. For example, this code:

    Code:
    void DoSomething(){
      int * ip(NULL);
      float * fp(NULL);
      ip = new int[25];
      fp = new float[30];
      /*...*/
      delete[] ip;
      delete[] fp;
    }
    is a memory leak waiting to happen. What if "fp = new float[30];" throws a std::bad_alloc? 25 integers are allocated that can never be deallocated.

    Further, people aren't always smart enough to set their pointers to NULL unless they point at something valid, and THOSE problems can be a real bear to find and debug.

    Overall, to make a food analogy (so sue me, I like cooking), pointers are like a sharp chef's knife -- absolutely needed for many tasks, but you need to know how to use them without cutting yourself. Wrapper classes are like hiring a professional chef for that part of the work -- he can use the knife with perfect safety, and you get the benefit.
    Last edited by Cat; 08-21-2003 at 06:22 PM.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)For polymorphism
    --------------------------------------------------------------------------------
    Isn't that just a fancy term for function overloading?


    No, polymorphism has nothing to do with function overloading.

    And what does that have to do with pointers?

    You cannot get polymorphic behavior with references--only pointers.

    Anyway, what about :

    3) Efficiency


    References are more efficient than pointers--there's no copying involved.
    Last edited by 7stud; 08-21-2003 at 06:41 PM.

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by 7stud
    You cannot get polymorphic behavior with references--only pointers.
    Try this code and see what happens:

    Code:
    #include <iostream>
    
    class Base{
    public:
    	virtual void Say(){ std::cout << "Base!" << std::endl;}
    };
    
    class Derived: public Base{
    public:
    	void Say(){ std::cout << "Derived!" << std::endl;}
    };
    
    int main(){
    	Derived d;
    	Base & bref = d;
    	bref.Say();
    }

    References are more efficient than pointers--there's no copying involved.
    Actually, they're exactly the same; a reference and a pointer are implemented identically (both store the address of an object). The only difference is in the rules of usage.

  13. #13
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    3) Efficiency
    References are more efficient than pointers--there's no copying involved.
    Could you give an example??? I always thought references as constant pointers.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things dont come easy in life!!!

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    References are more efficient than pointers--there's no copying involved.

    --------------------------------------------------------------------------------
    Could you give an example???


    Sure. I ran the following code:

    Code:
    void func(int& r)
    {  
        r = 100;
    }
    
    int main()
    {
        int a = 10;
        int& rA = a;
        
        func(rA);
        
        return 0;
    }
    against the following:
    Code:
    void func(int* p)
    {  
        *p = 100;
    }
    
    int main()
    {
        int a = 10;
        int* pint = &a;
        
        func(pint);
        
        return 0;
    }
    and I pulled out my stopwatch and I timed both programs. The one with the reference parameter was faster.


    How's this then?

    [EDIT]

    And what does that have to do with pointers?

    You can get polymorphic behavior with pointers.

    Anyway, what about :

    3) Efficiency


    Pointers aren't more efficient than references. (Stupid book...stupid, stupid book.)
    [/EDIT]
    Last edited by 7stud; 08-21-2003 at 07:54 PM.

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Well when your passing it as a pointer, your passing the address, correct? But when your passing it as refrence, what exactly are you passing?
    Well, there are a few things wrong with your code:

    1) It does not work.
    2) It does not work.
    3) It does not work.

    Hope this helps.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with file
    By nevrax in forum C Programming
    Replies: 12
    Last Post: 04-23-2007, 07:18 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. Time between Military Hours
    By StarOrbs in forum C++ Programming
    Replies: 18
    Last Post: 03-01-2005, 06:46 PM
  4. Sams Teach Yourself Game Programming in 24 Hours
    By Android in forum Game Programming
    Replies: 17
    Last Post: 06-08-2004, 11:20 PM
  5. Sams Teach Yourself ?
    By CAP in forum C Programming
    Replies: 4
    Last Post: 09-03-2002, 09:16 PM

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