0

Dear all I have this code running on visual studio . I am getting error as below I have taken example from books. I have installed visual studio recently. I have tested basic code working well. I am getting Error for Include files so try to correct Include path error. Kindly suggest me what can be done

Link video

IntialError Correction Made

    #include <iostream>
#include <cstring>

using namespace std;

class BaseClass1
{
    private:
        int Num1;
    public:
        void GetNumber(int);
};

class BaseClass2
{
    private:
        int Num2;
    public:
        void GetNumber1(int);
};


class Derived :public BaseClass1,public BaseClass2
{
    public:
        void Display_Result(void);
};
//************************************************************

void BaseClass2::GetNumber1(int b)
{
    Num2=b;
}

//************************************************************

void BaseClass1::GetNumber(int a)
{
    Num1=a;
}

//************************************************************

void Derived::Display_Result()
{
 
 cout<<"Num1"<<Num1<<endl;
 cout<<"Num2"<<Num2<<endl;
 cout<<"Result"<<(Num2*Num1)<<endl;


}


int main(int argc, char** argv) {
    
Derived D;
D.GetNumber(50);
D.GetNumber1(100);
D.Display_Result();

    return 0;
}

Error I am getting

 C:\Desktop\C++ coding\Visual_basic> cd "c:Desktop\C++ coding\Visual_basic\" ; if ($?) 
{ g++ Inheitance.cpp -o Inheitance } ; if ($?) { .\Inheitance }
Inheitance.cpp: In member function 'void Derived::Display_Result()':
Inheitance.cpp:55:16: error: 'int BaseClass1::Num1' is private within this context
  cout<<"Num1"<<Num1<<endl;
                ^~~~
Inheitance.cpp:9:7: note: declared private here
   int Num1;
       ^~~~
Inheitance.cpp:56:16: error: 'int BaseClass2::Num2' is private within this context
  cout<<"Num2"<<Num2<<endl;
                ^~~~
Inheitance.cpp:17:7: note: declared private here
   int Num2;
       ^~~~
Inheitance.cpp:57:19: error: 'int BaseClass2::Num2' is private within this context
  cout<<"Result"<<(Num2*Num1)<<endl;
                   ^~~~
Inheitance.cpp:17:7: note: declared private here
   int Num2;
       ^~~~
Inheitance.cpp:57:24: error: 'int BaseClass1::Num1' is private within this context
  cout<<"Result"<<(Num2*Num1)<<endl;
                        ^~~~
Inheitance.cpp:9:7: note: declared private here
   int Num1;
       ^~~~
akash
  • 1
  • 1
  • There seems several ways to fix. One way is adding a public *getter* of the private members and use them. Getters will be like `int SetNumber() const { return Num1; }` (example for `BaseClass1`) (this is joke: You choose a name `GetNumber` for the setter, so choosing a name `SetNumber` for the getter sounds reasonable) – MikeCAT Aug 12 '21 at 10:42
  • 2
    The error is self-explanatory. `Num1` is declared `private` to `BaseClass1`. Unless friended, no one (including derived classes) can access it by name. – WhozCraig Aug 12 '21 at 10:43
  • 2
    Adding to what was already said, the standard way to fix this is to change the `private` members to `protected` members. That makes them `public` for derived classes but `private` for everything else. – Louis Cloete Aug 12 '21 at 10:46
  • your naming is somehow reversed. Usually getters and setters are named from the callers perspective, because thats where the name is used. Your `GetNumber` methods should rather be called `SetNumber` – 463035818_is_not_an_ai Aug 12 '21 at 11:12
  • Dear all . I have taken sample from Working Example from Book. Same i posted Here.This code work on Dev C++ IDE but not on Visual Studio. Devc++ IDe after some time it get hangs. – akash Aug 12 '21 at 12:39

0 Answers0