Class Access Modifiers

You might also like

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 3

// The public members

#include <iostream>
using namespace std;
class Line
{
public:
double length;
void setLength(double len);
double getLength();
};
double Line::getLength(void)
{
return length;
}
void Line::setLength(double len)
{
length = len;
}
int main()
{
Line obj;
obj.setLength(100);
cout << "\n Length of line : " << obj.getLength() << endl;
obj.length = 10;
cout << "\n Length of line : " << obj.length << endl;
return 0;
}

// The private members:


#include <iostream>
using namespace std;
class Box
{
public:
double length;
void setWidth(double wid);
double getWidth();

};

private:
double width;

void Box::setWidth(double wid)


{
width = wid;
}
double Box::getWidth()
{
return width ;
}
int main()
{
Box obj;
obj.length = 100;
cout << "\n Length of box : " << obj.length << endl;
obj.setWidth(50);
cout << "\n Width of box : " << obj.getWidth() << endl;
}

return 0;

// The protected members:


#include <iostream>
using namespace std;
class Box
{
protected:
double width;
};
class SmallBox:Box
{
public:
void setSmallWidth(double wid);
double getSmallWidth();
};
void SmallBox::setSmallWidth(double wid)
{
width = wid;
}
double SmallBox::getSmallWidth()
{
return width;
}
int main()
{
SmallBox obj;
obj.setSmallWidth(100);
cout << "\n Width of box: " << obj.getSmallWidth() << endl;
}

return 0;

You might also like