[go: up one dir, main page]

0% found this document useful (0 votes)
21 views5 pages

Lab8 Inheritance 2

Uploaded by

ysjgjnjzhm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views5 pages

Lab8 Inheritance 2

Uploaded by

ysjgjnjzhm
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab 8

Example 1: Inheritance #1: Private data in the BASE class:


#include <iostream>
using namespace std;
class CRectangle
{
private:
int width, length;
public:
void set_width (int w)
{width=w;}
void set_length (int l)
{ length = l; }
int area ()
{ return (width*length); }
};
class Box: public CRectangle
{
int height;
public:
void set_height (int h)
{ height=h; }
int volume ()
{ return (area()*height); }
/*private members of the base class cannot be accessed by the derived class
*/
//int volume () {return width*length*height);}
};

int main() {
CRectangle rect ;
Box box;
rect.set_width(3);
rect.set_length(4);
box.set_width(3); //inherited
box.set_length(4); //inherited
box.set_height(5);
cout << "Rectangle area: " << rect.area() << endl;
1
cout << "Box base area: " << box.area() << endl; // inherited
cout << "Box volume: " << box.volume() << endl;
return 0 ;
}
Example 2: Inheritance #2: Protected data in the BASE class:
#include <iostream>
using namespace std ;

class CRectangle
{
protected:
int width, length ;
public:
void set_width (int w)
{ width=w; }
void set_length (int l)
{ length=l; }
int area ()
{ return (width*length); }
};

class Box: public CRectangle


{
int height;
public:
void set_height (int h)
{ height=h; }
/*protected members of the base class can be accessed by the
derived class */
int volume ()
{ return (width*length*height); }
};

int main(){
CRectangle rect;
Box box;
rect.set_width(3);
rect.set_length(4);
box.set_width(3);

2
box.set_length(4);
box.set_height(5);
cout << "Rectangle area: " << rect.area() << endl;
cout << "Box base area: " << box.area() << endl;
cout << "Box volume: " << box.volume() << endl;
return 0 ;
}

Program 12.1: Inheritance #3: Circle & Cylinder:


#include <iostream>
using namespace std ;

const double PI = 3.14 ;


// class declaration section
class Circle
{
protected:
double radius ;
public:
Circle(double = 1.0) ; // constructor
double calcval() ;
};

// class implementation section for Circle


Circle::Circle(double r) // constructor
{
radius = r ;
}
// calculate the area of a Circle
double Circle::calcval()
{

3
return(PI * radius * radius) ;
}
// class declaration section where Cylinder is derived
from Circle
class Cylinder : public Circle
{
protected:
double length ; // add one data member and
public: // two member functions
Cylinder(double r = 1.0, double l = 1.0)
{
radius = r ;
length = l ;
}
double calcval() ;
};
// class implementation section for Cylinder
double Cylinder::calcval() // calculates a volume
{
return (length * Circle::calcval()) ; // note the base function call
}

int main()
{
Circle Circle_1, Circle_2(2) ; // create two Circle objects
Cylinder Cylinder_1(3,4) ; // create one Cylinder object

cout << "The area of Circle_1 is " << Circle_1.calcval() << endl ;
cout << "The area of Circle_2 is " << Circle_2.calcval() << endl ;
cout << "The volume of Cylinder_1 is " << Cylinder_1.calcval() << endl ;
return 0;

4
}

You might also like