Thread: using constructor for simple c++ code

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    30

    using constructor for simple c++ code

    I want to write a simple c++ code that will calculate some data for me. Since I'm new with c++ and I want to work with constructors to learn how they work, I have a problem and don't know how to solve it.
    Code:
    #include <iostream>
     
    using namespace std;
     
    class calc {
       public:
          double setPro (double pro );
          double getPro( void );
          calc();  // This is the constructor
       private:
          double prozent;
    };
     
    // Member functions definitions including constructor
    calc::calc(void) {
       cout << "Object is being created" << endl;
    }
    double calc::setPro( double pro ) {
       pro = prozent;
       cout<<prozent;
    }
    double calc::getPro( void ) {
       return prozent;
    }
    
    
    // Main function for the program
    int main() {
       calc quanten;
     
      
    
    
       quanten.setPro(10); 
       
     
       return 0;
    }
    When I run the code, I get the number 1.79154e-307 which is probably the size of the integer. For the begining I simply want to display the number I handle the function setPro. (In this case it's 10)
    Thanks for your help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > pro = prozent;
    You probably meant
    prozent = pro;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2017
    Posts
    30
    Yes you are right. I still don't understand why I need this lane. Why do I need a private variable (generaly) and why do I have to say prozent = pro? I just don't see the advantage of this methode.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    The value on the left side of a "=" sign is set to the value on the right side.
    The use of OOP getters and setters is basic in OOP programming.
    Lookup the term encapsulation for at least part of the reason to do it.

    Edit: Added link did not read the info at the link Encapsulation (computer programming) - Wikipedia

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-23-2012, 10:47 PM
  2. Replies: 3
    Last Post: 11-16-2009, 07:51 AM
  3. Replies: 10
    Last Post: 06-02-2008, 08:09 AM
  4. Why it is not good code for constructor
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 12-27-2007, 08:29 AM
  5. simple constructor
    By 1999grandamse in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2002, 03:37 PM

Tags for this Thread