0% found this document useful (0 votes)
10 views

oop (1)

Uploaded by

Hamza Bilal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

oop (1)

Uploaded by

Hamza Bilal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Private:

#include<iostream>
using namespace std;
class baseclass
{
private:
int s;
protected:
int t;
public:
int u;
baseclass()
{
s = 11;
t = 12;
u = 13;
}
};
class deriveclass: private baseclass
{
//t and u becomes private members of deriveclass and s will remain private
public:
void show ()
{
cout << "s is not accessible";
cout << "\nt is " << t;
cout << "\nu is " << u;
}
};
int main()
{
deriveclass l; //object created
l.show();
//l.s = 11; not valid : private members are inaccessible outside the class
//l.t = 12; not valid
//l.u = 13; not valid : t and u have become derived class’ private members
return 0;

OUTPUT:
Protected:

#include<iostream>
using namespace std;
class baseclass
{
private:
int a;
protected:
int b;
public:
int c;
baseclass()
{
a = 10;
b = 11;
c = 12;
}
};
class deriveclass: protected baseclass
{
//b and c becomes protected members of deriveclass
public:
void show ()
{
cout << "a is not accessible";
cout << "\nb is " << b;
cout << "\nc is " << c;
}
};
int main()
{
deriveclass d; // object created
d.show();
//d.a = 10; not valid : private members are inaccessible outside the class
//d.b = 11; not valid
//d.c = 12; not valid : b and c have become derived class’ private member
return 0;

OUTPUT:
PUBLIC:

#include<iostream>
using namespace std;
class baseclass
{
private:
int u;
protected:
int v;
public:
int w;
baseclass()
{
u = 3;
v = 4;
w = 5;
}
};
class deriveclass: public baseclass
{
//v becomes protected and w becomes public members of class derive
public:
void show()
{
cout << "u is not accessible";
cout << "\nvalue of v is " << v;
cout << "]\nvalue of w is " << w;
}
};
int main()
{
deriveclass c;
c.show();
//c.u = 3; not valid: private members are inaccessible outside the class
//c.v = 4; not valid: v is now protected member of derived class
//c.w = 5; valid: w is now a public member of derived class
return 0;
}

OUTPUT:

CONCLUSION:
Whenever data needs any kind of restriction of accessibility that can be set as private or protected
so that only the authorized functions can access them. Otherwise, the data can be set as public
which can be accessed from anywhere in the program by any class in inheritance.

You might also like