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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Need help in C++ classes

Need help in C++ classes

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpcsharpvisual-studioquestion
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.
  • Q Offline
    Q Offline
    Quecumber256
    wrote on last edited by
    #1

    Good morning, I need help in designing a class in C++. For experimental purposes I started with something simple. // Header File Planets.h Declares class Planets. class Planets { public: double Mercury; private: double weight; }; //Implementation file Planets.cpp implements the member //functions of Class Planets. #include "stdafx.h" #include "Planets.h" double Planets::Mercury // Pre: Earth weight. // Post: Earth weight times the gravitation constant // of Mercury - 0.4155. { return wgh * 0.4155; } // MT_Planets.cpp : main project file. #include "stdafx.h" #include <iostream> #include "Planets.h" using namespace std; int main() { double iWgh = 235.6; double MyWgh; MyWgh = Planets.Mercury(iWgh); cout << "My weight on Mercury is: " << MyWgh << endl; system("pause"); return 0; } When I try to compile the project I get the following error: Error 1 error C2063: 'Planets::Mercury' : not a function c:\Documents and Settings\Mark McCumber\My Documents\Visual Studio 2005\Projects\Visual C++\MT_Planets\MT_Planets\Planets.cpp 10 Can someone explain to me what I'm doing wrong? Quecumber256

    R I 2 Replies Last reply
    0
    • Q Quecumber256

      Good morning, I need help in designing a class in C++. For experimental purposes I started with something simple. // Header File Planets.h Declares class Planets. class Planets { public: double Mercury; private: double weight; }; //Implementation file Planets.cpp implements the member //functions of Class Planets. #include "stdafx.h" #include "Planets.h" double Planets::Mercury // Pre: Earth weight. // Post: Earth weight times the gravitation constant // of Mercury - 0.4155. { return wgh * 0.4155; } // MT_Planets.cpp : main project file. #include "stdafx.h" #include <iostream> #include "Planets.h" using namespace std; int main() { double iWgh = 235.6; double MyWgh; MyWgh = Planets.Mercury(iWgh); cout << "My weight on Mercury is: " << MyWgh << endl; system("pause"); return 0; } When I try to compile the project I get the following error: Error 1 error C2063: 'Planets::Mercury' : not a function c:\Documents and Settings\Mark McCumber\My Documents\Visual Studio 2005\Projects\Visual C++\MT_Planets\MT_Planets\Planets.cpp 10 Can someone explain to me what I'm doing wrong? Quecumber256

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      Quecumber256 wrote:

      class Planets { public: double Mercury; double Mercury();

      Quecumber256 wrote:

      double Planets::Mercury double Planets::Mercury() { return wgh * 0.4155; }

      Mercury - It is a function? Then you missed the ()

      Quecumber256 wrote:

      system("pause");

      You MUST not do this. This is bad in several ways. Something like a getch(); or std::cin.get(); will do instead.

      It is a crappy thing, but it's life -^ Carlo Pallini

      Q 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        Quecumber256 wrote:

        class Planets { public: double Mercury; double Mercury();

        Quecumber256 wrote:

        double Planets::Mercury double Planets::Mercury() { return wgh * 0.4155; }

        Mercury - It is a function? Then you missed the ()

        Quecumber256 wrote:

        system("pause");

        You MUST not do this. This is bad in several ways. Something like a getch(); or std::cin.get(); will do instead.

        It is a crappy thing, but it's life -^ Carlo Pallini

        Q Offline
        Q Offline
        Quecumber256
        wrote on last edited by
        #3

        Rajesh, Thank you for the reply, but I'm still getting a problem. I'm trying to reprogram my brain to use OOP instead of procedural programming. The problem is simple in concept. Take a person's Earth weight and calcultate their weight on the other planets of our Solar system. The trick here is it has got to be done using classes and constructors. For example: MyWgh = Planets.Mercury(235.5) would give me their weight on the planet Mercury. According to the instructions the default constructor should return the weight enter for Earth. Can you help me grasp this concept? Quecumber256

        C 1 Reply Last reply
        0
        • Q Quecumber256

          Good morning, I need help in designing a class in C++. For experimental purposes I started with something simple. // Header File Planets.h Declares class Planets. class Planets { public: double Mercury; private: double weight; }; //Implementation file Planets.cpp implements the member //functions of Class Planets. #include "stdafx.h" #include "Planets.h" double Planets::Mercury // Pre: Earth weight. // Post: Earth weight times the gravitation constant // of Mercury - 0.4155. { return wgh * 0.4155; } // MT_Planets.cpp : main project file. #include "stdafx.h" #include <iostream> #include "Planets.h" using namespace std; int main() { double iWgh = 235.6; double MyWgh; MyWgh = Planets.Mercury(iWgh); cout << "My weight on Mercury is: " << MyWgh << endl; system("pause"); return 0; } When I try to compile the project I get the following error: Error 1 error C2063: 'Planets::Mercury' : not a function c:\Documents and Settings\Mark McCumber\My Documents\Visual Studio 2005\Projects\Visual C++\MT_Planets\MT_Planets\Planets.cpp 10 Can someone explain to me what I'm doing wrong? Quecumber256

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          The idea of the assignment will be to teach you polymorphism. I'm assuming you'll want a base class of CPlanet, with more specialised inheriting classes which over-ride the member functions of CPlanet. So, you would have CEarth, and CMercury both inheriting from CPlanet, and each overriding (eg) GetWeight (double dMass), and GetDistanceFromSun (), and so on. // slightly advanced bit here As there is no need for the CPlanet class to even have an implementation of GetDistanceFromSun, you could even leave it as a pure virtual member function, forcing any inheriting classes to implement it. // end of slighty advanced bit. You would end up with:

          CEarth earth;
          CMercury mercury;
          double weight;
          
          weight = earth.GetWeight (100);
          cout << "100 kg on earth is : " << weight << endl;
          weight = mercury.GetWeight (100);
          cout << "100 kg on mercury is : " << weight << endl;
          

          and could even end up with:

          CPlanet \*planet = GetNthPlanet (5);
          double weight;
          weight = planet->GetWeight (100);
          cout << "100 kg on 5th planet is : " << weight << endl;
          
          delete planet;
          

          CPlanet *GetNthPlanet (int n)
          {
          switch (n)
          {
          case 1: return new CMercury;
          case 2: return new CVenus;
          // and so on
          case 9:
          if ( (rand() %100) < 10)
          return CPluto;
          else
          return CPlanetoid;
          }
          return NULL; // FAIL!
          }

          I'm hesitant to write much more, as I don't want to spoonfeed you - if you don't struggle a bit, you don't learn. Iain.

          Q 1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            The idea of the assignment will be to teach you polymorphism. I'm assuming you'll want a base class of CPlanet, with more specialised inheriting classes which over-ride the member functions of CPlanet. So, you would have CEarth, and CMercury both inheriting from CPlanet, and each overriding (eg) GetWeight (double dMass), and GetDistanceFromSun (), and so on. // slightly advanced bit here As there is no need for the CPlanet class to even have an implementation of GetDistanceFromSun, you could even leave it as a pure virtual member function, forcing any inheriting classes to implement it. // end of slighty advanced bit. You would end up with:

            CEarth earth;
            CMercury mercury;
            double weight;
            
            weight = earth.GetWeight (100);
            cout << "100 kg on earth is : " << weight << endl;
            weight = mercury.GetWeight (100);
            cout << "100 kg on mercury is : " << weight << endl;
            

            and could even end up with:

            CPlanet \*planet = GetNthPlanet (5);
            double weight;
            weight = planet->GetWeight (100);
            cout << "100 kg on 5th planet is : " << weight << endl;
            
            delete planet;
            

            CPlanet *GetNthPlanet (int n)
            {
            switch (n)
            {
            case 1: return new CMercury;
            case 2: return new CVenus;
            // and so on
            case 9:
            if ( (rand() %100) < 10)
            return CPluto;
            else
            return CPlanetoid;
            }
            return NULL; // FAIL!
            }

            I'm hesitant to write much more, as I don't want to spoonfeed you - if you don't struggle a bit, you don't learn. Iain.

            Q Offline
            Q Offline
            Quecumber256
            wrote on last edited by
            #5

            Iain, Thanks, but this gets away from my initial problem; defining the Planets class for use. Quecumber256

            1 Reply Last reply
            0
            • Q Quecumber256

              Rajesh, Thank you for the reply, but I'm still getting a problem. I'm trying to reprogram my brain to use OOP instead of procedural programming. The problem is simple in concept. Take a person's Earth weight and calcultate their weight on the other planets of our Solar system. The trick here is it has got to be done using classes and constructors. For example: MyWgh = Planets.Mercury(235.5) would give me their weight on the planet Mercury. According to the instructions the default constructor should return the weight enter for Earth. Can you help me grasp this concept? Quecumber256

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Something similar to:

              #include <iostream>
              using namespace std;

              class Planet
              {
              private:
              double _dFactor;

              public:
              Planet(double dFactor = 1.0) : _dFactor(dFactor) {}

              double localWeight(double dWeightOnEarth)
              {
              return dWeightOnEarth * _dFactor;
              }
              };
              void main()
              {
              Planet planetEarth, planetMercury(.38);
              double dYourWeight = 235.6;

              cout << "If your weight on Earth is " << planetEarth.localWeight(dYourWeight) << endl;
              cout << "then weight on Mercury is " << planetMercury.localWeight(dYourWeight) << endl;
              }

              ? :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              Q 1 Reply Last reply
              0
              • C CPallini

                Something similar to:

                #include <iostream>
                using namespace std;

                class Planet
                {
                private:
                double _dFactor;

                public:
                Planet(double dFactor = 1.0) : _dFactor(dFactor) {}

                double localWeight(double dWeightOnEarth)
                {
                return dWeightOnEarth * _dFactor;
                }
                };
                void main()
                {
                Planet planetEarth, planetMercury(.38);
                double dYourWeight = 235.6;

                cout << "If your weight on Earth is " << planetEarth.localWeight(dYourWeight) << endl;
                cout << "then weight on Mercury is " << planetMercury.localWeight(dYourWeight) << endl;
                }

                ? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                Q Offline
                Q Offline
                Quecumber256
                wrote on last edited by
                #7

                Thanks, this example helped me with my problem. Quecumber256

                C 1 Reply Last reply
                0
                • Q Quecumber256

                  Thanks, this example helped me with my problem. Quecumber256

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  I'm glad of. BTW did you notice .38 instead of 0.4155? :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  Q 1 Reply Last reply
                  0
                  • C CPallini

                    I'm glad of. BTW did you notice .38 instead of 0.4155? :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    Q Offline
                    Q Offline
                    Quecumber256
                    wrote on last edited by
                    #9

                    Yes I did, but I used 0.4155 because that was the number provided in the text. I bet the number you gave me was the most accurate one. Quecumber256

                    C 1 Reply Last reply
                    0
                    • Q Quecumber256

                      Yes I did, but I used 0.4155 because that was the number provided in the text. I bet the number you gave me was the most accurate one. Quecumber256

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      Well, actually your number is accurate, expressing the wrong physical quantity. :-D

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      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