Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Declaring Variables

Declaring Variables

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gregarion
    wrote on last edited by
    #1

    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:
    };
    [/CODE]

    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:

    };[/CODE]

    and then assigned the output of that class to the variable(value) , but it will only work within the class. how do i go about saving a certain variable from the output of a certain class and using it for the other variables.

    M 1 Reply Last reply
    0
    • G gregarion

      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:
      };
      [/CODE]

      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:

      };[/CODE]

      and then assigned the output of that class to the variable(value) , but it will only work within the class. how do i go about saving a certain variable from the output of a certain class and using it for the other variables.

      M Offline
      M Offline
      Maximilien
      wrote on last edited by
      #2

      not certain I understand completely your problem, wchich sounds a lot like a very basic problem. something like this? :

      class A
      {
      private:
      int m_a;
      public:
      int f(){return m_a;};
      };

      class B
      {
      private:
      int m_b;
      void f()
      {
      A a;
      m_b = a.f();
      };
      };

      or something like this?:

      class A
      {
      private:
      int m_a;
      public:
      int GetA(){return m_a;};
      };

      class B
      {
      private:
      int m_b;
      public:
      void SetB( int b){ m_b = b;};
      };

      void main()
      {
      A a;
      B b;
      b.SetB( a.GetA() );
      }

      depending on the relation between A and B, this could be done in a multitude of different ways.

      This signature was proudly tested on animals.

      G 1 Reply Last reply
      0
      • M Maximilien

        not certain I understand completely your problem, wchich sounds a lot like a very basic problem. something like this? :

        class A
        {
        private:
        int m_a;
        public:
        int f(){return m_a;};
        };

        class B
        {
        private:
        int m_b;
        void f()
        {
        A a;
        m_b = a.f();
        };
        };

        or something like this?:

        class A
        {
        private:
        int m_a;
        public:
        int GetA(){return m_a;};
        };

        class B
        {
        private:
        int m_b;
        public:
        void SetB( int b){ m_b = b;};
        };

        void main()
        {
        A a;
        B b;
        b.SetB( a.GetA() );
        }

        depending on the relation between A and B, this could be done in a multitude of different ways.

        This signature was proudly tested on animals.

        G Offline
        G Offline
        gregarion
        wrote on last edited by
        #3

        Sorry , let me try to make it clear. For example, in class A, it will extract a certain value. What i want to do is to save this value in a way that it can be called out in any other classes, either for just displaying the information or to do some calculations.

        L M 2 Replies Last reply
        0
        • G gregarion

          Sorry , let me try to make it clear. For example, in class A, it will extract a certain value. What i want to do is to save this value in a way that it can be called out in any other classes, either for just displaying the information or to do some calculations.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You need to add some extra methods in your class to return the data in whatever way the caller needs. Try rereading the C++ guide(s) or books on classes and how to use them.

          MVP 2010 - are they mad?

          G 1 Reply Last reply
          0
          • L Lost User

            You need to add some extra methods in your class to return the data in whatever way the caller needs. Try rereading the C++ guide(s) or books on classes and how to use them.

            MVP 2010 - are they mad?

            G Offline
            G Offline
            gregarion
            wrote on last edited by
            #5

            What should i be looking for exactly when i read the book?

            L 1 Reply Last reply
            0
            • G gregarion

              Sorry , let me try to make it clear. For example, in class A, it will extract a certain value. What i want to do is to save this value in a way that it can be called out in any other classes, either for just displaying the information or to do some calculations.

              M Offline
              M Offline
              Maximilien
              wrote on last edited by
              #6

              Well, you need something to "hold" the data that class A will retrieve. for example If class A is simply reading data from a file, then class B could hold the data for it. For example in MFC, you have the class CStdioFile that only reads text from a file, but in a software that use that class, I will need something to hold the text (for example a vector of text lines that are read). Another variation, sometimes if the class is more or less specialized it could be used to read and hold data at the same time. for example I could have a class MyStdIoFile that is derived from CStdioFile that will not only read from the file, but store the data internally.

              class MyStdIoFile : public CStdioFile
              {
              public:
              Read( CString path ){/* call code from CStdIoFile to read and put it in m_Data*/ };

              protected:
              vector m_Data;
              }

              again, there are tons of different ways of doing this, it's more a question of design than a question of code; who reads, who writes. who keep the data, who display the data, how is it read, how is it kept, how is it displayed. Take a piece of paper and draw littles boxes representing your different objects and draw lines and arrows between them to show the relationships.

              This signature was proudly tested on animals.

              G 1 Reply Last reply
              0
              • M Maximilien

                Well, you need something to "hold" the data that class A will retrieve. for example If class A is simply reading data from a file, then class B could hold the data for it. For example in MFC, you have the class CStdioFile that only reads text from a file, but in a software that use that class, I will need something to hold the text (for example a vector of text lines that are read). Another variation, sometimes if the class is more or less specialized it could be used to read and hold data at the same time. for example I could have a class MyStdIoFile that is derived from CStdioFile that will not only read from the file, but store the data internally.

                class MyStdIoFile : public CStdioFile
                {
                public:
                Read( CString path ){/* call code from CStdIoFile to read and put it in m_Data*/ };

                protected:
                vector m_Data;
                }

                again, there are tons of different ways of doing this, it's more a question of design than a question of code; who reads, who writes. who keep the data, who display the data, how is it read, how is it kept, how is it displayed. Take a piece of paper and draw littles boxes representing your different objects and draw lines and arrows between them to show the relationships.

                This signature was proudly tested on animals.

                G Offline
                G Offline
                gregarion
                wrote on last edited by
                #7

                Okay , that sounds complicated. Basically why i need it to store the output given out in a certain class is because i need to use it for cppunit testing later on. Basically, each class in my project will give out a certain output based on the document it reads For example, classA gives an output of 333. ClassB gives an output of 434. what i was trying to do is to try to save this values in a way that when i write my cppunit, i can use it to check if it is equal to the hardcoded input.

                1 Reply Last reply
                0
                • G gregarion

                  What should i be looking for exactly when i read the book?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  gregarion wrote:

                  What should i be looking for exactly when i read the book?

                  The fact that you need to ask this question suggests you need to look at everything. If you do not understand how classes hold their data and how they return it to other objects/callers then you are missing the fundamentals of object oriented programming; a significant hole in your knowledge.

                  MVP 2010 - are they mad?

                  R 1 Reply Last reply
                  0
                  • L Lost User

                    gregarion wrote:

                    What should i be looking for exactly when i read the book?

                    The fact that you need to ask this question suggests you need to look at everything. If you do not understand how classes hold their data and how they return it to other objects/callers then you are missing the fundamentals of object oriented programming; a significant hole in your knowledge.

                    MVP 2010 - are they mad?

                    R Offline
                    R Offline
                    Rozis
                    wrote on last edited by
                    #9

                    Richard MacCutchan wrote:

                    The fact that you need to ask this question suggests you need to look at everything. If you do not understand how classes hold their data and how they return it to other objects/callers then you are missing the fundamentals of object oriented programming; a significant hole in your knowledge.

                    I vote a 10 for this one! :)

                    L 1 Reply Last reply
                    0
                    • R Rozis

                      Richard MacCutchan wrote:

                      The fact that you need to ask this question suggests you need to look at everything. If you do not understand how classes hold their data and how they return it to other objects/callers then you are missing the fundamentals of object oriented programming; a significant hole in your knowledge.

                      I vote a 10 for this one! :)

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      ;) :-\

                      MVP 2010 - are they mad?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups