Just learned classes and...

This is a discussion on Just learned classes and... within the C++ Programming forums, part of the General Programming Boards category; I would like to know what to test myself with. I know pretty much everything to a basic understanding up ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Just learned classes and...

    I would like to know what to test myself with. I know pretty much everything to a basic understanding up till now so im just curious. Also I dont want to make tic-tac-toe or indian poker. only because i already made a fairly sucky tictactoe and I dont know how to play indian poker lol... I havent gotten any real suggestions from the other message board I was at.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    4

    Talking Well... I just finished an easy project i made up, would you like to try and copy it?

    Okay, so i just made an age evalutor program. It's simple.... Enter your age, prosses info, tell comments about how old you are. i'll upload the .cc file and you can complie it. don';t jusr copy the source instead try and figure it out on your own and only look if you need a little help . you'll need to know basic ammount of if loops, input, and crativity. wish you the most of luck :-D.


    enter age [ ]
    prosses
    say you are X old
    say something like oh, you're a preteen only.

    thanks for using my age prog.




    hope you enjoy it. i'll have more little programs ideas once i've made more. currently i'm making a caculator that can add,sub,mult,devide,sq, ect..
    -jumbo
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    I like your suggestion, but I have already made one of those to lol!, but not to that extent. You just had alot more else ifs in it. What im trying to do is have a project thats going to test me , to see if I have a basic understanding of classes since that was the last thing I learned.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    4
    well, i have no idea what type of "test" you would like :-p. i have one suggestion though. i made an ATM bank account in one of my classes. basically it asked you if you wanted to get money,deposit, all the atm functions. you press the number, and if you're withdrawing money you'll need to type in your name and pin. the program get's the data base with all the names/ pins form a different file then reads it. it would take awhile, at least for me it did. i wrote it in C too.. so yea. i'm kind of in the same hole that you're in. i wanted to test my self but i don;'t know what to do . maybe we could bounce ideas off each other and find a good project to "test" our skills.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    ya...

    I like your idea. Im doing something sorta similiar, I just thought of it. Im not familiar with classes at all so this might be difficult. Im going to use a class to call a file and display it to the screen. but what im really aiming to do is either have those files hold my code or use them to interact with the user. I got a basic outline in my head of what its going to look like.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I like your suggestion, but I have already made one of those to lol!,
    All the better. It is a good exercise to do the exact same program, but with classes, so you can see the differences.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    26
    Lol, 7stud, you'll appreciate this more than anyone else here, but that's what I'm doing with my Orc at the moment.

    Attempting to make a class (monster) So that I could just use it to define Orc. Have some troubles geting the variables to pass about properly, (IE - Something that should be returning 11, is returning 0?)

    But that's another story.

    Class's confuse me, but I will succeed!
    I'm using Dev-C++ 4.9.8.0

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Well Im doing the bank idea... and using alot of file i/o to do it just to test my abilities with it. If you want to see what I have done so far its right here:
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by Smeep
    Lol, 7stud, you'll appreciate this more than anyone else here, but that's what I'm doing with my Orc at the moment.

    Attempting to make a class (monster) So that I could just use it to define Orc. Have some troubles geting the variables to pass about properly, (IE - Something that should be returning 11, is returning 0?)

    But that's another story.

    Class's confuse me, but I will succeed!
    As a first pass, just think of classes as fancy arrays. Like an array, a class is just a container with data inside it. However, instead of accessing the data with an integer index as you do with an array:

    myarr[0]

    you access the data with a name:

    myobj.color

    which makes it much easier to know what the data is. After all, how descriptive is myarr[0]? So, a simple class would look like this:
    Code:
    class Apple
    {
    public:
    	string color;
    	int size;
    };
    The useful thing about that class is that unlike an array, it can store different types inside it. With an array, you can only have all int's, all doubles, etc. inside it. With a class, you can have all different types inside, and you just access them by name:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Apple
    {
    public:
    	string color;
    	int size;
    };
    
    int main()
    {
    	Apple MyApple; //create your 'array substitute'
    
    	//assign the members some values:
    	MyApple.color = "red";
    	MyApple.size = 5;
    
    	//output the values:
    	cout<<MyApple.color<<endl
    	cout<<MyApple.size<<endl;
    
    	return 0;
    }
    One of the most interesting 'types' a class can contain inside it is a function. It's almost just like calling a regular function--except you precede the function call with the name of the container:

    MyApple.displaymembers();

    A function inside a class can also operate directly on the other members of the class. For instance, this is what adding a displaymembers() function to the Apple class would look like:
    Code:
    class Apple
    {
    public:
    	string color;
    	int size;
    
    	void displaymembers()
    	{
    		cout<<color<<endl
    		cout<<size<<endl;
    	}
    };
    The displaymembers() function can 'see' all the other class members. Here is an example of how you would use that class function:
    Code:
    int main()
    {
    	Apple MyApple; //create your 'array substitute'
    
    	//assign the members some values:
    	MyApple.color = "red";
    	MyApple.size = 5;
    
    	//call Apple function:
    	MyApple.displaymembers();
    
    	return 0;
    }
    Last edited by 7stud; 04-24-2005 at 11:18 PM.

  10. #10
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    The data is best kept private so you can explictly control how objects are used. The public section provides the interface to the user. Also if there is an error causing a member to take an illegal value you know that it was one of your member functions.

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,810
    Quote Originally Posted by Kirdra
    The data is best kept private so you can explictly control how objects are used. The public section provides the interface to the user. Also if there is an error causing a member to take an illegal value you know that it was one of your member functions.
    To further expand on what Kirda is saying. He is talking about making the data withing your class only accessable by your class via the private keyword.
    Small Example.
    Code:
    class Dog
    { 
        public:
            //this function will set the age of our dog
            void setAge(const unsigned int setAge)
            {
                age = setAge;
            }
            //this function will get the dog's age
            unsigned int getAge() const
            {
                return age;
            }
        private: //this make the data only accesable to your class 
                     //you cannot do .age on this or you will get an error
            unsigned int age;
    };
    Woop?

  12. #12
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    What are the restrictions on protected:?

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    15
    Quote Originally Posted by 7stud
    As a first pass, just think of classes as fancy arrays. Like an array, a class is just a container with data inside it. However, instead of accessing the data with an integer index as you do with an array....
    I know it's an older post but I found that it was VERY helpful in the fact that it was broken down and made understandable, especially to me as a newbie to programming.

    /me shows his gratitude.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm glad it helped.

  15. #15
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    To the OP: Have you experimented with inheritance, abstract base classes, templated classes...?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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

Similar Threads

  1. C++ Classes: Use, Misuse...Confusion.
    By Snorpy_Py in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2006, 02:46 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

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