•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 429,791 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,800 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 5890 | Replies: 3
![]() |
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,972
Reputation:
Rep Power: 32
Solved Threads: 117
Declaring Objects
When one works with classes, each instance of the class which is defined is known as an object. C++ is an extremely object oriented language (perhaps not so much so as Java, however). In OOP, or Object Oriented Programming, one can easily perform multiple tasks on objects which are created. For example, when one declares an instance of the class, they can enact numerous functions upon that instance of the class.
Structures
Structures, or structs, are a simpler form of a class. They allow the creation of objects which handle multiple variables, but no functions. For such a reason, all variables within a struct can be considered public to the entire program. While structure instances are declared the same was as class instances, one can only work upon a variable in a struct (as opposed to variables and functions in a class).
struct studentThe above is a simple structure which handles students. By declaring student Jason; for example, the variable Jason is created of type student. This is similar to how an instance of a class is created. One can deal with the name and gpa of Jason by simply using Jason.name[] and Jason.gpa.
{
char[10] name;
float gpa;
};
Pointers
In its simplest form, RAM (random access memory) can be throught of as a huge series of slots with addresses, each containing different data used by the computer. Pointers are directly related to a computer's use of variables stored in RAM. A pointer is an address of where data is stored in memory.
Pointers allow the memory management available in C++ to really go into full swing. You can theoretically choose the exact place in memory you wish for objects to occupy. In addition, you can overwrite objects in memory, copy memory addresses, etc.
Passing By Reference
It is possible to pass variables by reference into a function. When passing by reference, only the memory address of a variable is being passed in. Variables are passed by reference by following their data types with an ampersand (&) symbol.
Of course, when variables are being passed into a function, they act simply like local variables. Theoretically, copies of these variables are created in RAM and these copies are used inside of the function. If their values are altered in any way, it is only temporary. As soon as the function runs its course, the original values are used. For this reason, the same variable name can exist in multiple functions, each storing a different value or even data type.
All of this is different with variables which are passed by reference. When a variable is passed into a function by reference, changes made to the variable are permanently altered in the function calling the function in question.
When one uses a return statement in a function, they can only return one piece of data or one variable. However, suppose one needed to write a function to return multiple variables. That is when passing by reference comes into play. This can be accomplished by writing a void function, thus with no return statement (or just a return; statement with no value.) Pass any number of variables by reference into this void function. Then, any changes made inside the function will be directly altered from where it was called from. This is almost like a workaround for only being able to use make use of one return statement per function call.
void square(int size, int& area, int& perimeter)In the above program, the size variable is used to represet the size of one side of a square. This variable is being passed into the function normally. The area and perimeter variables, however, are being passed by reference. This is because the purpose of this function is to compute both the area and perimeter and send them back to the function which called square(). This is possible by passing these two parameters by reference.
{
area = size ^ 2;
perimeter = size * 4;
return;
};
Dani the Computer Science Gal
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,972
Reputation:
Rep Power: 32
Solved Threads: 117
Post your specific question in the programming forums please
Dani the Computer Science Gal
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
api apple asm assembly x86 programming hla demo blogger blogging c c++ ccna cocoa code coding competition compiler compilers computer debugging developer development errors evaluation framework gdata google high-performance innovation java languages linerider mcse microsystems networking news next object online open oriented planning platform practices programming python rss software step steps sun tools tutorials xml
- Object Oriented Programming (Computer Science and Software Design)
Other Threads in the C++ Forum
- Previous Thread: Building a database application
- Next Thread: Array or String Comparing Assignment



Linear Mode