Thread: Private or Public?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Private or Public?

    Hi, I have a class of some objects. The class is
    Code:
    #ifndef SALES_DATA_H_INCLUDED
    #define SALES_DATA_H_INCLUDED
    #include <string>
    #include <iostream>
    using namespace std;
    class Sales_data{
    friend ostream &print(ostream&,const Sales_data&);
    friend istream &read(istream&,Sales_data&);
    public:
        Sales_data &combine(const Sales_data&);
        Sales_data()=default;
        Sales_data(istream &is)
         {
             double price = 0;
             is >> (*this).bookNo >> (*this).units_sold >> price;
             (*this).revenue = price * (*this).units_sold;
         };
    
        string isbn() const {return bookNo;}
        double avg_price() const {return units_sold?revenue/units_sold:0;}
    private:
        std::string bookNo;
        unsigned units_sold;
        double revenue;
    };
    
    
    Sales_data add(const Sales_data &lhs,const Sales_data &rhs)
    {
        Sales_data sum=lhs;
        sum.combine(rhs);
        return sum;
    }
    ostream &print(ostream &os,const Sales_data &item)
    {
        os<<item.isbn()<<" was sold "<<item.units_sold<<" copies at total tk "<<item.revenue
        <<" and the average price is "<<item.avg_price()<<endl;
        return os;
    }
    //input function
    istream &read(istream &is, Sales_data &item)
    {
    double price = 0;
    is >> item.bookNo >> item.units_sold >> price;
    item.revenue = price * item.units_sold;
    return is;
    }
    Sales_data& Sales_data::combine(const Sales_data &rhs)
    {
      units_sold+=rhs.units_sold;
      revenue+=rhs.revenue;
      return *this;
    }
    
    #endif // SALES_DATA_H_INCLUDED
    But, This doesnt compile and gives error at line 27. Says, objects are private with respect to that context. but there is nothing there..

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Other than the missing main() this compiles without errors for me.

    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is this a header? Then, don't use "using namespace." It's considered bad practice.
    Other than that, you really should be consistent with indentation, remove the "(*this)." parts, and read this: SourceForge.net: Do not remove parameter names - cpwiki
    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.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    My main source file is
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    #include "Sales_data.h"
    #include "funtions.h"
    int main()
    {
    Sales_data total(cin);
    Sales_data sum=total,trans;
    while(read(cin,trans))
    {
    
    
        if(trans.isbn()==total.isbn())
        {
            total.combine(trans);
            sum.combine(trans);
    
        }
        else
        {
            print(cout,total);
            sum.combine(trans);
            total=trans;
        }
    }
    print(cout,total);
    cout<<"total  "<<sum.units_sold<<" books sold in  "<<sum.revenue<<" dollar"<<endl;
    cout<<"average sales price of all book is "<<sum.avg_price()<<" dollar"<<endl;
    
    
    }
    and I have also modified the header.. still dont compile
    Code:
    #ifndef SALES_DATA_H_INCLUDED
    #define SALES_DATA_H_INCLUDED
    
    
    class Sales_data{
    friend ostream &print(ostream&,const Sales_data&);
    friend istream &read(istream&,Sales_data&);
    public:
        Sales_data &combine(const Sales_data&);
        Sales_data()=default;
        Sales_data(istream &is)
         {
             double price = 0;
             is >> (*this).bookNo >> (*this).units_sold >> price;
             (*this).revenue = price * (*this).units_sold;
         };
    
        string isbn() const {return bookNo;}
        double avg_price() const {return units_sold?revenue/units_sold:0;}
    private:
        std::string bookNo;
        unsigned units_sold;
        double revenue;
    };
    
    Sales_data add(const Sales_data&,const Sales_data&);
    ostream &print(ostream&,const Sales_data&);
    istream &read(istream&,Sales_data&);
    
    #endif // SALES_DATA_H_INCLUDED
    and heres the header of my functions
    Code:
    #ifndef FUNTIONS_H_INCLUDED
    #define FUNTIONS_H_INCLUDED
    Sales_data add(const Sales_data &lhs,const Sales_data &rhs)
    {
        Sales_data sum=lhs;
        sum.combine(rhs);
        return sum;
    }
    ostream &print(ostream &os,const Sales_data &item)
    {
        os<<item.isbn()<<" was sold "<<item.units_sold<<" copies at total tk "<<item.revenue
        <<" and the average price is "<<item.avg_price()<<endl;
        return os;
    }
    //input function
    istream &read(istream &is, Sales_data &item)
    {
       double price = 0;
       is >> item.bookNo >> item.units_sold >> price;
       item.revenue = price * item.units_sold;
       return is;
    }
    Sales_data& Sales_data::combine(const Sales_data &rhs)
    {
      units_sold+=rhs.units_sold;
      revenue+=rhs.revenue;
      return *this;
    }
    
    #endif // FUNTIONS_H_INCLUDED

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Don't do this:
    Code:
    using namespace std;
    #include "Sales_data.h"
    #include "funtions.h"
    using directives should not come before header inclusions like that. Neither should they be at file scope in the header files. Rather, fully qualify those names.

    Now, what is the error message(s)?
    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

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Found the error, I was Using
    Code:
    cout<<"total  "<<sum.units_sold<<" books sold in  "<<sum.revenue<<" dollar"<<endl;
    cout<<"average sales price of all book is "<<sum.avg_price()<<" dollar"<<endl;
    in main function though it was private.
    using directives should not come before header inclusions like that. Neither should they be at file scope in the header files. Rather, fully qualify those names.
    Are you suggesting I write the code like this
    Code:
    std::string bookno;
    std::cout<<"bla bla";
    std::ostream()
    like this?? but that takes a lot more extra writings..of the same word 'std' whats the problem in using directives?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, my comments are with respect to your header files at file scope. If you want to have a using directive in a source file after header inclusions, or in a header but within a function scope, go ahead. Just note that indiscriminate use of using directives and using declarations can lead to name collision, possibly causing a compile error or perhaps changing the meaning of your code to something that you did not expect.
    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

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Thanks for the advise.. I will be carefull next time..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. private or public inheritance?
    By KIBO in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2009, 12:57 AM
  2. private/public get/set
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 12:49 AM
  3. Private or Public
    By gtriarhos in forum C# Programming
    Replies: 3
    Last Post: 10-10-2005, 07:14 PM
  4. #define private public
    By revelation437 in forum C++ Programming
    Replies: 21
    Last Post: 12-20-2003, 04:37 PM
  5. public and private
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-10-2002, 11:02 AM