• No results found

class derived-class: access-specifier base-class

N/A
N/A
Protected

Academic year: 2022

Share "class derived-class: access-specifier base-class"

Copied!
9
0
0

Loading.... (view fulltext now)

Full text

(1)

DHA Suffa University

CS 103–Object Oriented Programming Fall 2015

Lab #13: Inheritance Inheritance

Inheritance is a mechanism of reusing and extending existing classes without modifying them, thus producing hierarchical relationships between them.

Inheritance is one of the most important concepts in object-oriented programming. A class that is used as the basis for inheritance is called a superclass or base class. A class that inherits from a superclass is called a subclass or derived class. The terms parent class and child class are also acceptable terms to use respectively. A child inherits properties and methods from its parent while adding additional properties and methods of its own. This provides an opportunity to reuse the code functionality and reduce implementation time.

The idea of inheritance implements the is-a relationship. For example,

Base & derived Classes:

A class can be derived from more than one class, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:

class derived-class: access-specifier base-class

Where access-specifier is one of public, protected, or private, and base-class is the name of a

previously defined class. If the access-specifier is not used, then it is private by default.

(2)

Example#01 //Shape.h

#include <iostream>

using namespace std;

#ifndef SHAPE_H

#define SHAPE_H //Base class class Shape {

protected:

int width;

int height;

public:

void setWidth(int w);

void setHeight(int h);

};

#endif // SHAPE_H //Shape.cpp

#include "Shape.h"

void Shape::setWidth(int w) {

width = w;

}

void Shape::setHeight(int h) {

height = h;

}

//Rectangle.h

#include "Shape.h"

class Rectangle: public Shape {

public:

int getArea();

};

//Rectangle.cpp

#include "Rectangle.h"

int Rectangle::getArea() {

return (width * height);

}

(3)

Access Control and Inheritance:

A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to who can access them in the following way:

Access public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no

Private, public and protected Inheritance

We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:

Public Inheritance: When deriving a class from a public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class.

Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class.

Private Inheritance: When deriving from a private base class, public and protected

members of the base class become private members of the derived class.

(4)

Task 01 : write a source code of given classes

Shape

#color:String

#filled:boolean +Shape()

+Shape(color:string,filled:boolean) +getColor():String

+setColor(color:String):void +isFilled():boolean

+setFilled(filled::boolean):void +getArea():double

+getPerimeter:double

Circle Rectangle

#radius:double #width:double

+Cirlce() #length:double

+Circle(radius:double) +Rectangle() +Circle(radius:double,

color:String, filled:boolean) +Rectangle(width:double, length:double) +getRadius():double

+Rectangle(width:double, length:double, color:String, filled:boolean)

+setRadius(radius:double):void +getWidth():double

+getArea():double +setWidth(width:double):void +getPerimeter():double +getLength():double

+setLength(length:double):void +getArea():double

+getPerimeter():double

Square +Square()

+Square(side:double)

+Square(side:double, color:String,

(5)

Task01 main.cpp int main () {

circle circleObject;

circleObject.setRadius(2.245);

circleObject.getArea();

CircleObject.getPerimeter();

circleObject.setFilled(true);

}

/* similarly create the object of remaining class */

Multiple inheritance

You can derive a class from any number of base classes. Deriving a class from more than one direct base class is called multiple inheritances.

In the following example, classes A, B, and C are direct base classes for the derived class X:

class A { /* ... */ };

class B { /* ... */ };

class C { /* ... */ };

class X : public A, public B, public C { /* ... */ };

The following inheritance graph describes the inheritance relationships of the above example. An

arrow points to the direct base class of the class at the tail of the arrow:

(6)

Example#2 //Shape.h

#include <iostream>

using namespace std;

// Base class Shape class Shape

{

protected:

int width;

int height;

public:

void setWidth(int w);

void setHeight(int h);

};

//Shape.cpp

#include "Shape.h"

void Shape::setWidth(int w) {

width = w;

}

void Shape::setHeight(int h) {

height = h;

}+

//PaintCost.h

// Base class PaintCost

#include <iostream>

using namespace std;

class PaintCost {

public:

int getCost(int area);

};

//PaintCost.cpp

#include "PaintCost.h"

int PaintCost::getCost(int area) {

return area * 70;

}

//Rectangle.h

#include "Shape.h"

(7)

//Rectangle.cpp // Derived class

#include "Rectangle.h"

int Rectangle::getArea() {

return (width * height);

}

//main.cpp

#include "Rectangle.h"

int main(void) {

Rectangle rect;

int area;

rect.setWidth(5);

rect.setHeight(7);

area = rect.getArea();

// Print the area of the object.

cout << "Total area: " << rect.getArea() << endl;

// Print the total cost of painting

cout << "Total paint cost: $" << rect.getCost(area) << endl;

return 0;

}

(8)

Multilevel Inheritance

In C++ programming, a class be can derived from a derived class which is known as multilevel inheritance.

Example#3 //Animal.h

#include<iostream>

using namespace std;

class Animal {

public:

int life;

Animal();

void showHealth();

};

//Animal.cpp

#include "Animal.h"

Animal::Animal() {

life=100;

}

void Animal::showHealth() {

cout<<"\n This is Animal Class with Health="<<life;

}

//Tiger.h

(9)

//Tiger.cpp

#include "Tiger.h"

Tiger::Tiger() {

}

void Tiger::attack() {

cout<<"\n This class can Attack !!";

}

//SaberTooth.h

#include "Tiger.h"

class SaberTooth : public Tiger {

public:

int bigFangs;

SaberTooth();

void attackMore();

};

//SaberTooth.cpp

#include "SaberTooth.h"

SaberTooth::SaberTooth() {

bigFangs=10;

}

void SaberTooth::attackMore() {

cout<<"\n This class can Attack Cruelly!!";

}

//main.cpp

#include "SaberTooth.h"

int main() {

cout<<"\n---- Animal Class---\n";

Animal a;

a.showHealth();

cout<<"\n---- Tiger Class--- \n";

Tiger t;

t.showHealth();

t.attack();

cout<<"\n---- SaberTooth Class--- \n";

SaberTooth s;

s.showHealth();

s.attackMore();

return 0;

}

References

Related documents

› With private inheritance, public and protected members of the base class become private members of the derived class. › Private members are still private to the

– protected : Derived classes and friend s can access protected members of the base

class, only the public and protected members of base class can be accessed by the member functions of derived class. This means no private member of the base class

9.6 Overriding Base-Class Members in a Derived Class 9.7 Public, Protected and Private Inheritance.. 9.8 Direct Base Classes and Indirect

protected: only member functions of the same class or of derived classes can access it: other classes or global functions can’t public: every function can access it. class MyClass

 Private methods of the base class are not accessible to a derived class (unless the derived class is a friend of the base class).  If the subclass is derived

 Private methods of the base class are not accessible to a derived class (unless the derived class is a friend of the base class).  If the subclass is derived

Table 4.8 Pearson Correlation Statistics for the Variables 97 Table 4.9 Regression Result of transformational leadership and Crisis Management 99 Table 4.10 Regression Result