Not getting it

I fallen behind in class. Stress, generally and mostly stress. The semester isn't over yet, I can get back on track I know it. I plan on going to some tutoring on monday but thats too long of a wait to start getting back on track. So here I am, and here is what I'm not getting.

I'm not getting the point of using classes, pointers, constructors, deconstructors, and the likes. I understand them, and can write/type/ and use them but other than "it's efficient", I'm not getting the point of using them and where it would be optimal to use them.

So if someone can please explain it to me, I will be so much closer to getting back on track. I'm reading the tutorial section of cplusplus.com right now (like http://www.cplusplus.com/doc/tutorial/classes/ for example) but idk.
The three main reasons for using classes are:

-Organization (similar to using functions)
--For example instead of having:
1
2
3
4
5
6
7
8
9
char* cstr = nullptr;
std::size_t cstr_size = 0;

void resize(char*& cstr, std::size_t old_size, std::size_t new_size, char filler = '\0');
char& getch(char* ctr, const std::size_t size, std::size_t index);
//...

resize(cstr, cstr_size, 80, 'I');
char ch = getch(cstr, cstr_size, 45);

Simply use a "string" class:
1
2
3
std::string s(80, 'I');
s.resize(100, 'a');
char ch = s[45];


-Reusability
--You create modular code that you can reuse in other projects, saving you time and effort (like creating a mold with which you can now cast hundreds of copies)
--For examples, you can simply look at the libraries provided as part of the C++ standard, like the STL containers, the clock, std::functional, etc.
---In fact, as you are learning C++ programming in your class, you're probably using at least two classes all the time: std::istream and std::ostream, of which std::cin and std::cout are objects.
http://www.cplusplus.com/reference/iolibrary/
http://www.cplusplus.com/reference/stl/

-Modelling something
--Classes are a great way to create a model, for example let's say you wrote a "Ball" class, you'd probably be able to do stuff like this:
1
2
3
4
5
//Set radius
Ball myBall(13.99f);
myBall.move(45.0f, 45.0f);
myBall.rotate(3.0f);
myBall.shrink(myBall.radius()/2.0f);



Here are some reasons to use pointers:
-Polymorphism
--Let's say you have an Animal class with different derivations like Cat or Dog. Now let's say you want to know the sound an animal makes but you really don't care which type of animal it is.
1
2
3
4
5
for(std::size_t i(0); i < 5; ++i){
    Animal* myAnimal = getRandomAnimal(); //This returns a pointer to some random derivation
    std::cout << myAnimal->sound_str() << '\n';
    delete myAnimal;
}

And an example output may be:

Meow
Bark
Moo
Neigh
Hi

where you were actually using five different derivation types, but since you used a pointer to the base, you really didn't have to care. Note how I didn't have to specify Cat or Dog.

-Pointers are one way to modify the original variable
--If you write a function whose purpose is to modify the parameter given, you'll need to either use pointers or references (references are a better alternative)
--One example where you may want to use pointers this way is an optional request
1
2
3
void print_random_animal_sounds(std::size_t num_to_generate, Animal* = nullptr);
//Here I provide the option for the user to retrieve a dynamic array
//   with the various animals created 


-You may need a dynamic array instead of a fixed size array
--For example, you may ask the user to enter in data, but it is the user who specifies how many data points he/she will enter.

Specifically with pointers...
Aside from learning how to use them for the first time, you should rarely have to use a "raw pointer", i.e.
 
T* ptr; //where T is some type, e.g. int 

You really should prefer when possible:
-references for passing parameters
--Easier to use and generally less prone to errors (though you can still create problems if references aren't handled correctly)
-containers for dynamic storage space
--Does all the hard work for you in managing raw pointers
--http://www.cplusplus.com/reference/stl/
-smart pointers for other places you need a pointer
--Like containers as far as managing raw pointers
--http://www.cplusplus.com/reference/memory/

Hope this helps. I also struggled with the point of using classes while I was learning the basics.
Last edited on
Topic archived. No new replies allowed.