What can classes be used for?
My book gives the example of a cat...but what can they be used for that is useful?
What can classes be used for?
My book gives the example of a cat...but what can they be used for that is useful?
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
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?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
Using DEV-C++ Under Windows XP
+------------------------------+
"No! Do, or Do Not. There is no Try..."
Classes can be used for dogs, too. Cats and dogs.Originally Posted by bumfluff
Last edited by 7stud; 04-06-2006 at 01:13 PM.
Nothing that the book gives me is really useful code at all. What can you write in C++ that would actually be useful?
Here is an example of using an array to store some data:
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> 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; }
So, to start, you can think of a class as an array that can store different types of values.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; }
Last edited by 7stud; 04-06-2006 at 01:18 PM.
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
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
Example: I want to play with 1000-digit numbers. What do I do? I create a class:
Once I'm done I can do this:Code:class my_int { int *digits; //other functions used to manipulate the digits }
Some advanced tricks will eventually lead us toCode: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
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.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
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.