Thread: Classes and private:

  1. #1
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Classes and private:

    I know I can use what I put into private with in the class but I am having issues tying it all together. Can anyone clear this up?

    Code:
    #include <iostream>
    
    class Plot {
        
        void area(float x, float y)
        {
            
            int Area = x * y;
            
            std::cout<< "The area is: "<< Area << std::endl;
        
        }
        void parameter(float x, float y)
        {
            
            int parameter = 2 * (x + y);
            
            std::cout<<"The parameter is: "<< parameter << std::endl;
        }
        
    public:
        float length;           //not using these right
        float width;
    };
    
    int main(int argc, const char * argv[])
    {
     
       Plot calculations;
        
        float inLength;
        float inWidth;
    
        std::cout<<"Please enter the lenth and width of the object in question?"<<std::endl;
        
        std::cout<<"Lenth:"<<std::endl;
        std::cin>>inLength;
        std::cout<<"Width: "<<std::endl;
        std::cin>>inWidth;
        
      
        calculations.area(inLength, inWidth);          //member of a private member, failing
        calculations.parameter(inLength, inWidth);     //member of a private member, failing
        
        
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    By default the access to a class in C++ is private, if you want public or protected members you must use the public: and protected: specifiers. I always recommend you always state your intentions by always using the access specifiers:

    Code:
    class bogus
    {
       public:
          // some public members.
    
       protected:
          // some protected members.
    
       private:
          // some private members.
    };
    Jim

  3. #3
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    ugh....

    I thought declared public and private that I was blind to looking for it. But look at this. I want length to be private so if I just write it a variables within the class that would be correct?
    Code:
     #include <iostream>
    
    class Plot {
    public:
        void area(float x, float y)
        {
            
            int Area = x * y;
            
            std::cout<< "The area is: "<< Area << std::endl;
        
        }
        void parameter(float x, float y)
        {
            
            int parameter = 2 * (x + y);
            
            std::cout<<"The parameter is: "<< parameter << std::endl;
        }
        
    private:
        float length;
        float width;
    };
    
    int main(int argc, const char * argv[])
    {
     
       Plot calculations;
        
        float inLength;
        float inWidth;
    
        std::cout<<"Please enter the lenth and width of the object in question?"<<std::endl;
        
        std::cout<<"Lenth:"<<std::endl;
        std::cin>>inLength;
        std::cout<<"Width: "<<std::endl;
        std::cin>>inWidth;
        
      
        calculations.area(inLength, inWidth);
        calculations.parameter(inLength, inWidth);
        
        
        return 0;
    }
    Like this:
    Code:
     class Plot {
    public:
        void area(float length, float width)
        {
            
            int Area = length * width;
            
            std::cout<< "The area is: "<< Area << std::endl;
        
        }
        void parameter(float length, float width)
        {
            
            int parameter = 2 * (length + width);
            
            std::cout<<"The parameter is: "<< parameter << std::endl;
        }
        
    private:
        float length;
        float width;
    };

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I don't really see any point of your private class variables, you never use them so why even have them.

    Jim

  5. #5
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    I want to but I cannot find a place to use them.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Perhaps you need to rethink the purpose of your class? Maybe you should create a function to assign a value to these variables? Then instead of passing parameters into your member functions just use the private member variables.


    Jim

  7. #7
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Am I wrong in thinking I am assigning a value to these variables this way?

    Code:
     #include <iostream>
    
    class Plot {
    public:
        
        float getLength(float)
        {
        
            return length;
        }
        float getWidth(float)
        {
        
            return width;
        }
        void area()
        {
            
            int Area = length * width;
            
            std::cout<< "The area is: "<< Area << std::endl;
        
        }
        void parameter()
        {
      
            int parameter = 2 * (length + width);
            
            std::cout<<"The parameter is: "<< parameter << std::endl;
        }
        
    private:
        float length;
        float width;
    };
    
    int main(int argc, const char * argv[])
    {
     
       Plot calculations;
        
        float inLength;
        float inWidth;
    
        std::cout<<"Please enter the lenth and width of the object in question?"<<std::endl;
        std::cout<<" "<<std::endl;
        std::cout<<"Lenth:"<<std::endl;
        std::cin>>inLength;
        std::cout<<"Width: "<<std::endl;
        std::cin>>inWidth;
        
        calculations.getLength(inLength);
        calculations.getWidth(inWidth);
      
        calculations.area();
        calculations.parameter();
        
        
        return 0;
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, you are. GetLength() and GetWidth() takes unnecessary parameters with which it does nothing with and returns the private member variables (it does absolutely not assign them).
    Area() and Parameter() (perimeter?) calculates the area and perimeter from these variables, but they are never assigned.
    Perhaps you meant to say that GetLength and GetWidth be SetLength and SetWidth and actually assign the private variables instead of returning them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Like this?

    Code:
     #include <iostream>
    
    class Plot {
    public:
        void setLength(float x)
        {
        
            length = x;
        
        }
        void setWidth(float y)
        {
            
            width = y;
            
        }
        void area()
        {
            
            int Area = length * width;
            
            std::cout<< "The area is: "<< Area << std::endl;
        
        }
        void perimeter()
        {
      
            int perimeter = 2 * (length + width);
            
            std::cout<<"The parameter is: "<< perimeter << std::endl;
        }
        
    private:
        
        float length;
        float width;
    };
    
    int main(int argc, const char * argv[])
    {
     
       Plot calculations;
        
        float inLength;
        float inWidth;
    
        std::cout<<"Please enter the lenth and width of the object in question?"<<std::endl;
        std::cout<<" "<<std::endl;
        std::cout<<"Lenth:"<<std::endl;
        std::cin>>inLength;
        std::cout<<"Width: "<<std::endl;
        std::cin>>inWidth;
        
        calculations.setLength(inLength);
        calculations.setWidth(inWidth);
      
        calculations.area();
        calculations.perimeter();
        
        
        return 0;
    }

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, precisely so.
    area and perimeter should probably be getters (ie, returning said calculations instead of printing it).
    It might also be good to document your member variables by prefixing them with something (like m_). This allows you to distinguish between local variables and class member variables more easily.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499
    Thank you very much...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. private vectors of classes
    By greatunknown in forum C++ Programming
    Replies: 8
    Last Post: 03-10-2010, 11:23 AM
  2. Trouble with private portion of Classes
    By SoBlue in forum C++ Programming
    Replies: 3
    Last Post: 01-22-2007, 05:32 PM
  3. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  4. Public vs. Private variables in classes
    By blankstare77 in forum C++ Programming
    Replies: 13
    Last Post: 08-31-2005, 05:43 PM
  5. Classes: public or private?
    By RealityFusion in forum C++ Programming
    Replies: 11
    Last Post: 08-19-2003, 08:52 PM