0

Sir / Mam please tell me why this c++ program calling the base class variable when its private ??

#include<iostream> using namespace std; class base { private : int basevar; public: base() { cout<<"enter the value of base variable "; cin>>basevar; } void display() { cout<<"base variable value is "<<basevar<<endl; } }; class derived : public base { private: int dervar; public: derived() { cout<<"enter the value of derived variable "; cin>>dervar; } void display() { base:: display(); cout<<"derived variable value is "<<dervar<<endl; } }; int main() { derived a,b; a.display(); b.display(); return 0; }

28th Jan 2023, 3:10 AM
Harshit Gupta
2 Answers
+ 4
In the same class private variables or members function are accessible inside base class u have declared one variable basevar and constructor and another Function display when u will create object constructor will call and here u taking input and display Function will print this basevar . Try to access this variable in main or derived class it will give u error
28th Jan 2023, 3:41 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
0
Ooh thanks bro
12th Feb 2023, 10:23 AM
Harshit Gupta