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
#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; ^~~~
int SetNumber() const { return Num1; }(example forBaseClass1) (this is joke: You choose a nameGetNumberfor the setter, so choosing a nameSetNumberfor the getter sounds reasonable)Num1is declaredprivatetoBaseClass1. Unless friended, no one (including derived classes) can access it by name.privatemembers toprotectedmembers. That makes thempublicfor derived classes butprivatefor everything else.GetNumbermethods should rather be calledSetNumber