I'm Lost in Programming Limbo

Pages: 12
I've been googling left and right for project ideas so that I build my knowledge base of C++ slowly. I've done the basics, but when I think up projects I end up doing ones that turn out to just be using what I already know and don't push learning anything knew or I think of something so hard I back off and end up not finishing it. I tried Project Euler and found that my mathematics skills are majorly rusty and don't know where to brush up on them. Is there any projects list somewhere for improving your C++ skills? I'm in limbo between beginner and intermediate skill level as I know the basics but don't feel I know anything more than that.
Have you built any custom containers, ie linked lists, binary trees, hash maps, etc? They are a god excersize in pointers if you aren't already comfortable with them.
No, the most I've done is classes (basic classes with inheritance, composition), functions, data types, loops (do, while, do/while, for), statements (if, switch), and both standard and file IO. So definitely beginner level stuff. Only glazed over pointers and references in the past. Did a address book back in November (never finished it because I started tweaking in File IO but never finished it due to the holidays).

I posted the code in January to allegro.cc and was told that:

Setters in general should take a const reference:
1
2
3
void setName(const string& first, const string& last);
void setAddr(const string& addy);
void setPhone(const string& phone);


Which I didn't know because all the books and tutorials I read never said a thing about doing it that way. So I apparently have been using the wrong places to learn from.
Getter/Setter combos should be done via function overlaoding - you should never have to type the word 'get' or the word 'set':
1
2
3
4
5
6
7
8
9
void SomeVar(const SomeType &from)
{
    /*check from*/
    somevar = from;
}
SomeTime SomeVar() const
{
    return somevar;
}
@LB
I'd classify that as a personal preference, personally I do prepend get/set to the variable name as the method name.

@BHXSpecter
It is generally better to pass a constant reference when passing objects around as it will prevent copying the object.
Last edited on
Though, it is generally better to just pass primitives by value instead of const reference. (Yet another contradiction...)
Instead of discussing how to implement setters and getters, think if you actually need them.
Accessors break encapsulation.
Check out Tell Don't Ask and Demeter's Law

Stop studying the language.
You better look for algorithms and design patterns.
There are programming contest. A good book is Programming Challenges (by Skiena)
I do usually put set/get in front but actually I think L B's method is better.

As for breaking encapsulation, having the user access variables themselves is worse (also, you can't have read-only or write-only access because C++ lacks a keyword similar to C#'s readonly).
@chrisname:
1
2
3
4
5
6
7
8
class SomeClass
{
    SomeType somevar;
public:
    const SomeType &SomeVar;
    SomeClass() : SomeVar(somevar) {}
    //etc...
};
As for breaking encapsulation, having the user access variables themselves is worse
Of course the alternative for accessors it is not public members. Still, I'm not sure if it is worse.
Okay, so algorithms, design patterns, contests, and Programming Challenges? Anything else I should look into and study? I think that is one of my major flaws, I have focused on C++ and never really kept it general like some places recommend. This narrow view has truly caused a huge handicap in programming.

Also thanks for the input about classes and the accessors.
Less study, more fingers-on experience. Get coding! It's the fastest and easiest way to learn ;)
Last edited on
If you, like me, haven't gotten into the mathematics behind programming, I recommend that. I think the biggest problem I have with programming is that I lack the theoretical background. Although you can get by without that much mathematics in programming I suspect that once I do understand it, I'll be a lot better. I'm currently just bouncing around Wikipedia, reading articles recursively (i.e., reading every article about things I don't understand, and then reading all the articles in those articles that I don't understand and so on) just to get a significant breadth of knowledge. Meanwhile, I'm writing everything down that I don't fully understand so that in a month or so when I've built up a repertoire of information I'll be able to gain depth of knowledge into the specific subjects in which I lack understanding.

I started from String theory but you may want to start somewhere else. I don't suppose it matters, but you could start from any physics, computer science or mathematics article (good ones would probably be information theory, discrete mathematics, linear algebra and lambda calculus (especially if you're into functional programming)). Every time you come across something you don't understand, read the article about it. If there's anything in that article you don't understand, read the article about that.

I personally just scan the articles because I want to build breadth before depth as I said before.

@ne55
Well, what is the alternative? Some objects need to have attributes that can be changed dynamically and I don't see a better way to do that than to provide functions like "setx" and "getx" (with or without "set" and "get").

@L B
That's kind of hackish. I still think accessors are better.
Last edited on
Hackish? In what way? :\
It doesn't look natural, and you can't abstract it away. If you could encapsulate it in another class then it would be better but I don't see how you could really do that.

Less study, more fingers-on experience. Get coding! It's the fastest and easiest way to learn ;)

@LB I do code but everything I do is just little play apps like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	string fileName;
	char nums;
	cout << "Enter the filename: ";
	cin >> fileName;
	cout << endl;
	
	ifstream inFile;
	inFile.open(fileName.c_str());
	inFile.get(nums);
	while(!inFile.eof())
	{
		cout << nums;
		inFile.get(nums);
	}
	
	inFile.close();
	return 0;
}


Since I don't know many advanced things I just keep doing things like that. Plus like I said, I have focused on learning programming by focusing on C++ which I have been told that it is a bad thing to do. I don't know if that is true though, but I feel like I'm getting nowhere fast, thus this thread.
@chrisna:
Take a look at Tell Don't Ask and Demeter's Law
The idea is to let the object that has the information to do the work. ¡And who better suited to handle its internals than the object itself!
So getters and setters are warnings that you put the logic in the wrong place.

An example.
It would be wrong to introduce my hand into your pocket, extract your wallet and count the money (of course, I give it back).
But it's OK to give mine to the wallet inspector in order to find irregularities (like fake bills).

That said, you could program geometric algorithms without having to worry about coordinates. You just need sum of vectors and multiplications (the k-kinds)

There are times, however, when is your responsibility to provide that information.
By instance, a container must give a way of access its elements. Still a lot of things can be done with just
_ ¿member?
_ insert
_ remove
_ for_each
Write larger programs - think of existing ones that you think you know how to remake, and remake them.

As for not just C++, try learning a scripting language like Python (personally it was very hard to me to move out of the strongly typed mindset). Then, once you've done a load of object oriented programming in C++ (or even in Java), try a functional language like Haskell.

A variety is good, but endless studying and simple programs will not help very much ;)
@ne555,
Your analogy is more like direct access than accessor functions. A correct analogy would be tapping me on the shoulder and asking me to count the money myself and then tell you how much (getting) or telling me how much money I have (setting).

I use getters and setters so that a class' behaviour can be changed and so that it can communicate its state to external classes/functions/whatever.

I will, however, take a look at the articles you mentioned.

[edit] I see what you mean now that I've started reading Tell, don't ask.
Last edited on
Trying to find the Tell, Don't Ask and Demeter's Law, but not sure if I'm finding the right articles. One leads me to the Pragmatic Bookshelf while Demeter's leads me to Blogs and wikis. More evidence that I'm not good at Google searching.

[REVISION]
Yes I know that the pragmatic bookshelf Tell, Don't Ask has Law of Demeter in it, but didn't know if that was the article or if I was supposed to be looking for two different articles.
Last edited on by closed account z6A9GNh0
Pages: 12