Questions about C++

Bustaballs

Artifact Armor
Adventurer
Joined
Nov 12, 2004
Messages
1,490
Age
39
Location
Kansas
Nation Banner
Windy
Main Job
Beastmaster-Male
Secondary Job
WhiteMage-Male
Third Job
RedMage-Male
Server
Quetzalcoatl
FFXIV
Bustaballs
Okay. First off, I've done plenty of programming in various versions of BASIC. I'd say I was an intermediate in VB6 but no pro. I've learned and used many many scripting languages for different programs.

I've tried many times to dip my hands into C++ and I always seem to fail. Every time I do it, I grab a book and right away from one of the very first tutorials, I get compile errors. Anytime I download some code online and try to compile it, I can't.

I just recently decided to take another try and downloaded dev-c++. I looked over a Hello World tutorial, typed up the code, and compile error. That just ticks me off when I get a compile error from Hello World. I've also used different versions of Visual Studio and I believe I've tried Borland in the far past.

I was wondering if someone could help me get started until I can afford some books.

I've only ever used languages that were OS specific (outside of python and flash), so I was curious as to what exactly is involved when coding for a different OS. Would you need a different compiler to make a Hello World app for a linux distro? How exactly does that work?
 
C++ is a beast best toyed with when one has much patience.

Foregoing all the compiler garbage... Make a file in text editor, "filename.cc"

int main(int argc, char* argv[])
{
return 0;
}

In visual studio, that should compile immediately and pop up a black box and quickly close the box.

Linux CLI compiling in two steps:
g++ -c filename.cc // this yields an object file
g++ -o filename.o // pretty sure this will make an executable named filename

In one;
g++ exefilename filename.cc // should compile and link making the file exefilename



Yet more advanced:
#include <iostream> // If you see <iostream.h>, the .h versions are not standard
#include <cstdio>

using namespace std; // You learn this is bad later, but for now go with it

int main(int argc, char* argv[])
{
cout << "Hello, world.";
return 0;
}

iostream opens up the ability to use cout, cin etc... If you want to research it more, look up "STL iostream". You may have to look a bit for beginner friendly stuff.

I did not compile any of these, but I suspect they should work. Let me know if you run into problems or if you want more assistance. C++ I can do very very well lol.
 
Yeah. I understand all of the includes are just allowing external libraries to be used. Your code compiled just fine for me but disappear instantly upon running. I'll look up the command to make the program wait until the user presses a key to end. Thanks for at least getting me started. =)

I hope to be making awesome text based games within a week and awesome Atari 2600 game clones within a month or less.
 
While we are on C++, if anyone is aware of a good tutoring program for C++ (specifically object-oriented design with C++) please shoot me a pm or let me know. I'm taking an object-oriented programming class and just can't follow my prof's teaching style. Basically, I feel completely lost and I didn't feel like that in previous programming classes, so I'm not sure where the gap is. I feel like I just need someone to sit down with me and go through the code with me step-by-step or something because I'm completely clueless in the lectures and I've gotten pretty good grades in programming classes in the past. Any help is appreciated. Thanks!
 
Yeah. I understand all of the includes are just allowing external libraries to be used. Your code compiled just fine for me but disappear instantly upon running. I'll look up the command to make the program wait until the user presses a key to end. Thanks for at least getting me started. =)

I hope to be making awesome text based games within a week and awesome Atari 2600 game clones within a month or less.

Yup, that sounds about right.

declare a string inside main (I think string is in cstdio) and you can stop input like so:

string myTempString;
cin >> myTempString;

You have ambitious goals... in fact, I really think you're setting yourself down a very discouraging path. C++ has a very large number of lessons to learn if you don't already know about data structures and object orientation.

Just out of curiousity...
How do you intend to display the visual elements? OpenGL? XNA? Direct3D?
 
While we are on C++, if anyone is aware of a good tutoring program for C++ (specifically object-oriented design with C++) please shoot me a pm or let me know. I'm taking an object-oriented programming class and just can't follow my prof's teaching style. Basically, I feel completely lost and I didn't feel like that in previous programming classes, so I'm not sure where the gap is. I feel like I just need someone to sit down with me and go through the code with me step-by-step or something because I'm completely clueless in the lectures and I've gotten pretty good grades in programming classes in the past. Any help is appreciated. Thanks!

I know of no program, but if you can think of specific questions I can try and do my best to answer them. Object orientation has 3 real tenets: Polymorphism, Encapsulation, and Abstraction.

I've not read the fundamental concepts section, but it looks promising.
http://en.wikipedia.org/wiki/Object-oriented_programming

