User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,972
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 117
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Object-Oriented Programming

  #1  
Nov 16th, 2003


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 student
{

char[10] name;
float gpa;

};

The 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.

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)
{

area = size ^ 2;
perimeter = size * 4;
return;

};

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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 1
Reputation: sakia is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
sakia sakia is offline Offline
Newbie Poster

Re: Object-Oriented Programming

  #2  
Apr 16th, 2004
I need help for a programming a data structures using Java chess competition can you help me?please
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,972
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 117
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: Object-Oriented Programming

  #3  
Apr 16th, 2004
Post your specific question in the programming forums please
Reply With Quote  
Join Date: Feb 2005
Location: New York
Posts: 11
Reputation: Pmaster is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Pmaster's Avatar
Pmaster Pmaster is offline Offline
Newbie Poster

Solution Re: Object-Oriented Programming

  #4  
Feb 28th, 2005
I have to 2 C++ books. I have been struggling with this topic.

Your explanation made it simple and in much LESS words!


thanks
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:24 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC