## [C++] Tutorial 1: Discussion

I have a question about my C++ program....I have to compute the area of a rectangle.....I have to write this program using functions....I have my instructions correctly output to the user in the command prompt and I am allowing the user to enter in a value for the length and the width....that's all fine....I wrote the computation for the solution to the problem but the cout info to appear on the screen but that part does not show up along with the answer.....can anyone help me?
Attached Files
 area.doc (28.0 KB, 9 views)
 My C++ is a little rusty. I want to have a class that has a 3 by 3 array of integers as a member variable. I want the class to return the array through a member function. I am having trouble getting the initialization right. I think you can tell what I want to do from the following program although of course the compiler would go crazy if I tried to compile this. Can someone please tell me how to rewrite this so it is correct? Code: class MyClass { public: //this function should return the array int * MyFunction(){ return my_array; } //default constructor MyClass(){ my_array = {{1,2,3},{4,5,6},{7,8,9}}; } private: int my_array [3][3]; }
 Hmmm. I think they kind of provide a solution at the MSDN website: http://msdn.microsoft.com/en-us/library/2xfh4c7d.aspx I have a few questions about that...does anyone understand what they are doing there?
 For ehrenfest - For your first question, the trouble you have is about the return type. You are returning a pointer to integers. That will work for one dimensional arrays, but not for higher dimensional arrays. Your 3 by 3 array is actually a pointer to pointers to integers. That is, the first pointer has an array of other pointers. The lower level pointers each have arrays of three integers. So, you could make the return type usable by making it a pointer to pointers. That said, I would suggest against doing it. What you are returning is a direct access into the memory held by the class. Anyone who makes a change to this return value is changing the contents of the object, and not just some local version of the array. Even worse, the object could be destroyed (by going out of scope, for example) and leave you with a pointer that no longer points where you think it points. This is a seg fault or other corruption waiting to happen. Reconsider why you want to do this. It may be that the return doesn't help you and you really want a copy constructor or an accessor function. It may be that you don't really want the safety of encapsulating the data. In which case, it is a better design idea not to try and fake safety that isn't really there. As for your second question - they are working with managed memory containers in the visual compilers. Only try to follow them if you do not ever need to use this for anything other than the visual platform. John
 Hello: I am using MS Visual Studio as the compiler. I have a c++ routine that I needed in my current project, which I saved under my Header Files. This file is needed for the compiler to interpret certain variable declarations. However, when I F7 to compile I get the following error: Cannot open include file: 'nr3.h': No such file or directory But I can clearly see the nr3.h file in my project. What is wrong? Please find my code below: #pragma once #include "nr3.h" class gamma { Doub gammln(const Doub xx) { . . . } Main program: #include "gamma.h" #include "incgammabeta.h" #include "nr3.h" using namespace std; int main() { double a, b, x, incompleteBeta; . . . return 0; }
 Is the directory that holds the nr3.h header in your include paths? That is the typical cause of this error. John
 Hello, I am a beginner to C++ and I have a couple of inquiries on the subject. First, do I have to be a mathematician to program C++? And lastly, if so, what mathematical knowledge must I have?
 No, you don't. Any programming requires you to think algorithmically, and that is a skill mathematicians tend to have, but you don't have to be a mathematician to program. John

 Quote by John_Phillips No, you don't. Any programming requires you to think algorithmically, and that is a skill mathematicians tend to have, but you don't have to be a mathematician to program. John
Thanks John, that cleared some stuff up for me. I'm not bad at math, but I'm not all that good....Does any one else know of any mathematics I may need, besides arrays?
 hey, i dont think you need any maths beyond the basics at school for programming, its more about being able to plan your program out in your head and work out how your going to do it, then you can get into the nitty gritty and google most of the stuff you have trouble with :P you dont even have to understand what a mathematical array is. you can just think of one as like an excel sheet :P logical laying of stuff out i guess is why it suits mathematicians :P what kinda things do you want to learn from your programming? ive done a fair bit now in some pretty different languages, and C++ may not necessarily be the best thing to start with for you... if you want to make applications with windows and things, VB or C# could be a good place to start; its pretty and easy. i dont know what everyone else thinks?

 Quote by samski hey, i dont think you need any maths beyond the basics at school for programming, its more about being able to plan your program out in your head and work out how your going to do it, then you can get into the nitty gritty and google most of the stuff you have trouble with :P you dont even have to understand what a mathematical array is. you can just think of one as like an excel sheet :P logical laying of stuff out i guess is why it suits mathematicians :P what kinda things do you want to learn from your programming? ive done a fair bit now in some pretty different languages, and C++ may not necessarily be the best thing to start with for you... if you want to make applications with windows and things, VB or C# could be a good place to start; its pretty and easy. i dont know what everyone else thinks?
Okay, that makes sense as well :D what about Python as a first language? I also dug up an old book on QBasic. is it good to learn this as a first language for organization etc...? or is Python better for a first?

EDIT: Also I heard that Ruby is making improvements with speed, would this be better over python for a first?

 Quote by Rancour Okay, that makes sense as well :D what about Python as a first language? I also dug up an old book on QBasic. is it good to learn this as a first language for organization etc...? or is Python better for a first? EDIT: Also I heard that Ruby is making improvements with speed, would this be better over python for a first?
hey, well i first learnt to program using pbasic, this is used to program picaxe microcontrollers, really basic stuff. i guess qbasic might be similar...

i first learnt to program a computer using a program called autohotkey, its really easy to use

then i did a project using visual basic 6 (old stuff) and picked that up fairly quickly

im now doing a project on C# and finding that not so bad after my previous experience

once you know how to structure a program, the toughest challenge is finding the right commands and understanding the concepts of new languages. (eg, i can understand/program the nitty gritty of a C# program, but i get my head in a twist when it comes to putting that code into methods and how to declare things).

try qbasic, im not sure how bad it is. or have a go at autohotkey and pm me if u need any help.

another thing i find when learning to program is: always try and set yourself a target. dont just program lines together. think of something cool you could do with the language (have a read of the introduction first so you know your not gonna be stretching what is possible with it). that way, even though you might be guessing a bit at first, you learn very fast what goes where and i find the understanding of stuff comes later (eg methods in C#, i had no idea, but i get it now :P)

big jump from something like pbasic to visual basic etc is you never use the 'goto' command in VB, thats the basic of object orientated programming.

edit: oh and look at example code in the language, youll probably find yourself doing this alot as you search for something and find an example script. looking helps you keep with the conventions, and speeds up the learning curve. its probably best to create a new "programming" folder now!
 hello what are the real n imaginary numbrz in c++ classes definng our own operators
 Recognitions: Gold Member Science Advisor Staff Emeritus In the standard C++ library, the type "complex" is defined as complex( const Type& _RealVal = 0, const Type& _ImagVal = 0 ) There is no "real" except as referring to the real part, _RealVal, of a complex number. Real numbers are represented as "int", "float", or "double".
 god i am getting into computer science myself.um can i learn the basics of c++here