The wiki lists more than 3, but really it is all just extensions of the above 3 ideas.

Fire your questions at me and I'll see if I can't help.
 
c++ is a lot more complicated then Visual Basic. You will get A LOT of compile errors when you first start out.

When you download code, there will usually always be compile errors for some reason, I always got the same when I did it. You have to look through it and see if you find anything not supposed to be there.
 
Yup, that sounds about right.

declare a string inside main (I think string is in cstdio) and you can stop input like so:

string myTempString;
cin >> myTempString;

You have ambitious goals... in fact, I really think you're setting yourself down a very discouraging path. C++ has a very large number of lessons to learn if you don't already know about data structures and object orientation.

Just out of curiousity...
How do you intend to display the visual elements? OpenGL? XNA? Direct3D?

I plan on eventually being able to make games on various platforms one day long down the road. I'd like to be able to make ports of my creations for Linux distros, Mac, and various console and handheld systems via homebrew. When the Pandora comes out, I'll probably be coding on it. No, I WILL be coding on it even if I have to use GLBasic.

So, definitely OpenGL. I'll probably mess with Direct3D as well, though. I hear it's easier to learn and use.
 
While we are on C++, if anyone is aware of a good tutoring program for C++ (specifically object-oriented design with C++) please shoot me a pm or let me know. I'm taking an object-oriented programming class and just can't follow my prof's teaching style. Basically, I feel completely lost and I didn't feel like that in previous programming classes, so I'm not sure where the gap is. I feel like I just need someone to sit down with me and go through the code with me step-by-step or something because I'm completely clueless in the lectures and I've gotten pretty good grades in programming classes in the past. Any help is appreciated. Thanks!

I'm currently looking over tutorials on this site:

http://www.cprogramming.com/tutorial.html

I used to use Planet Source Code a lot when I was working on stuff in Visual Basic and it helped me tremendously.

http://planetsourcecode.com/
 
Ok, here goes...

This was our first assignment (which I failed miserably on for some reason):
Scenario/Summary

Program Title: The Days of the week

Write a program that contains a class that implements the days of the week. The program should be able to perform the following on an object of the class.

1. Set the day
2. Print the day to the computer monitor (not the printer)
3. Return the day

We will use this same basic program next week to add additional operations and incorporate additional object oriented concepts.
Deliverables

A zipped folder that includes the complete solution folder. See the video on how to collect, zip, and submit your files to the dropbox.

L A B S T E P S

STEP 1: Create the Project and Main Class

Create a new console application project and name it "Week1Lab_YourName".

Create a new class called DayOfTheWeek. The class should have a data member that can store the day of the week such as Mon for Monday, Tues for Tuesday etc...

STEP 2: Create the member functions

Create the necessary member functions that will perform the required operations outlined in the lab summary above.

Call these functions setDay, printDay and getDay.

STEP 3: Create a main() program

Write a main program that will instantiate two objects of the class DayOfTheWeek. Use these objects to test the various operations on this class.

Using the examples in the lectures and reading to perform the required operations.

Basically, the way the program should work is setDay should ask the user to enter a number (1-7). getDay should translate that number into a day string (Monday, Tuesday, etc) and printDay should then tell the person the day of the week corresponding to the number they entered.

Here is my class (and feel free to be as critical as possible):

Code:
class DayOfTheWeek
{
public:
	void setDay();
	void getDay(int);
	void printDay() const;

private:
	char dayName;
	int dayNumber;

};

The getDay function is pretty common-sense -- nested if statements. I don't need to return any error codes or anything if the person enters anything outside of numbers 1-7 and I don't need any loops to keep requesting new day input. printDay is pretty obvious, too. The setDay and main() functions have me all confused.

I showed my code to my teacher and basically all he said was "I see several problems with it." Boy, was that helpful. I really need to know how to call functions in a class in the main() function properly and I should be good from there.
 
That depends on the function. if you have something like this(and its been a few years so the objects are not going to be perfect):

String SetDay(int Day)
{
Select day
1:
return "Monday";
2:
return "Tuesday";
etc(I forget proper syntax of a case statement)
}

Then under main() it would just be

Main()
{
char DayOfWeek[10];
DayOfWeek = SetDay(3);
}

But i'm trying to remeber this from like 4 years ago.... so don't expect this to compile.
 
That depends on the function. if you have something like this(and its been a few years so the objects are not going to be perfect):

String SetDay(int Day)
{
Select day
1:
return "Monday";
2:
return "Tuesday";
etc(I forget proper syntax of a case statement)
}

Then under main() it would just be

Main()
{
char DayOfWeek[10];
DayOfWeek = SetDay(3);
}

