-1

Can anyone please tell me what have I done wrong in the program below? :

// C++-Assignment2.cpp
#include <iostream.h>
#include <conio.h>
class fahrenheit
{
    private:
        int fah;
    public:
        fahrenheit()
        {
            fah=0;
      }
        void fget();
        void fdisp();
};
class celsius: public fahrenheit
{
    private:
        int cel;
    public:
    void calc();
        void cdisp();
};
void fahrenheit::fget()
{
    cout<<"\n Enter temperature value in Fahrenheits:";
    cin>>fah;
}
void fahrenheit::fdisp()
{
    cout<<"\n  Temperature in Fahrenheits: "<<fah;
}
void celsius::calc()
{
  cel=5*(fah-32)/9;
}
void celsius::cdisp()
{
    cout<<"\n Temperature in Celsius:"<<cel;
}
void main()
{
    clrscr();
    celsius c1;
    c1.fget();
    c1.fdisp();
    c1.calc();
    c1.cdisp();
    getch();
}

I'm sorry if it was asked before, but I couldn't find a question in which the user mentioned that they included a constructor (like how I did). Also, I do understand this program does not really make that much sense (considering how I framed those two units and such). Still a beginner, so not really into 'perfecting' semantics at the moment.

Errors:

Compiling 2-ASSIGN.CPP:
Error 2-ASSIGN.CPP 36: 'fahrenheit::fah' is not accessible in function celsius::calc()
Error 2-ASSIGN.CPP 36: 'fahrenheit::fah' is not accessible in function celsius::calc()
Aditya
  • 52
  • 8
  • 1
    Unrelated, but how silly is that: _`class celsius: public fahrenheit`_ Celsius really _is a_ Farenheit unit?? – πάντα ῥεῖ May 14 '16 at 18:24
  • Sorry, I am a beginner. Haven't really been into complete semantics. I'll surely work out on that once I am comfortable with handling C++. :p – Aditya May 14 '16 at 18:26
  • 2
    Please include the errors you are getting. – Richard Critten May 14 '16 at 18:29
  • I have edited my post, please check them out. – Aditya May 14 '16 at 18:30
  • @student Please add errors as verbatim text in your post. Screenshots aren't useful. – πάντα ῥεῖ May 14 '16 at 18:39
  • @student If you really need to have a class, it should be something like a `TemperatureUnitConverter` that allows decoration with additional temperature unit conversion methods. The straight forward way is to use plain functions. – πάντα ῥεῖ May 14 '16 at 18:47
  • Actually I did use "class convert" before. But that was how I framed my code. Since I couldn't get over the errors (same errors), I thought of rather sticking to the code which was taught to me in a coding lecture. – Aditya May 14 '16 at 19:14

2 Answers2

1

In this member function

void celsius::calc()
{
  cel=5*(fah-32)/9;
}

you are trying to access private data member fah of the base class.

You could declare it as having protected access control. For example

class fahrenheit
{
    protected:
        int fah;
    //...
};

Or you could define a public member function that returns the value of this data member.

As for me then this class hierarchy does not make sense.:)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Okay, I replaced 'private' with 'protected' but doing so the derived class can't access the member functions of the base class now (somehow it seems to access the member data) [Errors](http://s32.postimg.org/rl2ixecud/screenshot_2016_05_15_at_00_05_52.png) – Aditya May 14 '16 at 18:37
  • @student You need to declare protected only the data member. The functions of the class can be public. – Vlad from Moscow May 14 '16 at 18:39
  • I did declare the data members (int fah) as protected leaving the functions as public. – Aditya May 14 '16 at 19:10
  • Also unfortunately, declaring a member function (public access) which returns the value of 'fah' didn't work. It still produced those errors. – Aditya May 14 '16 at 19:25
  • @student You may not access a private or protected data member outside the class or classes where it is declared. So either you can declare a data member as protected and access it in derived classes directly (but not outside the classes) or you can write a public access method that returns the value of the data member. – Vlad from Moscow May 15 '16 at 10:04
0
void celsius::calc()
{
   cel=5*(fah-32)/9; 
 }

You try to access fah which is private.Private members cannot be accessed by derived class. In good design you would have a public getter method in base class which you can use to get the value of fah in derived class.

Something like

int farenhiet::get_fah()
{
    return fah;
}

Then in derived class

void celsius::calc()
{
   cel=5*(get_fah()-32)/9; 
 }
Gaurav Sehgal
  • 7,422
  • 2
  • 18
  • 34
  • I am trying to access fah but I have seen in some questions where the answers mentioned that "to access the private members of the base class, you need to define a constructor". Hopefully, I have not mis-understood them. – Aditya May 14 '16 at 18:33
  • AFAIK this is not true. see this http://stackoverflow.com/questions/18944451/how-to-make-a-derived-class-access-the-private-member-data – Gaurav Sehgal May 14 '16 at 18:34
  • @Gaurav Sehgal: I just saw that edit in your post. I will try the way you suggested tomorrow (its mid-night right now). I hope it works. – Aditya May 14 '16 at 19:15
  • @user3147395: I don't think so. Upto my knowledge, a semi-colon after a function parenthesis means you will declare that function outside the class and I don't think constructors can be "declared" inside the class and then "defined" outside. Please do correct me if I am wrong. – Aditya May 14 '16 at 19:19
  • Nope, that method did not work, unfortunately. I still get those 2 errors. [Errors](http://s32.postimg.org/6cyya7b0l/screenshot_2016_05_15_at_12_21_19.png) – Aditya May 15 '16 at 06:53
  • Also the topic to which you linked to says that I'll have to either use friend (I dont know how to use 'friend' to make a derived class access a base class data member) or protected (which did not work) – Aditya May 15 '16 at 06:57