Thread: kill me

  1. #1
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50

    Angry kill me

    m gona kill myself if i dont find a tutorial that actually explains itself. well ill get to the point. this code...
    Code:
    class Dog {
    public:
        void setAge(int age);
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    private:
        int age;
        int weight;
    };
    
    void Dog::setAge(int age)
    {
        this->age = age;
    }
    
    int Dog::getAge()
    {
        return age;
    }
    
    void Dog::setWeight(int weight)
    {
        this->weight = weight;
    }
    
    int Dog::getWeight()
    {
        return weight;
    }
    
    void Dog::speak()
    {
        cout << "BARK!!" << endl;
    }
    not explained at all, and this is the best tutorial that supposedly exists so can sum1 explain, line by line wat everythin means, i know its a big ask but im sure u can find it in your hearts
    sorry, got carried away

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I think I know why this tutorial is giving you some heartburn.. it does not provide you a sample driver program to let you see how everything works..

    all you are seeing right now essentially is a class and function implementation.

    If you want.. I can provide you a sample driver program that can tie all of this together.. and give you a chance to see how a complete program is supposed to work.


    hang in there son.
    Last edited by The Brain; 09-13-2004 at 03:37 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    yea, chuk that implementation prog my way

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    roger.. allow me like 20 mins to come up with something
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The program is a tutorial on a simple class. You can break the sample code up into two parts.

    First is the class defintion:
    Code:
    class Dog {
    public:
        void setAge(int age);
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    private:
        int age;
        int weight;
    };
    Next we have the implementation:
    Code:
    void Dog::setAge(int age)
    {
        this->age = age;
    }
    
    int Dog::getAge()
    {
        return age;
    }
    
    void Dog::setWeight(int weight)
    {
        this->weight = weight;
    }
    
    int Dog::getWeight()
    {
        return weight;
    }
    
    void Dog::speak()
    {
        cout << "BARK!!" << endl;
    }
    The first part describes what functions make up the class, and what variables make up the class. For now, dont worry about the public/private parts.

    The next part shows us the implementations of the functions that make up the class.

    Here is a sample which shows us how to use that class:

    Code:
    int main()
    {
    	Dog dog1;
    	Dog dog2;
    
    	dog1.setAge(3);
    	dog1.setWeight(50);
    
    	dog2.setAge(5);
    	dog2.setWeight(60);
    
    	cout << "Dog 1 is " << dog1.getAge() << " years old." << endl;
    	cout << "Dog 2 is " << dog2.getAge() << " years old." << endl;
    
    
    	return 0;
    }

  6. #6
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    hey da brain wats takin u so long, i gotta get off in a min

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Here is the tutorial.. complete with a driver program.

    One of the joy's of object oriented programming.. as you have just learned first hand.. is code reuse.. you can write any driver program you want.. but still be able to use the same class and functions if you wanted to

    Code:
    #include<iostream>
    using namespace std;
    
    //Here is your class
    class Dog {
    public:
        void setAge(int age);
        int getAge();
        void setWeight(int weight);
        int getWeight();
        void speak();
    private:
        int age;
        int weight;
    };
    
    
    
    //Here is a driver program I just whipped up
    int main()
    {
    	
    	//Create a Dog class object to allow access to class member functions
    	Dog pet;  
    
    	int input;
    
    	cout << "\n\nEnter your dog's age: ";
    	cin >> input;
    
    	pet.setAge(input);
    
    	cout << "\nEnter your dog's weight (in pounds): ";
    	cin >> input;
    
    	pet.setWeight(input);
    
    	
    
    	cout << "\n\n\n\nYour dog is " << pet.getAge() 
                 <<  " and weighs a hefty "<< pet.getWeight() << "lbs!!!   ";
    
    	cout << "\n\nMaybe you should put your dog on a diet!";
    
    	cout << endl << endl << endl;
    	 
    	pet.speak();
    	pet.speak();
    	pet.speak();
    
    
    return 0;
    
    }
    		  
    	
    
    
    
    //Here is your FUNCTION IMPLEMENTATION
    void Dog::setAge(int age)
    {
        this->age = age;
    }
    
    int Dog::getAge()
    {
        return age;
    }
    
    void Dog::setWeight(int weight)
    {
        this->weight = weight;
    }
    
    int Dog::getWeight()
    {
        return weight;
    }
    
    void Dog::speak()
    {
        cout << "BARK!!" << endl;
    }

    This code will compile. I'm not going to comment a lot on this program.. i think you'll be able to follow along with what's going on here now.

    In fact.. I would like to see you write your own driver program
    Last edited by The Brain; 09-13-2004 at 12:40 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    bithub's example is perfect.. it shows you you you can split up your program into 3 main parts...

    [header file]

    [driver program]

    [function implementation]




    once you have studied, "seperate compilation".. you'll see how you can save the header, driver, and implementation into their own individual files.. compile them into obj. (object files), and link them together to create a single .exe


    it's easy.
    Last edited by The Brain; 09-13-2004 at 03:52 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    *sniff sniff* what's that smell? Oh wait its another C++C_Forever. This dude doesn't even know what a function returning a value means but yet he is trying to learn classes.
    Woop?

  10. #10
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    C++C_Forever
    what is that?
    Keyboard Not Found! Press any key to continue. . .

  11. #11
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    check out this thread for some links to c++ tuts and a free book.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  12. #12
    Registered User Rare177's Avatar
    Join Date
    May 2004
    Posts
    214
    hmm if your so into learning C++ and arent happy with the tutorial you have, try buying a C++ book!!!!

    i would recommend Accelerated C++ or thinking in C++
    also the sams primer plus series are pretty good.

  13. #13
    I'll take you down! polonyman's Avatar
    Join Date
    Sep 2004
    Posts
    50
    thankz, that free book saved me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to kill a dragon with various programming languages
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 10-01-2007, 12:13 PM
  2. Kill Signal Number? - Help !
    By brett in forum C Programming
    Replies: 5
    Last Post: 06-20-2007, 03:39 AM
  3. need code to kill running application
    By Jinxed in forum Windows Programming
    Replies: 1
    Last Post: 01-31-2002, 09:06 PM