But i'm trying to remeber this from like 4 years ago.... so don't expect this to compile.

Not to knock this, as it is valid, there are however much better ways of doing things. What you have described is a perfect example of RTTI (Run time type inference).

Polymorphism in object oriented models solves this through mechanisms such as inheritence and [interfacing(not something C++ directly supports)].

I see nothing wrong with your basic model of your class Arianelle. Think about it like this.

Hardware: You have a calculator in hand. A physical object that is mass marketed and every calculator is the same as every other calculator.
OO Code: The calculator is your instantiated object of the calc class. If you instantiated more, they would be exact copies of each other. (initially)

Oh thats just grand! A calculator...

Hardware: But... your calculator is just a shell that does nothing. No power. No buttons. No memory.
Software: You essentially have code that is ClassName {};

Uh... that's useless...

Hardware: Now... you want the calculator to store some numbers... so fair enough... wham, now your calculator has some memory.
Software: You just declared some variables.

Still, its kinda useless... You've just created a grandiose storage device with no way to store data, or get it for that matter.

Hardware: You have to be able to enter numbers, so you add the buttons necessary for the calculator.
Software: You create public setter methods that receive input and store them to the declared variables.

Well shit, you can type the damn numbers into the calculator, but wtf good is that if you can't read what you typed?

Hardware: You add a screen to display the contents of what is in memory.
Software: You create public getter methods that retrieve what is in memory and return it to a calling method.


Long story short, an object is an idea of encapsulating atomic processes. A calculator isn't exactly atomic, but you can conceptualize it as such as I did above. It is a tangible idea that does magic work on numbers and returns a number.


So your day class should be just as general. What does it have to do? It has to be able to store a day. It has to be able to have its day set. It has to be able to have the value of the day it holds retrieved.

Wait... there is more. Who do you want touching your data and how? Do you want the electrical engineer to short circuit your calculator to store invalid data in it's memory, or do you want to force him to use the buttons and only put in what you allow?

That is why there are public and private keywords. Private means that ONLY methods contained within your class can EVER user the private method/variable. Public means the method/variable is publicly accessible.

It is good practice that all variables be private. I won't do your work for you, but I'll see if this pseudocode can't help.

class MilkyWay
enum Planets "MARS, EARTH, VENUS, ..., NEPTUNE"
private: Planets planetName
public: MilkyWay(void)
public: void setPlanet(string)
public: string getPlanet(void)

Using the enumeration I have given myself the data I need to enforce that I only ever have a valid planet in my variable planetName.


----

If you have not covered enumerations, then you're probably not expected to know them and you should ignore my advice. Your model is not entirely wrong and in fact is probably close to what I would have done.
 
You know what.. I think I'll need to relearn my c++ again because of you guys and the fact I haven't coded anything in the past 5 years or so >.>;
 
That enumerator tip helped MAJORLY in my last assignment. It cut out a lot of unnecessary work and because I barely used them in my last class, I didn't even think of it until you'd said something. What you showed is pretty similar to the solution the prof provided so it was really useful. Thanks for the help!

With this new assignment, I seem to be having a mental gap on function variable types.

Here is my class:
Code:
class Time
{
public:
	Time(); //constructor
	[B]void setTime(int h, int m) { hour = h, minute = m; };[/B]
	string getTime(); //returns time
	int getHour { return hour; }; //hour accessor
	int getMinute { return minute; }; //minute accessor
	[B]int plusOneHour(); //increments hr[/B]
	[B]int plusOneMinute(); //increments min[/B]
private:
	int hour;
	int minute;
};

Now, I'm trying to build the plusOneHour() and plusOneMinute() functions that will feed data back into the setTime function:

Code:
int Time::plusOneHour()
{
	int h;
	int m;
	h = hour;
	m = minute;

	h = h + 1;

	if (h >= 24) //hour exceeds 23
	{ return setTime(h-24, m); }
	else
	{ return setTime(h, m); }
}

int Time::plusOneMinute()
{	
	int h;
	int m;
	h = hour;
	m = minute;

	m = m+1;

	if (m < 60)
	{ return setTime(h, m); }
	else //minute exceeds 59
	{ return plusOneHour(); }
}

What variable type should I use for my plusOne() functions, or is there another way to do this?

Also, when dealing with a child class, is it OK to have everything set as public? This is how the prof did it in the lecture, but he said it was just to give us an idea:

Code:
class extTime : Time
{
public:
	extTime(); //constructor
	int TZ; //timezone variable
	setTimezone(int x) { TZ = x; };
	void printTimeWithTimezone();
};

I'm thinking maybe TZ should be private.
 
Back
Top