Thread: Class Use

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Class Use

    What can classes be used for?

    My book gives the example of a cat...but what can they be used for that is useful?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    They are usefull in representing aggregate data types (types/objects composed of smaller individual pieces). If you had to deal with all the variables, maybe dozens or hundreds, representing a complex system as individual components all the time, it would be very difficult to keep track of them not to mention the difficulties passing all of these values into or returning values from functions. Wrapping them all up into a class (or struct) makes thing much easier. Complexity also would otherwise increase when dealing with multiple such objects (arrays) unless they were formed into an aggregate type.

    If you wanted to keep track of a person's name, height, age, weight, gender, job, address, and ssn you could create 8 variables to store them. Passing them into function would be messy. If you want to deal with 10 such persons you would have 80 variables floating around your program or you'd have to make 8 arrays to hold everything.

    On the other hand, you can add them all to a class (struct) and then in your program you'd create 1 array of 10 instances of the class instead. Passing these to other functions would mean passing 1 parameter instead of potentially 8 making your code look cleaner and easier to read/debug/maintain.
    "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

  3. #3
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96
    On the other hand, you can add them all to a class (struct) and then in your program you'd create 1 array of 10 instances of the class instead. Passing these to other functions would mean passing 1 parameter instead of potentially 8 making your code look cleaner and easier to read/debug/maintain
    So if I wanted to keep a data base of personal information, as in your example, along with functions to do modifications on those variables. Lets say I have a class with all your example variables listed as private and some public functions to access and modify the variables. Would I be better off creating a vector where each element contains the entire class, functions and all; or have the vector as a private member of the class with each element containing only the struct of variables?
    Using DEV-C++ Under Windows XP
    +------------------------------+

    "No! Do, or Do Not. There is no Try..."

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by bumfluff
    What can classes be used for?

    My book gives the example of a cat...but what can they be used for that is useful?
    Classes can be used for dogs, too. Cats and dogs.
    Last edited by 7stud; 04-06-2006 at 01:13 PM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Nothing that the book gives me is really useful code at all. What can you write in C++ that would actually be useful?

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here is an example of using an array to store some data:
    Code:
    #include <iostream>
    using namespace std;
    
    void show(int num[], int size)
    {
    	for(int i = 0; i<size; i++)
    	{
    		cout<<num[i]<<" ";
    	}
    	cout<<endl;
    }
    
    int main()
    {
    	const int size = 3;
    	int numbers[size] = {10, 20, 30};
    	show(numbers, size);
    
    	return 0;
    }
    However, with an array all the data must be of the same type, in this case int's. With a class, you can store different types of data in it:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    class Data
    {
    public:
    	int num1;
    	double num2;
    	string str;
    };
    
    
    void show(Data aDataObj)
    {
    	cout<<aDataObj.num1<<" "<<aDataObj.num2<<" "<<aDataObj.str<<endl;
    }
    
    
    int main()
    {
    	Data myData;
    
    	myData.num1 = 10;
    	myData.num2 = 3.5;
    	myData.str = "hello";
    
    	show(myData);
    
    	return 0;
    }
    So, to start, you can think of a class as an array that can store different types of values.
    Last edited by 7stud; 04-06-2006 at 01:18 PM.

  7. #7
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    Ability to see the usefull stuff of C++, you don't get early in your programming days.

    All the classes are usefull for are better organization of your project.
    And you don't have an idea what that means until you live through horrible sphaggeti code hell.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Most example in a book are going to be fairly trivial examples that might not seem meaningful. The purpose in those examples is to show you how to do something. You then need to take that knowledge you learn and try to apply it to a more real world example. Any program where you need to manage collections (especially large ones) of objects that are logically represented as collections themselves (aggregates) of simpler data objects is a good candidate for use of a struct/class to represent those aggregate objects.

    You can expand upon the cat/dog example concept into perhaps a global airline tracking system where you manage thousands of airplane objects each consisting of an airplane type, flight number, current positional information, fuel status, passenger lists, crew lists, luggage information, estimated flight time, departure airport, arrival airport. Each of those items can themselves be sub-objects; a passenger for example can be a class that hold the name, age, weight, social security number, final destination city; an airline type can be a sub-type holding information such as manufacturer, max passenger capacity, max takeoff weight, empty airplane weight, range, airplane model number. If you have to manage thousands of these objects without using a struct/class to collect related items then you're going to have a big problem.

    The basic idea is to group the related things you need to keep track of into a concept that can be more easily dealt with throughout the program.
    "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

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Example: I want to play with 1000-digit numbers. What do I do? I create a class:

    Code:
    class my_int
    {
        int *digits;
        //other functions used to manipulate the digits
    }
    Once I'm done I can do this:

    Code:
    my_int n; //declare "n" to be of type "my_int"
    
    my_int function(my_int n)
    {}
    
    my_int m = function(n);
    
    //we can use my_int as any other datatype, like int and float
    Some advanced tricks will eventually lead us to

    Code:
    class my_complex
    {
        my_int real;
        my_int imaginary;
    }
    
    my_int a, b;
    a = "12345678901234567890";
    b = "1248163264128";
    my_complex c(a, b);
    c *= a + b;
    
    //etc
    You can think of a class as a representation of an object, and write functions to perform operations on entire objects at a time. Objects may contain other objects, objects may interact with other objects, objects may inherit the properties of other objects, and so on. This is why C++ is called an "Object Oriented" language.

    CProgramming.com has this pretty good tutorial which, among other things, mentions classes. goto
    Last edited by jafet; 04-07-2006 at 12:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM