Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 16

ACCESS

SPECIFIERS
A class is an expanded concept of a data
structure: instead of holding only data, it can
hold both data and functions.

Classes are generally declared using the


keyword class, with the following format

class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2; ...
} object_names;
 private members of a class are
accessible only from within other members
of the same class or from their friends.
 protected members are accessible from
members of their same class and from
their friends, but also from members of
their derived classes.
 Finally, public members are accessible
from anywhere where the object is visible.
PRIVATE

• The private keyword


specifies that those
members are accessible
only from member functions
and friends of the class.
#include <iostream>
using namespace std;
class CRectangle
{
int width, height; output:
public:
CRectangle (); rect area: 25
CRectangle (int,int);
int area (void) {
return (width*height); rectb area: 12
}
};
CRectangle::CRectangle ()
{ width = 5;
height = 5;
}
CRectangle::CRectangle (int a, int b)
{
width = a;
height = b;
}
int main ()
{
CRectangle rect (3,4); // implicit call
CRectangle rectb; //calls default constructor
cout << "rect area: " << rect.area() << endl;
cout << "rectb area : " << rectb.area() << endl;
return 0;
}
PUBLIC
• public members are accessible from
anywhere where the object is visible
• Eg
public :
int a, b;
void getdata();
# include <iostream>
using namespace std;
class CRectangle
{
int x, y;
public:
void setvalues (int,int);
int area () OUTPUT:
{
return (x*y); area: 12
}};
void CRectangle::set_values (int a, int b)
{
x = a;
y = b;
}
int main ()
{
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
PROTECTED ACCESS
SPECIFIER:
 Accessible by member functions
within its class and any class
immediately derived from it
 When protected member inherited in:
1. public mode : It becomes protected in
derived class
2. private mode: It becomes private in
derived class
3. protected mode: It becomes protected
in derived class
WHEN TO USE PROTECTED ACCESS
SPECIFIER ?..

When it is appropriate for a


class's subclasses to have
access to the
method or field ,but not for
unrelated classes.
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
char colour[30];
public:
void get_colour()
{
cout<<"Enter colour:\n";
cin>>colour;
cout<<"Inside shape class::\n"<<colour<<endl;
}
};
class two_d:public shape
{
protected:
float s_len;
public:
void get_slen()
{
cout<<"Enter length for square:"<<endl;
cin>>s_len;
}
class three_d:public shape
{
protected: int main()
int c_len; {
public:
void get_clen()
square s;
{ cube c;
cout<<"Enter the lenth of cube:"<<endl; clrscr();
cin>>c_len; s.get_colour();
}
s.get_slen();
};
class cube:public three_d s.get_area();
{ c.get_colour();
protected: c.get_clen();
int c_vol; c.get_volume();
public:
void get_volume()
getch();
{ return 0;
c_vol=(c_len*c_len*c_len); }
cout<<"Volume of cube:"<<c_vol<<endl;
}
};
OUTPUT:
Enter colour:
BLUE
Inside shape class::
BLUE
Enter length for square:
20.0
Area of square : 400

Enter colour:
RED
Inside shape class::
RED
Enter the lenth of cube:
25.0
Volume of cube : 15625
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
char colour[30];
public:
void get_colour()
{
cout<<"Enter colour:\n";
cin>>colour;
cout<<"Inside shape class::\n"<<colour<<endl;
}
};
class two_d : protected
public shape
{
protected:
float s_len;
public:
void get_slen()
{
cout<<"Enter length for square:"<<endl;
cin>>s_len;
}
class three_d:public shape
{
protected: int main()
int c_len; {
public:
void get_clen()
square s;
{ cube c;
cout<<"Enter the lenth of cube:"<<endl; clrscr();
cin>>c_len; s.get_colour();
}
s.get_slen();
};
class cube:public three_d s.get_area();
{ c.get_colour();
protected: c.get_clen();
int c_vol; c.get_volume();
public:
void get_volume()
getch();
{ return 0;
c_vol=(c_len*c_len*c_len); }
cout<<"Volume of cube:"<<c_vol<<endl;
}
};
ERROR IS SHOWN AS:

•Error PROGRAM.CPP: 'shape::get_colour()' is not accessible


THANK YOU!

You might also like