Thread: Can Someone Translate this text

  1. #16
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    An initialiser list is the preferred method for assigning values to data members in class constructors. For example:

    Code:
    // initialise members to zero / null
    planet::planet() : weight(0), newweight(0){} // strings are default initialised to a null string
    or:

    Code:
    // initialise members to supplied values
    planet::planet(const std::string& p, int w, int n) : plan(p), weight(w), newweight(n){}
    If you initialise data members in the body of a constructor, it means that they are first default constructed, then assigned to. An initialiser list is more efficient as they are both constructed and initialised at the same time. This method is mandatory if a data member is a constant.

  2. #17
    Registered User
    Join Date
    Sep 2005
    Posts
    10
    Ahem,

    Code:
    // initialise members to supplied values
    planet::planet(const std::string& p, int w, float n) : plan(p), weight(w), newweight(n){}

  3. #18
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Hey guys and girls, I changed my program around a little, I just like to know if I'm going in the right direction from what is required above.
    Code:
    // this is my header file
    class planet
    {
    private:
    		float weight;
    		float newweight;
    		string plan;
    public:
    	planet();
    	planet(float initweight, float initnewweight);
    	float Getweight () const;
    	float Getnewweight () const;
    	void Write() const;
    };
    // followed by my .cpp file
    #include "gravity.h"
    #include <iostream>
    #include <string>
    
    planet::planet()
    {
    	weight = 0;
    	newweight = 0 ;
    	plan = "Earth";
    }
    planet::planet(float initweight, float initnewweight)
    {
    	weight =  initweight;
    	newweight = initnewweight;
    	plan = initplanet;
    }
    planet::planet(string initplanet)
    {
    	plan = initplanet;
    }
    
    float planet::Getweight () const
    {
    	return weight;
    }
    float planet::Getnewweight () const
    {
    	return newweight;
    }
    void planet::Write () const
    {
    	cout << "your weight on " << plan << "is" << newweight << endl;
    }
    // followed by my client file
    #include <iostream>
    #include "gravity.h"
    
    using namespace std;
    
    int main()
    {
    	cout << "Please enter your weight" << endl;
    	cin >> initweight;
    	cout << "Please enter the planet you wish to know how much you weight on, if you were to enter an 
    	incorrect planet name it will be assumed that Earth was your choice" << endl;
    	cin >> initplanet;
    	initweight.planet();
    	initweight.Getweight();
    	initweight.Write();
    	return 0;
    }
    currently, I only wanted to check one thing, and that's calling the function. Apparently, I just don't know how.
    Last edited by lanh215; 01-17-2006 at 05:12 AM.

  4. #19
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    I broke it down to eight errors
    Code:
    /Users/orbitgalaxy/scrap/gravity.h:6: error: 'string' does not name a type
    /Users/orbitgalaxy/scrap/main.cpp:9: error: 'plan' was not declared in this scope
    /Users/orbitgalaxy/scrap/main.cpp:15: error: 'plan' was not declared in this scope
    /Users/orbitgalaxy/scrap/main.cpp:15: error: 'initplanet' was not declared in this scope
    /Users/orbitgalaxy/scrap/main.cpp: At global scope:
    /Users/orbitgalaxy/scrap/main.cpp:17: error: expected `)' before "initplanet"
    /Users/orbitgalaxy/scrap/main.cpp:32: error: 'cout' was not declared in this scope
    /Users/orbitgalaxy/scrap/main.cpp:32: error: 'plan' was not declared in this scope
    /Users/orbitgalaxy/scrap/main.cpp:32: error: 'endl' was not declared in this scope

  5. #20
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    going crazy because I'm just going around and around in circles, so I'm just going to start the program over in scratch again. Starting with a piece of blank white paper.
    Thanks for the help ladies and gentlemen. Forums are great, but I would rather have your aim and program while chatting. Oh, and if there are any hint at all, that anyone could give me on my rough journey into pencil and paper programming. Please let me know. I'll be here 24.
    Thanks all

  6. #21
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    Code:
    #include <string>
    include it to your .h file and see what happens..

    also use this

    Code:
    std::string plan;
    instead of
    Code:
    string plan;
    Last edited by what3v3r; 01-17-2006 at 05:32 AM.

  7. #22
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Ok, forget every stupid thing I post, I figured it out. well most of it, yay. However, the one part i'm stuck on is creating a function that takes a weight on Earth as an argument and returns the weight on the planet.
    Let's say the planet is Mercury with a gravitational pull of 0.4155. And Earth with a gravitational pull of 1.0. How would I associate the string Earth with the float 1.0? and how do I take a "weight on earth as an argument" when the user inputs the string "Earth"? This is the only code I am stuck on, and I would really appreciate anyone's help on this one. Thanks a whole lot.

  8. #23
    Registered User
    Join Date
    Dec 2005
    Posts
    54
    Quote Originally Posted by CornedBee
    I claim - and Google agrees with me - that there is no such thing as an observer operator in C++ programming.

    BTW, do you have to do it in C++? The introduction to typesafe enums in the Java5 documentation contains most of the code such a program would need in Java.
    possibly they are supposed to overload an operator...or maybe they just have poor wording and are supposed to make a function that acts like an observer? heh

  9. #24
    Registered User
    Join Date
    Jan 2006
    Posts
    12
    Sorry, but fyi, there are theoretically three categories of basic operations, and they are constructors, transformers, and "observers." The observer function allows programmers to observe the state of an instance of an Abstract Data Type without changing it. Try googling it again with these concepts. Thanks for all the help guys

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    there are theoretically three categories of basic operations, and they are constructors, transformers, and "observers." The observer function allows programmers to observe the state of an instance of an Abstract Data Type without changing it.
    The thing is that the term 'operator' is not the same as the term 'function', so they should not be used interchangeably. That said, this "transformers/observers" concept sounds suspiciously like a "setters/getters" concept, and often setter and getter functions arent the best way of doing things.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble drawing text xlib.
    By Qui in forum Linux Programming
    Replies: 1
    Last Post: 05-10-2006, 12:07 PM
  2. Appending text to an edit control
    By dit6a9 in forum Windows Programming
    Replies: 3
    Last Post: 08-13-2004, 09:52 PM
  3. Text positioning and Text scrolling
    By RealityFusion in forum C++ Programming
    Replies: 3
    Last Post: 08-13-2004, 12:35 AM
  4. Scrolling The Text
    By GaPe in forum C Programming
    Replies: 3
    Last Post: 07-14-2002, 04:33 PM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM