LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2010, 12:41 PM   #1
gregarion
Member
 
Registered: Jan 2010
Posts: 69

Rep: Reputation: 15
Using Variables


Hey guys im facing a problem in declaring variables. i have a few classes like the one below...



Code:
#ifndef _FINANCE_H
#define	_FINANCE_H

#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std ;


class  readStraitsTimesIndex
{
    public:
         void StraitsTimesIndex(fstream&) ;
    private:
};

class readDow
{
    public:
        void Dow(fstream&) ;
    private:
 };
what i am trying to do is to use a value which is being the output of one of the class class and to be displayed into the other class. I tried using an object member like the one below ,

Code:
class  readStraitsTimesIndex
{
    public:
         void StraitsTimesIndex(fstream&) ;
String value ;
    private:

};
and then assigned a value to it, but it will only work within the class. how do i go about saving a certain variable and using it in all the classes
 
Old 02-04-2010, 02:26 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
You can access public variables from a class just as you would the member functions, eg:
Code:
readStraitsTimesIndex test;
std::cout << test.value << std::endl;
However, generally speaking, variables in an object oriented program should be private to their classes. Instead, one implements member functions that return the information that you require in another class.

This may begin by being as simple as a function that returns the value. But by having a function, the implementation can be changed over time without changing the outside interaction with the class.

If you find you have a lot of these 'global' variables, then you have probably not understood object oriented design.

Last edited by neonsignal; 02-04-2010 at 02:29 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Threads synchronisation problem (mutex variables and contitional variables) zahadumy Programming 6 12-07-2005 12:30 PM
please ..help me on variables bruse Linux - Newbie 8 04-16-2005 11:57 PM
variables? Soulful93 Programming 7 06-13-2004 12:24 AM
Shel scripting: variables pointing to variables and case Dark_Helmet Programming 5 06-08-2003 11:07 AM
variables granny Linux - Newbie 2 02-12-2003 04:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration